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



我没有太多使用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 使用率等。如何读取这些列并将每列存储到不同的数组中?

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)

希望这有帮助。

这是我阅读表格的首选方式。它最终只有一个返回值,并且写入时间很短。

#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]

相关内容

  • 没有找到相关文章

最新更新