python将其他csv中的某些数据插入新的csv(列作为比较的基础)

2024-06-14 07:15:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从2中插入一行nd.csv文件到1st.csv文件关于它的列。你知道吗

这是我的档案

1个st.csv文件你知道吗

store_name_id   seller_name    date_hired   age
100             jed            2014/0707    33
200             ej             2014/0708    33
200             charm          2014/0709    11
111             teahaa         2014/0710    22
111             luis           2014/0711    12
200             jess           2014/0712    66

二nd.csv文件你知道吗

ID     Store_Name     Store_description           contacts
100    new store      newly build store           1313134
200    young store    the owner is young           1111111
111    pretty store   the owner is pretty cool     1231331

下面是我的python代码:

#!/usr/bin/python

import csv

d = {}

with open("1st.csv", 'rb') as f:
  for row in csv.reader(f, delimiter=','):
    d[row[0]] = row

with open("2nd.csv", 'rb') as f:
  for row in csv.reader(f, delimiter=','):
    old_row = d.setdefault(row[0], [row[0]])
    old_row[4:] = row[1:]

with open("output.csv", "w") as f:
  csv.writer(f, delimiter='\t').writerows(d.values())

但结果是:

有些是这样的(不是真的):

100   jed      2014/0707    33
100   new store newly build store   1313134
200   ej       2014/0708    33
200   young store   the owner is young  1111111
200   charm    2014/0709    11
111   teahaa    2014/0710   22
111   luis     2014/0711    12
111   pretty store  the owner is pretty cool    1231331

他们合并是的,但我的预期和想要的输出必须是这样的

store_name_id   seller_name date_hired  age Store_name  Store_description   contacts
100              jed         2014/0707  33  new store   newly build store   1313134
200               ej         2014/0708  33  young store the owner is young  1111111
200             charm        2014/0709  11  young store the owner is young  1111111
111             teahaa       2014/0710  22  pretty store    the owner is pretty cool    1231331
111             luis         2014/0711  12  pretty store    the owner is pretty cool    1231331
200             jess         2014/0712  66  young store the owner is young  1111111

我还是个新手,正在学习这门语言。请帮忙。你知道吗


像这样的输出

store_name_id   seller_name date_hired  age Store_name  Store_description   contacts
100              jed         2014/0707  33  new store   newly build store   1313134
200               ej         2014/0708  33  young store the owner is young  1111111
111             teahaa       2014/0710  22  pretty store    the owner is pretty cool    1231331

它只得到一个单一的数据,其余的同一个商店名称\u id就不见了。你知道吗


Tags: 文件csvthestorenameidispretty
1条回答
网友
1楼 · 发布于 2024-06-14 07:15:39

我相信你得到的问题是,输出csv是完全无序的。这是因为dictionary没有任何顺序感,所以d.values()的结果在大多数情况下是任意顺序的。你知道吗

因此,不要使用字典,而是使用列表来存储,或者使用字典来指向该行的索引。你知道吗

根据评论,似乎您也在错误的顺序读取文件,您需要先读取b,列出其存储,然后读取a并创建输出列表。你知道吗

示例-

import csv

d = {}
lst = []
with open("b.csv", 'r') as f:
  reader = csv.reader(f, delimiter=',')
  header = next(reader)[1:]
  for i, row in enumerate(reader):
    d[row[0]] = i
    lst.append(row)

newlst = []
with open("a.csv", 'r') as f:
  reader = csv.reader(f, delimiter=',')
  head = next(reader)
  head.extend(header)
  newlst.append(head)
  for row in reader:
    idx = d.get(row[0], -1)
    if idx != -1:
        old_row = lst[idx]
        row.extend(old_row[1:])
    else:
        row = row + ["","","",""]
    newlst.append(row)



with open("output.csv", "w") as f:
  csv.writer(f, delimiter='\t').writerows(newlst)

相关问题 更多 >