网站制作学习网Python→正文:python读写csv
字体:

python读写csv

Python 2023/7/20 14:34:27  点击:不统计

http://www.forasp.cn/
 python读写csv,直接看下面代码,复制后可以直接保存到本地.py文件直接运行。

# coding = utf-8
"""
@ author: www.forsp.cn
@ info: 读取 csv
"""
import os
import csv # 如果没有安装 则运行 pip install csv


class Main:
def __init__(self):
self.csv_path = os.getcwd()

def run(self):
data = [['title-date','title-num'],
['2023-07-01','1 www'],
['2023-07-01','2 forasp'],
['2023-07-01','3 cn'],
]
file_name = "test.csv"
self.write_csv_file(data, file_name)
read_data = self.read_csv_file(file_name)
print(read_data)

pass

# 读取 csv
def read_csv_file(self, file_name, encoding = "GBK"):
result = []
print("开始处理:",self.csv_path + "/" + file_name)
f = open(self.csv_path + "/" + file_name, "r", encoding=encoding)
data = csv.reader(f, delimiter=",")
for row in data:
if(len(row)>1):# 每个
result.append(row)
return result

# 写csv文件
def write_csv_file(self, data, file_name, encoding = "GBK"):
f = open(self.csv_path + "/" + file_name, "w", encoding=encoding)
writer = csv.writer(f)
for row in data:
writer.writerow(row)
pass


if __name__ == "__main__":
obj = Main()
obj.run()

网站http://www.制forasp作.cn

·上一篇:python 替换/清除emoji表情 >>    ·下一篇:python读写excel >>
推荐文章
最新文章