美文网首页
python 读写csv文件

python 读写csv文件

作者: 坚持未来 | 来源:发表于2019-03-19 23:35 被阅读0次

问什么要使用csv格式,文件默认是,逗号分割的,文件较小,可以用文本编辑器直接打开,或用Excel表格直接分析数据。

下面是简单的代码实现逻辑。结合android的adb命令,执行命令,读取执行结果,写入csv文件,画图分析。

写csv 文件

import csv

import os

alldata = [("timestamp", "cpustatus"),("1",12)]

path = os.path.dirname(__file__)

pathfile = os.path.join(path,"csvfile.csv")

#newline 不指定newline='',则每写入一行将有一空行被写入

with open(path,'w',newline='') as fd:

    writer = csv.writer(fd)

    for row in alldata:

        writer.writerow(row)

读csv文件

import csv

import os

path = os.path.dirname(__file__)

pathfile = os.path.join(path,"csvfile.csv")

print(path)

with open(pathfile,'r') as fd:

    reader = csv.reader(fd)

# print(list(reader))

    for row in reader:

          print(row)

相关文章

网友评论

      本文标题:python 读写csv文件

      本文链接:https://www.haomeiwen.com/subject/oezomqtx.html