第一行无法从Python中的文本文件加载

时间:2018-11-26 18:41:06

标签: python python-3.x

我是Python的新手,我不明白为什么我的代码无法加载第一行。有人可以看看吗?

我的代码是:

f = open("test.txt")
line = f.readline()

joined=[]

while line:
    line=f.readline().split()
    for x in line:
        joined.append(line)

f.close()

print(joined)

“ test.txt”文件如下所示:

This is the 1st line !
This is the 2nd line .
This is the 3rd line ?
This is the 4th line
This is the 5th line .

我明白了(第一行缺失,也有重复的条目):

[['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '2nd', 'line', '.'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.'], ['This', 'is', 'the', '5th', 'line', '.']]

但是所需的输出是:

[['This', 'is', 'the', '1st', 'line', '!'], ['This', 'is', 'the', '2nd', 'line', '.'],  ['This', 'is', 'the', '3rd', 'line', '?'], ['This', 'is', 'the', '4th', 'line'], ['This', 'is', 'the', '5th', 'line', '.']]

还有,有没有办法将所有列表中的所有字符都小写?

4 个答案:

答案 0 :(得分:0)

您将舍弃第一个readline()返回的值,这就是为什么文件的第一行在输出中丢失的原因。您可以改为以迭代器的方式遍历文件对象:

joined = []
for line in f:
    joined.append(line.split())
print(joined)

答案 1 :(得分:0)

您可以使用列表理解来遍历在空间上分割的行:

with open('test.txt') as f:
    print([x.split() for x in f])

# [['This', 'is', 'the', '1st', 'line', '!'], 
#  ['This', 'is', 'the', '2nd', 'line', '.'],
#  ['This', 'is', 'the', '3rd', 'line', '?'],
#  ['This', 'is', 'the', '4th', 'line'],
#  ['This', 'is', 'the', '5th', 'line', '.']]

要全部小写:

print([x.lower().split() for x in f])

# [['this', 'is', 'the', '1st', 'line', '!'],
#  ['this', 'is', 'the', '2nd', 'line', '.'],
#  ['this', 'is', 'the', '3rd', 'line', '?'],
#  ['this', 'is', 'the', '4th', 'line'],
#  ['this', 'is', 'the', '5th', 'line', '.']]

之所以没有获得第一行,是因为您在外部循环中进行了.readline()却从未使用过它。您立即在循环中用另一个.readline()替换了该行,从第二个位置开始就留下了行。

答案 2 :(得分:0)

这里有几个问题。首先是第一行没有添加到您的joined列表中,因为它已定义,然后在第一次迭代的while循环的开头被覆盖,然后才可以添加到{{1 }}列表。

第二个是你写的:

joined

for x in line: joined.append(line) 中每个元素的整个line添加到joined。因此,第二行被追加了6次,因为它包含6个元素。我认为您打算将line附加到x上,但这将使您在单个列表中获得所有单词,而不是根据需要嵌套列表。由于每行已经被分成单个单词的列表,因此不需要此内循环。

您可以使用以下代码更简洁地编写代码:

joined

结果:

with open("test.txt", "r") as f:
    joined = [line.split() for line in f.readlines()]

答案 3 :(得分:0)

要回答其他问题,如何将所有字符都小写:

str.lower()

为您提供字符串的小写字母。因此,要完成上述答案:

with open('test.txt') as f:
print([x.split().lower() for x in f])

这应该做到。