Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.7:除了第一行之外,如何使用readlines()读取整个文件?_Python_Python 3.x_File_Python 3.7 - Fatal编程技术网

Python 3.7:除了第一行之外,如何使用readlines()读取整个文件?

Python 3.7:除了第一行之外,如何使用readlines()读取整个文件?,python,python-3.x,file,python-3.7,Python,Python 3.x,File,Python 3.7,我正在编写一个词汇程序,在这个程序中,你可以输入任意多的单词,并将它们翻译成另一种语言。这些单词保存在.txt文件中。然后你可以在Python控制台中打开这个文件,程序会问你一个单词,你应该用另一种语言输入翻译。但在第一行中,我有两种语言,后来我将它们拆分,然后再次使用它们。但是当程序询问词汇表时,我使用readlines(),然后程序还会询问语言的翻译(第一行),例如: German Translation: 但是我不想要这个,我想要程序读取这个文件中的每一行,除了第一行。我不知道这个文件

我正在编写一个词汇程序,在这个程序中,你可以输入任意多的单词,并将它们翻译成另一种语言。这些单词保存在.txt文件中。然后你可以在Python控制台中打开这个文件,程序会问你一个单词,你应该用另一种语言输入翻译。但在第一行中,我有两种语言,后来我将它们拆分,然后再次使用它们。但是当程序询问词汇表时,我使用readlines(),然后程序还会询问语言的翻译(第一行),例如:

German
Translation: 
但是我不想要这个,我想要程序读取这个文件中的每一行,除了第一行。我不知道这个文件中有多少行,因为用户可以输入任意多的单词

非常感谢你的帮助!这是我的代码,我在这里读到了这些行:

with open(name + ".txt", "r") as file:
        for line in file.readlines():
            word_1, word_2 = line.split(" - ")
            newLanguage_1.append(word_1)
            newLanguage_2.append(word_2)
with open(f"{name}.txt", "r") as file:
     next(file)
     for line in file:
         word_1, word_2 = line.split(" - ")
         newLanguage_1.append(word_1)
         newLanguage_2.append(word_2)

只需跳过第一行,file对象
file
由迭代器生成:

with open(name + ".txt", "r") as file:
        for line in file.readlines():
            word_1, word_2 = line.split(" - ")
            newLanguage_1.append(word_1)
            newLanguage_2.append(word_2)
with open(f"{name}.txt", "r") as file:
     next(file)
     for line in file:
         word_1, word_2 = line.split(" - ")
         newLanguage_1.append(word_1)
         newLanguage_2.append(word_2)
作为理解:

with open(f"{name}.txt", "r") as file:
     next(file)
     newLanguage_1, newLanguage_2 = zip(*(l.split(" - ") for l in file))

只需跳过第一行,file对象
file
由迭代器生成:

with open(name + ".txt", "r") as file:
        for line in file.readlines():
            word_1, word_2 = line.split(" - ")
            newLanguage_1.append(word_1)
            newLanguage_2.append(word_2)
with open(f"{name}.txt", "r") as file:
     next(file)
     for line in file:
         word_1, word_2 = line.split(" - ")
         newLanguage_1.append(word_1)
         newLanguage_2.append(word_2)
作为理解:

with open(f"{name}.txt", "r") as file:
     next(file)
     newLanguage_1, newLanguage_2 = zip(*(l.split(" - ") for l in file))

您可以通过调用
fd
上的
next
跳过第一行(因为文件对象是迭代器),如


您可以通过调用
fd
上的
next
跳过第一行(因为文件对象是迭代器),如

您可以添加一个计数器

with open(name + ".txt", "r") as file:
    i=0
    for line in file.readlines():
        if i==0:
            pass
        else:
            word_1, word_2 = line.split(" - ")
            newLanguage_1.append(word_1)
            newLanguage_2.append(word_2)
        i+=1
您可以添加一个计数器

with open(name + ".txt", "r") as file:
    i=0
    for line in file.readlines():
        if i==0:
            pass
        else:
            word_1, word_2 = line.split(" - ")
            newLanguage_1.append(word_1)
            newLanguage_2.append(word_2)
        i+=1

打开(“..”)作为f:…下一个(f);对于f中的行:…
您可以使用
next(fd)
跳过
fd中的第一行。
您可以做一个格式化示例吗。我不太明白你用open(“…”)作为f:…next(f)表示这个
是什么意思;对于f中的行:…
您可以使用
next(fd)
跳过
fd中的第一行。
您可以做一个格式化示例吗。我不太明白你的意思是什么是的,而且当你需要拆分一些
str
时,使用
str.partition
而不是
str.split
,所以你总是可以期待3个项目而不是一些未知数量的项目:)你的意思是我可以有一定数量的项目吗?不,你总是会得到3个带有
str.partition
的项目,所以你可以分别像
word1、\ux、word2='foo-bar'.partition(“”)
,你会在
word
word2
中得到
foo
bar
,我只是用next()跳过第一行,对吗?然后它每隔一行读取一次,不管有多少行?是的,for循环在逐行读取时会耗尽
fd
,直到EOFYep,当您需要
split
some
str
时,使用
str.partition
而不是
str.split
,所以你总是可以期待3个项目,而不是未知数量的项目:)你的意思是我可以有一定数量的项目吗?不,你总是会得到3个带有
str.partition
的项目,所以你可以像
word1,u,word2='foo-bar'。partition(“”)
,你会在
word
中得到确切的
foo
bar
,和
word2
分别使用next()跳过第一行,对吗?它每隔一行读取一次,不管有多少行?是的,for循环在逐行读取时会耗尽
fd
,直到EOF这也可能是一个解决方案,但我认为next()方法更容易!无论如何,谢谢:)这也可能是一个解决方案,但我认为next()方法更简单!无论如何谢谢你:)