我需要读取一个文件并将其拆分为Python中的不同列

时间:2014-04-08 21:07:09

标签: python arrays file timestamp

我没有太多使用Python的经验,而我正在尝试阅读日志文件。到目前为止,我能够读入文件并将其打印出来。

infile = open("C:\file.log", "r")
lines = infile.readlines()
print lines
print len(lines)
infile.close()

该文件包含双精度值和时间戳,例如:

08 Apr 2014 00:00:31, 22.198, 5.444, 67.901
08 Apr 2014 00:00:31, 33.198, 6.444, 54.901
08 Apr 2014 00:00:31, 44.198, 5.444, 84.901
08 Apr 2014 00:00:31, 55.198, 8.444, 94.901

这些值中的每一个都是不同的测量值,即。第一个数字是时间戳,第二个是CPU使用率等。如何读取这些数据并将每个列存储到不同的数组中?

2 个答案:

答案 0 :(得分:2)

r = csv.reader(open(filename))
dates = []
times = []
cpus = []
usages = []
for d, t, v1, v2 in r:
    dates.append(d)
    times.append(t)
    cpus.append(float(v1)
    usages.append(v2)

希望这有帮助。

答案 1 :(得分:0)

这是我阅读表格的首选方式。它以一个返回值结束,并且写得很短。

#create a list of keys for each data entry
label_list = ["timestamp", "cpu_usage", "foo", "bar"]
# make a container to hold the entries
data = []
# using with/as is a safer way to open files
with open("C:\file.log", "r") as file:
    for line in file:
        # split the element
        value_list = line.split(", ")
        # create a dictionary for each entry, with label_list as keys
        data.append({})
        for value, label in zip(value_list , label_list):
            data[-1][label] = value
# whenever you want a group of items you can do this. ex) cpu_usage
print [d["cpu_usage"] for d in data]