Mar-23-2022, 05:33 PM
I am really new into Python and I struggle with this problem. I have data like this in CSV file:
111_0,3005,QWE
111_0,3006,SDE
111_0,3006,LFR
111_1,3005,QWE
111_1,5345,JTR
112_0,3103,JPP
112_0,3343,PDK
113_0,2137,TRE
113_0,2137,OMG
What I want to do is to format and insert this data into another file in specified line, but it should split into as many files as many different values I have in the first column. So for example, there would be one file named 'file_111_0.txt' where would be only data from rows 1-3, another file named 'file_111_1.txt' with data from rows 4 and 5 and so on.
line1
line2
line3
line4
line5
line6
line7
line8
line9
something 3005 something2 QWE
something 3006 something2 SDE
something 3006 something2 LFR
something 3005 something2 QWE
something 5345 something2 JTR
something 3103 something2 JPP
something 3343 something2 PDK
something 2137 something2 TRE
something 2137 something2 OMG
But what should I do, and what I don't know how to do, is to create another file for every different value from first column, so file 'file_111_0.txt' would look like this:
line1
line2
line3
line4
line5
line6
line7
line8
line9
something 3005 something2 QWE
something 3006 something2 SDE
something 3006 something2 LFR
I have been thinking about using pandas or dictionaries but I can't get this results
111_0,3005,QWE
111_0,3006,SDE
111_0,3006,LFR
111_1,3005,QWE
111_1,5345,JTR
112_0,3103,JPP
112_0,3343,PDK
113_0,2137,TRE
113_0,2137,OMG
What I want to do is to format and insert this data into another file in specified line, but it should split into as many files as many different values I have in the first column. So for example, there would be one file named 'file_111_0.txt' where would be only data from rows 1-3, another file named 'file_111_1.txt' with data from rows 4 and 5 and so on.
import csv from csv import reader data = [] with open('outputfile.txt', 'r') as output_file: lines = output_file.readlines() last = lines[-1] with open('datafile.csv', 'r') as input_file: csv_reader = reader(input_file) for row in csv_reader: data.append(row) with open('output_file', 'w') as output_file: for i, line in enumerate(lines,1): if i == 10: output_file.writelines('something ' + data[row][1] + ' something2 ' + data[row][2]) else: output_file.writelines(line)This code above is basically what I made and it gives me this output:
line1
line2
line3
line4
line5
line6
line7
line8
line9
something 3005 something2 QWE
something 3006 something2 SDE
something 3006 something2 LFR
something 3005 something2 QWE
something 5345 something2 JTR
something 3103 something2 JPP
something 3343 something2 PDK
something 2137 something2 TRE
something 2137 something2 OMG
But what should I do, and what I don't know how to do, is to create another file for every different value from first column, so file 'file_111_0.txt' would look like this:
line1
line2
line3
line4
line5
line6
line7
line8
line9
something 3005 something2 QWE
something 3006 something2 SDE
something 3006 something2 LFR
I have been thinking about using pandas or dictionaries but I can't get this results