【发布时间】:2016-11-13 09:59:54
【问题描述】:
我有一个 .csv 文件,其中的数据按行构造为 [datetime, "(data1, data2)"],我已设法将数据作为 time 和 temp 导入 python,我面临的问题是如何分隔 @987654325将 @ 字符串转换为浮点格式的两个 new_temp 列,以便稍后用于绘图?
到目前为止我的代码是:
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 = [???]
当我print 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)',...etc]
如果有人可以帮助我,请提前感谢。
【问题讨论】:
-
附带说明,您应该只阅读 CSV 一次,然后提取列。目前,您一遍又一遍地阅读整个文件。
-
我检查了该解决方案并编写了 make_tuple(temp) 并得到错误:ValueError:格式错误的字符串。而且我还尝试了 tuple(int(x) for x in temp[1:-1].split(',')) 并得到错误: AttributeError: 'list' object has no attribute 'split'
-
我的解决方案有问题吗?它应该可以工作。
-
感谢这位似乎创造了魔法的人。
标签: python string python-2.7 floating-point