python 读写csv文件

问什么要使用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)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容