将.txt用于列表中
嗨,大家只是需要如此帮助阅读.txt到列表我认为我到目前为止是
r = open('RegsInList.txt','r')
rr =(r.readlines())
#print(rr)
这工作它导入文本文件并阅读它我需要它读入一个列表。 所以我可以把它放在列表obj下,然后分解它
listobj = ['AV46 WAD','WD40 ASD','BG65 KYS','WA48 DFT','GH09 DEG']
var1, var2, var3,var4 ,var5 = listobj
print(var1)
print(var2)
print(var3)
print(var4)
print(var5)
我的.txt看起来像,
'AV46 WAD','WD40 ASD','BG65 KYS','WA48 DFT','GH09 DEG'
感谢你的帮助,如果你可以给
编辑:
time1list = open('time1.txt','r')
time1 =(time1list.readlines())
#print(regs)
listobj = time1[0].replace("'", '').split(',')
time11, time12, time13,time14 ,time15 = listobj
print(time11)
print(time12)
print(time13)
print(time14)
print(time15)
给我错误
Traceback(最近一次调用最后):文件“/Users/GameDevelopment/Desktop/Desktop/Python/AvgSpeed/Times/Time1Split.py”,第2行,在time1 =(time1list.readlines())文件“/ Library / Frameworks / Python.framework / Versions / 3.5 / lib / python3.5 / encodings / ascii.py“,第26行解码返回codecs.ascii_decode(input,self.errors)[0] UnicodeDecodeError:'ascii'编解码器无法解码位0的字节0xe2:序号不在范围内(128)
你可以将这个.txt文件读入列表中并像下面这样打印出来:
with open("test.txt", "r") as infile:
content = [i.strip("'") for i in infile.read().split(",")]
for i in content:
print(i)
以下是实现您的目标的步骤:
示例代码:
with open("input.txt","r") as fp:
lines = fp.readline()
for line in lines:
listobj = line.split(",")
for item in listobj:
print(item)
链接地址: http://www.djcxy.com/p/54773.html