1

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

8
  • Is the value same for all rows ? I see the file you have now has just 1 value. value, timestampvalue,timestampvalue,timestampvalue..... Commented Jun 22, 2016 at 19:32
  • No, there are different values from -1.0000000 to 1.0000000. So float values between one and minus one Commented Jun 22, 2016 at 19:34
  • In that case, it looks like you lost the data, not just the format. If that's the case, we cannot get the data. Can you share a sample of what you have now ? Commented Jun 22, 2016 at 19:36
  • So basically you have a single line? Commented Jun 22, 2016 at 19:37
  • @PadraicCunningham yes Commented Jun 22, 2016 at 19:38

4 Answers 4

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

for (line, value, timestamp) in re.findall("(([^,]+),(.{13}))", input):
    print value, timestamp
Sign up to request clarification or add additional context in comments.

Comments

1

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, 

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.