我有一个最初为python 3.5+编写的脚本。我需要把它转换成2.6.2。
在我使用的脚本中
readlines()
我相信这两个版本的表现是不同的。
特别是我用的
读入()
从用换行符分隔的TXT文件中检索数据。
这是一个笑话:
t=open(supportID+"stmp.txt","r")
timeIn=(t.readlines())
a=(str(timeIn))
supportID=(t.readlines())
b=(str(supportID))
branch=(t.readlines())
c=(str(branch))
clientID=(t.readlines())
d=(str(clientID))
problem=(t.readlines())
e=(str(problem))
solution=(t.readlines())
f=(str(solution))
timeOut=(t.readlines())
g=(str(timeOut))
在我的3.x脚本中,每个
读入()
它可以根据需要执行,但是使用2.x这不起作用。我试着输入1-7的值,如上图所示为空白。
通过一些研究,我发现一些2.x用户使用
with open(filename)
这是首选的方法,还是有办法改变我的原件,使其工作?
编辑:
所以我要用这个格式
with open(supportID+"stmp.txt") as t:
for line in t:
print(line)
我插上这个,它就工作了,把每一行打印成一行。现在我想用这个把每一行赋给一个变量。
编辑2:
这目前对我的环境有效,但不是此功能的最佳实践。读取每一行并将每一行赋给一个变量。
t=open(supportID+"stmp.txt","r")
lines = t.readlines()
t.close()
a=lines[0]
b=lines[1]
c=lines[2]
...