【问题标题】:I want to merge the rows for a particular values in csv file我想合并 csv 文件中特定值的行
【发布时间】:2018-12-22 20:09:02
【问题描述】:

我有一个结构类似的 csv 文件。我想要实现的是合并颜色。就像产品代码 1001 有不同的颜色,即 BLACK CREAM GRAPHITE,我想要 1001 的一行和一个单元格中的所有颜色“;” (分号)分隔。我想对所有产品都这样做。

编辑

要求的输出:

1001-BLACK-P-OS ,BLACK;CREAM;Graphite

1002-BLACK-P-OS ,黑色;奶油

给定 CSV

1001-黑色-P-OS,黑色

1001-CREAM-P-OS , 奶油

1001-GRAPH-P-OS , 石墨

1002-黑色-P-OS ,黑色

1002-CREAM-P-OS ,CREAM

我正在尝试使用 python,但无法做到。

with open('ascolor.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        serial=row[0]
        d=''
        for r in readCSV:
            if serial is r[0]:
                d=d+r[1]
                d=d+';'

【问题讨论】:

  • 任何人请评论拒绝投票的原因
  • 您确定要将 3 种颜色存储在键 1001-BLACK-P-OS 下吗? (不是 dv)-black 已经是密钥的一部分了
  • 是的,因为我希望每个代码都有一个条目,即 1001 应该有一行,当我使用它时,我将删除四位数代码后的部分。
  • 那为什么不存储为1001,BLACK;CREAM;GRAPHITE呢?
  • 是的,但关键是如何合并到类似的东西

标签: python-3.x csv merge


【解决方案1】:

创建您的数据文件:

data = """1001-BLACK-P-OS , BLACK

1001-CREAM-P-OS , CREAM

1001-GRAPH-P-OS , GRAPHITE

1002-BLACK-P-OS ,BLACK

1002-CREAM-P-OS ,CREAM"""

fn = 'ascolor.csv'

with open(fn, "w") as f:
    f.write(data)

我们可以开始重新格式化它:

fn = 'ascolor.csv'
import csv    
data = {}
with open(fn) as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        if row:  # weed out any empty rows - they would cause index errors
            num = row[0].split("-")[0]   # use only the number as key into our dict
            d = data.setdefault(num,[row[0].strip()])  # create the default entry with num as key
                                               # and the old "1001-BLACK-P-OS text as first entry
            if len(d) == 1: # first time we add smth
                d.append([row[1].strip()])     # now add the first color into an inner list
            else:  # this is the second/third color for this key, append to inner list
                d[1].append(row[1].strip()) # this is kindof inefficient string concat

# after that youve got a dictionary of your data:

# print(data)
# {'1001': ['1001-BLACK-P-OS', ['BLACK', 'CREAM', 'GRAPHITE']], 
#  '1002': ['1002-BLACK-P-OS', ['BLACK', 'CREAM']]}


# when writing csv with module, always open file with newline = ""
# else you get silly empty lines inside your file. module csv will do
# all newlines needed. See example at
#    https://docs.python.org/3/library/csv.html#csv.writer
with open("done.csv","w",newline="") as f:
    writer = csv.writer(f,delimiter=",")
    for k in sorted(data.keys()):
        # this will add the 1001-BLACK-P-OS before it - I dont like that
        # writer.writerow([data[k][0],';'.join(data[k][1])]) 

        # I like this better - its just 1001 and then the colors 
        writer.writerow([k,';'.join(data[k][1])]) 

print("")
with open("done.csv","r") as f:
    print(f.read())

输出:

1001,BLACK;CREAM;GRAPHITE
1002,BLACK;CREAM

或使用注释行:

1001-BLACK-P-OS,BLACK;CREAM;GRAPHITE
1002-BLACK-P-OS,BLACK;CREAM

HTH

【讨论】:

  • 太棒了,简直完美!我会为你祈祷帮助我。每个人都投了反对票。
  • 谢谢。也尝试了解我做了什么,以便您下次可以尝试自己解决。
  • 是的,你们真的帮了我,你们真棒
猜你喜欢
  • 1970-01-01
  • 2021-09-23
  • 2015-02-02
  • 1970-01-01
  • 2011-03-22
  • 2019-02-08
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
相关资源
最近更新 更多