对于下面的代码,我总是收到错误ValueError: need more than 1 value to unpack
。它是从txt file
读取的,有5个不同的内容,所以肯定它有超过1行。
with open("People.txt", 'r') as inFile:
for line in inFile:
ip,user,password = line.strip().split(',')
文本文件如下所示:
ip,username,password
答案 0 :(得分:0)
@parchment在位于here的评论中回答。
然后空行是问题。您应该添加空白支票 线。 - 羊皮纸
示例有用的食谱:
In [17]: with open("src.txt") as f:
...: for f_ in f:
...: if f_.strip(): # One quick way to check for blank lines.
...: one_,two_ = f_.strip().split(",")
...: print one_, two_
foo bar
spam grok
In [18]:
答案 1 :(得分:-3)
您可以在文件末尾添加换行符。你可以做得更强大,例如那样。
with open("People.txt", 'r') as inFile:
for line in inFile:
if line.count(',') == 2:
ip,user,password = line.strip().split(',')