如何输出文件python中的每一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4709655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to output every line in a file python
提问by SourD
if data.find('!masters') != -1:
f = open('masters.txt')
lines = f.readline()
for line in lines:
print lines
sck.send('PRIVMSG ' + chan + " " + str(lines) + '\r\n')
f.close()
masters.txt has a list of nicknames, how can I print every line from the file at once?. The code I have only prints the first nickname. Your help will be appreciate it. Thanks.
masters.txt 有一个昵称列表,如何一次打印文件中的每一行?。我的代码只打印第一个昵称。您的帮助将不胜感激。谢谢。
采纳答案by mgiuca
Firstly, as @l33tnerd said, f.closeshould be outside the for loop.
首先,正如@l33tnerd 所说,f.close应该在 for 循环之外。
Secondly, you are only calling readlineonce, before the loop. That only reads the first line. The trick is that in Python, files act as iterators, so you can iterate over the file without having to call any methods on it, and that will give you one line per iteration:
其次,您只readline在循环之前调用一次。那只读取第一行。诀窍在于,在 Python 中,文件充当迭代器,因此您可以迭代文件而无需对其调用任何方法,并且每次迭代都会给您一行:
if data.find('!masters') != -1:
f = open('masters.txt')
for line in f:
print line,
sck.send('PRIVMSG ' + chan + " " + line)
f.close()
Finally, you were referring to the variable linesinside the loop; I assume you meant to refer to line.
最后,您指的lines是循环内的变量;我假设你的意思是参考line.
Edit: Oh and you need to indent the contents of the ifstatement.
编辑:哦,你需要缩进if语句的内容。
回答by NG.
You probably want something like:
你可能想要这样的东西:
if data.find('!masters') != -1:
f = open('masters.txt')
lines = f.read().splitlines()
f.close()
for line in lines:
print line
sck.send('PRIVMSG ' + chan + " " + str(line) + '\r\n')
Don't close it every iteration of the loop and print line instead of lines. Also use readlines to get all the lines.
不要在循环的每次迭代中都关闭它并打印行而不是行。还可以使用 readlines 获取所有行。
EDITremoved my other answer - the other one in this discussion is a better alternative than what I had, so there's no reason to copy it.
编辑删除了我的另一个答案——这个讨论中的另一个答案是比我更好的选择,所以没有理由复制它。
Also stripped off the \n with read().splitlines()
还用 read().splitlines() 去掉了 \n
回答by Serdar Dalgic
Did you try
你试过了吗
for line in open("masters", "r").readlines(): print line
?
?
readline()
only reads "a line", on the other hand
另一方面,只读取“一行”
readlines()
reads whole lines and gives you a list of all lines.
读取整行并为您提供所有行的列表。
回答by seggy
You could try this. It doesn't read all of f into memory at once (using the file object's iterator) and it closes the file when the code leaves the with block.
你可以试试这个。它不会立即将所有 f 读入内存(使用文件对象的迭代器),并且会在代码离开 with 块时关闭文件。
if data.find('!masters') != -1:
with open('masters.txt', 'r') as f:
for line in f:
print line
sck.send('PRIVMSG ' + chan + " " + line + '\r\n')
If you're using an older version of python (pre 2.6) you'll have to have
如果您使用的是旧版本的 python(2.6 之前),则必须拥有
from __future__ import with_statement
回答by user2154354
Loop through the file.
循环遍历文件。
f = open("masters.txt")
lines = f.readlines()
for line in lines:
print line

