【发布时间】:2017-03-19 12:07:54
【问题描述】:
如何从文件中的一行中只取出一个单词并将其保存在某个字符串变量中? 例如,我的文件有“this, line, is, super”行,我只想在变量 word 中保存第一个单词(“this”)。我试图逐个字符地阅读它,直到我打开“”,但是当我检查它时,我收到一个错误“'int'类型的参数不可迭代”。我该怎么做?
line = file.readline() # reading "this, line, is, super"
if "," in len(line): # checking, if it contains ','
for i in line:
if "," not in line[i]: # while character is not ',' -> this is where I get error
word += line[i] # add it to my string
【问题讨论】:
标签: string python-2.7 file