同学有大量csv文件,要我帮忙提取每个csv中‘type’字段为‘O3’的整一行数据
于是,上代码!
import csv
import os
import pandas as pd
import json
# 设置显示的最大列、宽等参数,消掉打印不完全中间的省略号
# pd.set_option('display.max_columns', 1000)
pd.set_option('display.width', 1000)
# pd.set_option('display.max_colwidth', 1000)
# pd.set_option('display.height', 1000)
# 显示所有列
pd.set_option('display.max_columns', None)
# 显示所有行
pd.set_option('display.max_rows', None)
dict = []
file_path = 'file' # 设置文件路径
# 建立空list
code_list = []
for root, dirs, files in os.walk(file_path):
# print(root)
# print(dirs)
# print(files)
for filename in files:
code_list.append(filename)
list_number = len(code_list)
index = 0
for k in code_list:
if index < int(list_number):
file_name = 'file/' + code_list[index]
index += 1
with open(file_name, 'r', encoding="utf-8") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if row['type'] == 'O3':
# j = json.dumps(row, ensure_ascii=False)
# print(j)
# with open('test_data.json', 'a') as json_file:
# json_file.write(j)
dict.append(row)
print(row)
pd.DataFrame(dict).to_csv('2021.csv')
利用pandas的DataFrame.to_csv方法,记得用的输入参数是字典格式而并非json
|