gpt4 book ai didi

python - 合并两个具有不同列数和列名称的 csv 文件

转载 作者:行者123 更新时间:2023-11-30 22:28:38 24 4
gpt4 key购买 nike

我有两个具有不同列数的 csv 文件。我想按第一列合并这些 csv 文件(它们具有不同的列名称)。

csv 1:

ID, name, A1, A2, A3
1101,台泥,29.19,2998730.0,0.23
1102,亞泥,36.06,933069.0,0.08
1103,嘉泥,23.29,132555.0,-0.17
1104,環球水泥,25.64,139041.0,0.45
1108,幸福水泥,11.8,80854.0,0.05

csv2:

NO, no_name, D1, D2, D3, D4
01003T,兆豐新光R1,14.08,14.08,13.95,14.00
01004T,土銀富邦R2,13.39,13.40,13.30,13.34
01007T,兆豐國泰R2,14.76,14.76,14.76,14.76
1101,台泥,35.45,35.45,34.80,35.15
1102,亞泥,26.45,26.50,26.30,26.50
1103,嘉泥,8.71,8.71,8.61,8.71

如果csv2 NO = csv1 ID,将D1、D2、D3、D4追加到csv1中,新的csv如下:

ID, name, A1, A2, A3, D1, D2, D3, D4
1101,台泥,29.19,2998730.0,0.23,35.45,35.45,34.80,35.15
1102,亞泥,36.06,933069.0,0.08,26.45,26.50,26.30,26.50
1103,嘉泥,23.29,132555.0,-0.17,8.71,8.71,8.61,8.71

我的想法是:读取两个 csv 文件的行,然后使用双循环找出第一列的相同值,然后将第一个 csv 文件和第二个 csv 文件中的某些列合并,然后创建一个合并 csv 文件,如下是代码:

`import csv

# readlines for the two csv file
inf = open('csv1.csv', "r").readlines()
inf1 = open('csv2.csv', "r").readlines()

data = {}

# use double loop find out the same value of first column
# combine first csv file and certain columns in second csv file
for line in inf:
part = line.split(',')
a = part[0]
for line1 in inf1:
part1 = line1.split(',')
b = part1[0]
if a == b in data:
row = line + ',' + part1[2] + ',' + part1[3] + ',' + part1[4] + ',' + part1[5]

# create a merge csv file
with open('m_data.csv', 'w', newline = '') as f_out:
fieldnames = ['ID', 'name', 'A1', 'A2', 'A3', 'D1', 'D2', 'D3', 'D4']
writer = csv.DictWriter(f_out, fieldnames = fieldnames)

writer.writeheader()

for c1, c2, c3, c4, c5, c6, c7, c8, c9 in data.items():
writer.writerow({'ID': c1, 'name': c2, 'A1': c3, 'A2': c4, 'A3': c5,'D1': c6, 'D2': c7, 'D3': c8, 'D4':c9})

f_out.close()`

我认为问题可能出在“合并第一个 csv 文件和第二个 csv 文件中的某些列”和“创建合并 csv 文件”之间,但我不知道如何修复它,或者还有其他方法?

非常感谢您的帮助!

最佳答案

此解决方案应该按预期工作:

import csv
from collections import OrderedDict


data = OrderedDict()

with open('temp.csv', 'r', encoding='utf-8', newline='') as infile1:
reader1 = csv.reader(infile1)
next(reader1, None) # Skip header.
for row in reader1:
data[row[0]] = row # id as the key and row as the value.

file2_NOs = set() # A set to store the NO fields of file2.

with open('temp2.csv', 'r', encoding='utf-8', newline='') as infile2:
reader2 = csv.reader(infile2)
next(reader2, None) # Skip header.
for row in reader2:
file2_NOs.add(row[0]) # Add the NO fields to this set.
if row[0] in data: # If the NO/id exists in file1...
data[row[0]].extend(row[2:]) # ... extend the rows.

with open('temp3.csv', 'w', encoding='utf-8', newline='') as outfile:
writer = csv.writer(outfile)
writer.writerow(['ID', 'name', 'A1', 'A2', 'A3', 'D1', 'D2', 'D3', 'D4'])
for row in data.values():
if row[0] in file2_NOs: # Write only if the number/id exists in file 2.
writer.writerow(row)

我首先将 file1 的行添加到 OrderedDict,其中 ids 作为键,行作为值。然后我迭代第二个文件,如果 NO 作为字典中的键存在,我会扩展该行。我还添加了一个集合 (file2_NOs),其中添加了 NO,这样我就可以检查 id 是否也是有效的 NO,然后只写入行(如果id 和 NO 都存在于两个文件中。

关于python - 合并两个具有不同列数和列名称的 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46579010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com