使用python中的行唯一值将一个csv拆分为多个

时间:2019-04-08 03:44:22

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

以下是我目前对问题和文件的了解。

这是我到目前为止所拥有的:

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},{}
  

“ {”“名称”“:”“圆”“,”“ cx”“:17,”“ cy”“:69,”“ r”“:102}”

以上格式在下面进行了更改。 “丢失

  

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

1 个答案:

答案 0 :(得分:0)

在进行分组依据之前先阅读标头,然后将标头放在每个生成的csv对象之前。