[英]PYTHON: Read from csv sheet
i would like to read a csv sheet.我想阅读 csv 表。 So far i have a loop which reads all rows from sheet and creates a specific txt file for each row.
到目前为止,我有一个循环,它从工作表中读取所有行并为每一行创建一个特定的 txt 文件。
with openCSV(file) as newData:
reader = csv.reader(newData)
next(reader)
dictData = {}
for i, row in enumerate(reader, 1)
dictData = {'articleID' = row[0], 'Desc':row[1], ....}
thats working fine.那工作正常。
Now what i want is:现在我想要的是:
If there are more articleID's with the same numbers it should be added in one txt file.如果有多个相同编号的 articleID,则应将其添加到一个 txt 文件中。
How can i do this?我怎样才能做到这一点?
My CSV File:我的 CSV 文件:
I think pandas
would be a perfect fit for this job我认为
pandas
非常适合这份工作
Install pip3 install pandas
安装
pip3 install pandas
Usage用法
import pandas as pd
df = pd.read_csv("data.csv")
df[df.duplicated(['ID'], keep=False)] #gives you all the duplicates
keep
: {'first', 'last', False}, default 'first' keep
:{'first', 'last', False},默认 'first'
Below.以下。 The code creates 3 csv files.
该代码创建 3 个 csv 文件。 One for each id.
每个 id 一个。
import csv
from collections import defaultdict
data_by_id = defaultdict(list)
with open('c:\\temp\\temp.csv')as f:
reader = csv.reader(f)
next(reader)
for row in reader:
data_by_id[row[0]].append(row[1:])
for _id, data in data_by_id.items():
with open('c:\\temp\\{}.csv'.format(_id), 'w') as f:
f.write('id,description,price\n')
for line in data:
line.insert(0, _id)
f.write(','.join(line) + '\n')
temp.csv温度csv
id,description,price
12,desc1,12.4
13,desc13,13.4
12,desc132,312.4
13,desc1er,5.8
11,desc1wewe,77.9
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.