Using .txt into a list kinda

hi guys just needing so help with reading .txt into a list I think what I've got so far is

r = open('RegsInList.txt','r')

rr =(r.readlines())
#print(rr)

this works it imports the text file and reads it I need it to read into a list. So I can put it under list obj and then it to split it up

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)

my .txt looks like,

'AV46 WAD','WD40 ASD','BG65 KYS','WA48 DFT','GH09 DEG'

thanks for the help if you can give

edited:

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)

gives me error

Traceback (most recent call last): File "/Users/GameDevelopment/Desktop/Desktop/Python/AvgSpeed/Times/Time1Split.py", line 2, in time1 =(time1list.readlines()) File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py", line 26, in decode return codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)


你可以将这个.txt文件读入列表中并像下面这样打印出来:

with open("test.txt", "r") as infile:
    content = [i.strip("'") for i in infile.read().split(",")]
for i in content:
    print(i)

Here are the steps to achieve your objective:

  • Read the file
  • Split item in each line by ','
  • Add each item to a list
  • Sample code:

        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/54774.html

    上一篇: 用argparse调用函数

    下一篇: 将.txt用于列表中