Python read .txt File -> list

ぃ、小莉子 提交于 2021-01-28 14:38:03

问题


I have a .txt File and I want to get the values in a list. The format of the txt file should be:

value0,timestamp0
value1,timestamp1
...
...
...

In the end I want to get a list with

[[value0,timestamp0],[value1,timestamp1],.....]

I know it's easy to get these values by

direction = []
for line in open(filename):
    direction,t = line.strip().split(',')
    direction = float(direction)
    t = long(t)
    direction.append([direction,t])
return  direction

But I have a big problem: When creating the data I forgot to insert a "\n" in each row.

Thats why I have this format:

value0, timestamp0value1,timestamp1value2,timestamp2value3.....

Every timestamp has exactly 13 characters.

Is there a way to get these data in a list as I want it? Would be very much work get the data again.

Thanks Max


回答1:


I coded a quickie using your example, and not using 13 but len("timestamp") so you can adapt

instr = "value,timestampvalue2,timestampvalue3,timestampvalue4,timestamp"

previous_i = 0
for i,c in enumerate(instr):
    if c==",":
        next_i = i+len("timestamp")+1
        print(instr[previous_i:next_i])
        previous_i = next_i

output is descrambled:

value,timestamp
value2,timestamp
value3,timestamp
value4,timestamp



回答2:


import re
input = "value0,0123456789012value1,0123456789012value2,0123456789012value3"

for (line, value, timestamp) in re.findall("(([^,]+),(.{13}))", input):
    print value, timestamp



回答3:


You will have to strip the last , but you can insert a comma after every 13 chars following a comma:

import re
s = "-0.1351197,1466615025472-0.25672746,1466615025501-0.3661744,1466615025531-0.4646‌​7665,1466615025561-0.5533287,1466615025591-0.63311553,1466615025621-0.7049236,146‌​6615025652-0.7695509,1466615025681-1.7158673,1466615025711-1.6896278,146661502574‌​1-1.65375,1466615025772-1.6092329,1466615025801"

print(re.sub("(?<=,)(.{13})",r"\1"+",", s))

Which will give you:

-0.1351197,1466615025472,-0.25672746,1466615025501,-0.3661744,1466615025531,-0.4646‌​7665,1466615025561,-0.5533287,1466615025591,-0.63311553,1466615025621,-0.7049236,146‌​6615025652-0.7695509,1466615025681,-1.7158673,1466615025711,-1.6896278,146661502574‌​1-1.65375,1466615025772,-1.6092329,1466615025801, 



回答4:


I think you could do something like this:

direction = []
for line in open(filename):
    list = line.split(',')
    v = list[0]
    for s in list[1:]:
        t = s[:13]
        direction.append([float(v), long(t)])
        v = s[13:]

If you're using python 3.X, then the long function no longer exists -- use int.



来源:https://stackoverflow.com/questions/37976716/python-read-txt-file-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!