Разделение одного CSV на несколько, используя уникальное значение строк в Python - PullRequest
1 голос
/ 08 апреля 2019

Ниже приведено то, что у меня есть в настоящее время с вопросом и файлом, который у меня есть.

Вот что у меня есть:

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

"{" "имя" ":" "круг" " "" сх "": 17 "" су "": 69 "" г "": 102}"

выше формат был изменен ниже. "отсутствуют

* * {Тысячи двадцать-два "Название": "круг", "ая": 1001, "CY": 258, "г": 96} * * 1 023

1 Ответ

0 голосов
/ 08 апреля 2019

Прочитайте заголовок, прежде чем выполнять группирование по, а затем добавьте заголовок к каждому из полученных в результате объектов CSV.

...