【问题标题】:How to split CSV rows then duplicate that row?如何拆分 CSV 行然后复制该行?
【发布时间】:2019-09-10 16:29:09
【问题描述】:

我有一个 CSV 文件,我需要从中生成一个包含新行的新文件。我在 Bash 和 Python 方面有一些经验。

例子:

Source

Country A,Place1;Place2;Place3,Other info
Country B,Place4;Place5;Place6,Other stuff
Country C,Place7;Place8;Place9,Other examples

Target

Place1,Country A,Other info
Place2,Country A,Other info
Place3,Country A,Other info
Place4,Country B,Other stuff
Place5,Country B,Other stuff
Place6,Country B,Other stuff

所以我需要用; 分隔符分割第二列,并根据行中的其余信息创建一个新行。

【问题讨论】:

  • awk 非常适合解决这个问题。花时间处理Awk Tutorial 的相关部分,尤其是split() 函数和for 循环。然后编辑您的 Q 以显示您解决问题的最佳尝试。包括您当前的输出和任何错误消息(确切地说,复制/粘贴)。祝你好运。
  • 感谢您的回复。我在工作中处于通常的情况,昨天有人需要这个,我一直在四处挖掘并使用 awk , sed 等进行测试,但无济于事。

标签: python bash csv


【解决方案1】:

这是一个 Python 3 解决方案。注意 newline='' 的使用 csv read/writer documentation:

import csv

with open('source.csv',newline='') as fin:
    with open('target.csv','w',newline='') as fout:
        r = csv.reader(fin)
        w = csv.writer(fout)

        # Read original three columns
        for country,places,other in r:

            # Write a row for each place
            for place in places.split(';'):
                w.writerow([place,country,other])

如果仍在使用 Python 2,请改用以下开放语法:

with open('source.csv','rb') as fin:
    with open('target.csv','wb') as fout:

【讨论】:

    【解决方案2】:

    假设它总是第二列。 如果 columnNumber 是不同的列,请更改它(为了便于使用,我从 1 开始计算,而不是 0)。

    import csv
    newData = []
    columnNumber = 2
    with open('data.csv') as csvfile:
        line = csv.reader(csvfile, delimiter = ',')
        for row in line:
            cStr = row[columnNumber-1].split(';')
            for i in range(0,len(cStr)):
                temp = []
                for j in range(0, len(row)):
                    if(j==columnNumber-1):
                        temp.append(cStr[i])
                    else:
                        temp.append(row[j])
                newData.append(temp)
    with open('output.csv', 'w', newline="") as outFile:
        writer = csv.writer(outFile)
        writer.writerows(newData)
    

    【讨论】:

      【解决方案3】:

      如果您有 csv 文件,那么最简单的方法是打开 Excel,然后导航到文件>打开并选择“所有文件”并导航到要修改的 csv 文件。当你打开这个文件时,它应该让你选择说明你想用什么字符作为分隔符,你可以输入“;”。

      应该有更多您同意的选项,然后您将拥有一个 xls 文件,其中的字段由“;”分隔。

      为了从这个到你想要的表,我建议创建一个数据透视表。我的回答是基于这是一次性的,而如果您必须重复此功能,最好用 Excel VBA 或 Python 编写一些东西。如果您遇到困难,很乐意提供进一步的建议。

      【讨论】:

        【解决方案4】:

        使用 Miller (https://github.com/johnkerl/miller) 非常简单。使用这个命令

        mlr --nidx --fs "," nest --explode --values --across-records -f 2 then reorder -f 2 input.csv
        

        你有

        Place1,Country A,Other info
        Place2,Country A,Other info
        Place3,Country A,Other info
        Place4,Country B,Other stuff
        Place5,Country B,Other stuff
        Place6,Country B,Other stuff
        Place7,Country C,Other examples
        Place8,Country C,Other examples
        Place9,Country C,Other examples
        

        【讨论】:

          猜你喜欢
          • 2023-04-02
          • 2018-07-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多