我只是想从文件中逐行读取并将每行附加到数组中。我以前做过没有任何问题,我不知道为什么这次它不起作用!打印功能只显示文件的第一行!! 这是我的代码:
keyword_array = []
with open('local directory\\C0577785c.txt') as my_keywordfile:
for keyword in my_keywordfile.readline().strip("[]").strip("'").split(","):
keyword_array.append( keyword.strip().strip("'").lower())
print(keyword_array)
此外,这是文件内容的子集:
C0001396 adam attack stokes
C0001396 Adam Stokes Attacks
C0001396 Adam-Stokes Attacks
C0001396 adam-stokes syndrome
C0001396 adams attack stoke
C0001396 adams stoke syndrome
C0001396 adams stokes attack
C0001396 ADAMS STOKES SYNDROME
C0001396 Adams-Stokes
C0001396 Adams-Stokes Syndrome
C0001396 Adams-Stokes; attack
C0001396 attack; Adams-Stokes
C0001396 attack; Stokes-Adams
C0001396 Attacks, Adam-Stokes
C0001396 Attacks, Stokes-Adams
C0001396 morgagni's disease
C0001396 Morgagni-Adam's Stokes syndrome
C0001396 Morgagni-Stokes-Adams
谢谢,
答案 0 :(得分:1)
使用嵌套循环读取my_keywordfile的行。
nodeSize
答案 1 :(得分:0)
您只调用一次readline(),因此只打印一行。
答案 2 :(得分:0)
在for循环中,您希望在文件对象上调用.readlines()。不是readline()。 readline()只返回第一行,readlines()返回一个可以迭代的行列表。
来自文档:
readlines(...) method of _io.TextIOWrapper instance
Return a list of lines from the stream.
hint can be specified to control the number of lines read: no more
lines will be read if the total size (in bytes/characters) of all
lines so far exceeds hint.
我还建议不要在for循环声明中清理行。如果你在for循环中这样做,它认为它看起来更容易。
# ....
with open('myfile.txt', 'r') as my_file:
for raw_line in my_file.readlines():
cleaned_data = raw_line.lower().strip("'")
#....