0

I have a .csv file with data constructed as [datetime, "(data1, data2)"] in rows and I have managed to import the data into python as time and temp, the problem I am facing is how do I seperate the temp string into two new_temp columns in float format to use for plotting later on?

My code so far is:

import csv
import matplotlib.dates as dates

def getColumn(filename, column):
    results = csv.reader(open('logfile.csv'), delimiter = ",")
    return [result[column] for result in results]
time = getColumn("logfile.csv",0)
temp = getColumn("logfile.csv",1)
new_time = dates.datestr2num(time)
new_temp = [???]

When I print temp I get ['(0.0, 0.0)', '(64.4164, 66.2503)', '(63.4768, 65.4108)', '(62.7148, 64.6278)', '(62.0408, 63.9625)', '(61.456, 63.2638)', '(61.0234, 62.837)', '(60.6823, 62.317)',...etc]

If anyone can help me then thanks in advance.

9
  • 1
    Possible duplicate of Parse a tuple from a string? Commented Jul 11, 2016 at 12:49
  • As a side note, you should read the CSV only once and then extract the columns. Currently you read the whole file over and over. Commented Jul 11, 2016 at 12:52
  • I checket that solution and wrote make_tuple(temp) and get the error: ValueError: malformed string. And i also tried the tuple(int(x) for x in temp[1:-1].split(',')) and get the error: AttributeError: 'list' object has no attribute 'split' Commented Jul 11, 2016 at 13:06
  • Is there any problem with my solution? It should work. Commented Jul 11, 2016 at 13:07
  • 1
    Thanks man that seems to have made the magic. Commented Jul 11, 2016 at 13:28

2 Answers 2

1

You may use this code:

import re
string = "['(0.0, 0.0)', '(64.4164, 66.2503)', '(63.4768, 65.4108)', '(62.7148, 64.6278)', '(62.0408, 63.9625)', '(61.456, 63.2638)', '(61.0234, 62.837)', '(60.6823, 62.317)']"
data = re.findall('[+-]?\d+\.\d+e?[+-]?\d*', string)
data = zip(data[0::2], data[1::2])
print [float(d[0]) for d in data]
print [float(d[1]) for d in data]
Sign up to request clarification or add additional context in comments.

7 Comments

This will fail if you have some differently formatted floats (e.g. 5.2456e10). Better use Python's built in parser for evaluating Python expressions in strings as suggested in the comments.
No special format was mentioned but thanks for the suggestion.
Does it work with 5.2456e-10, 5.2456e+9, -5.2456e-10, .1, 1.?
Yes, now it does, but I am sure you will find another case ;-) Deutsche Korrektheit was?
I found that the suggestion of user3387223 was quite important so I improved the solution again to allow negative values.
|
1

Going from the answer in Parse a tuple from a string?

from ast import literal_eval as make_tuple
temp = ['(0.0, 0.0)', '(64.4164, 66.2503)', '(63.4768, 65.4108)', '(62.7148, 64.6278)', '(62.0408, 63.9625)', '(61.456, 63.2638)', '(61.0234, 62.837)', '(60.6823, 62.317)']

tuples = [make_tuple(stringtuple) for stringtuple in temp]

and you have an array of tuples of doubles.

I undeleted the post and made it a full answer, because apparently it wasn't clear enough to reference the other post.

1 Comment

If it has already been answered, post it as a comment, not an answer. E.g. Possible duplicate of stackoverflow.com/questions/9763116/parse-a-tuple-from-a-string

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.