【问题标题】:Splitting one csv to multiple using rows unique value in python使用python中的行唯一值将一个csv拆分为多个
【发布时间】:2019-04-08 03:44:22
【问题描述】:

以下是我目前所拥有的问题和文件。

这是我目前所拥有的:

filename,file_size,file_attributes,region_count,region_id,region_shape_attributes,region_attributes
AN7_1Cropped.jpg,2544654,{},215,0,"{""name"":""circle"",""cx"":17,""cy"":69,""r"":102}",{}
AN7_1Cropped.jpg,2544654,{},215,1,"{""name"":""circle"",""cx"":214,""cy"":76,""r"":96}",{}
AN7_1Cropped.jpg,2544654,{},215,2,"{""name"":""circle"",""cx"":400,""cy"":48,""r"":86}",{}
AN7_1Cropped.jpg,2544654,{},215,3,"{""name"":""circle"",""cx"":593,""cy"":58,""r"":99}",{}
AN7_1Cropped.jpg,2544654,{},215,4,"{""name"":""circle"",""cx"":777,""cy"":-15,""r"":93}",{}
AN7_2Cropped.jpg,2544654,{},215,0,"{""name"":""circle"",""cx"":17,""cy"":69,""r"":102}",{}
AN7_2Cropped.jpg,2544654,{},215,1,"{""name"":""circle"",""cx"":214,""cy"":76,""r"":96}",{}
AN7_2Cropped.jpg,2544654,{},215,2,"{""name"":""circle"",""cx"":400,""cy"":48,""r"":86}",{}
AN7_2Cropped.jpg,2544654,{},215,3,"{""name"":""circle"",""cx"":593,""cy"":58,""r"":99}",{}
AN7_2Cropped.jpg,2544654,{},215,4,"{""name"":""circle"",""cx"":777,""cy"":-15,""r"":93}",{}

我想将数据拆分为多个 CSV,如下所示: 应该是这样的:

filename,file_size,file_attributes,region_count,region_id,region_shape_attributes,region_attributes
AN7_1Cropped.jpg,2544654,{},215,0,"{""name"":""circle"",""cx"":17,""cy"":69,""r"":102}",{}
AN7_1Cropped.jpg,2544654,{},215,1,"{""name"":""circle"",""cx"":214,""cy"":76,""r"":96}",{}
AN7_1Cropped.jpg,2544654,{},215,2,"{""name"":""circle"",""cx"":400,""cy"":48,""r"":86}",{}
AN7_1Cropped.jpg,2544654,{},215,3,"{""name"":""circle"",""cx"":593,""cy"":58,""r"":99}",{}
AN7_1Cropped.jpg,2544654,{},215,4,"{""name"":""circle"",""cx"":777,""cy"":-15,""r"":93}",{}


filename,file_size,file_attributes,region_count,region_id,region_shape_attributes,region_attributes
AN7_2Cropped.jpg,2544654,{},215,0,"{""name"":""circle"",""cx"":17,""cy"":69,""r"":102}",{}
AN7_2Cropped.jpg,2544654,{},215,1,"{""name"":""circle"",""cx"":214,""cy"":76,""r"":96}",{}
AN7_2Cropped.jpg,2544654,{},215,2,"{""name"":""circle"",""cx"":400,""cy"":48,""r"":86}",{}
AN7_2Cropped.jpg,2544654,{},215,3,"{""name"":""circle"",""cx"":593,""cy"":58,""r"":99}",{}
AN7_2Cropped.jpg,2544654,{},215,4,"{""name"":""circle"",""cx"":777,""cy"":-15,""r"":93}",{}

filename,file_size,file_attributes,region_count,region_id,region_shape_attributes,region_attributes
AN7_3Cropped.jpg,2544654,{},215,0,"{""name"":""circle"",""cx"":17,""cy"":69,""r"":102}",{}
AN7_3Cropped.jpg,2544654,{},215,1,"{""name"":""circle"",""cx"":214,""cy"":76,""r"":96}",{}
AN7_3Cropped.jpg,2544654,{},215,2,"{""name"":""circle"",""cx"":400,""cy"":48,""r"":86}",{}
AN7_3Cropped.jpg,2544654,{},215,3,"{""name"":""circle"",""cx"":593,""cy"":58,""r"":99}",{}
AN7_3Cropped.jpg,2544654,{},215,4,"{""name"":""circle"",""cx"":777,""cy"":-15,""r"":93}",{}
class SeprateCsvFiles:
      def separateCSV(self,iPath,oPath):

        for key, rows in groupby(csv.reader(open(iPath, 'r')),
                                 lambda row: row[0]):
            with open(oPath+"%s.csv" % key, "w") as output:
                for row in rows:
                    output.write(",".join(row) + "\n")

我得到的输出如下:

filename,file_size,file_attributes,region_count,region_id,region_shape_attributes,region_attributes
AN7_1Cropped.jpg,2321174,{},196,0,{"name":"circle","cx":1001,"cy":258,"r":96},{}
AN7_1Cropped.jpg,2321174,{},196,1,{"name":"circle","cx":804,"cy":331,"r":90},{}
AN7_1Cropped.jpg,2321174,{},196,2,{"name":"circle","cx":955,"cy":448,"r":100},{}
AN7_1Cropped.jpg,2321174,{},196,3,{"name":"circle","cx":620,"cy":400,"r":103},{}
AN7_1Cropped.jpg,2321174,{},196,4,{"name":"circle","cx":451,"cy":509,"r":99},{}
AN7_1Cropped.jpg,2321174,{},196,5,{"name":"circle","cx":649,"cy":584,"r":84},{}

"{""name"":""circle"",""cx"":17,""cy"":69,""r"":102}"

上面的格式在下面改变了。 " 不见了

{"name":"circle","cx":1001,"cy":258,"r":96}

【问题讨论】:

    标签: python-3.x pandas export-to-csv pandas-groupby


    【解决方案1】:

    在进行分组之前阅读标题,然后将标题添加到每个生成的 csv 对象中。

    【讨论】:

    • 感谢您的建议。有效。遇到了一个新问题。
    猜你喜欢
    • 2015-05-29
    • 1970-01-01
    • 2016-07-26
    • 2020-06-11
    • 2021-06-05
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多