Python Forum
How to split file by same values from column from imported CSV file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to split file by same values from column from imported CSV file?
#1
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.

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
Reply


Messages In This Thread
How to split file by same values from column from imported CSV file? - by Paqqno - Mar-23-2022, 05:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 501 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Help copying a column from a csv to another file with some extras g0nz0uk 3 561 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
  Converting column of values into muliple columns of counts highland44 0 345 Feb-01-2024, 12:48 AM
Last Post: highland44
  file open "file not found error" shanoger 8 1,445 Dec-14-2023, 08:03 AM
Last Post: shanoger
  Search Excel File with a list of values huzzug 4 1,402 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Need to replace a string with a file (HTML file) tester_V 1 865 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  How can I change the uuid name of a file to his original file? MaddoxMB 2 1,068 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  save values permanently in python (perhaps not in a text file)? flash77 8 1,383 Jul-07-2023, 05:44 PM
Last Post: flash77
  How to "tee" (=split) output to screen and into file? pstein 6 1,547 Jun-24-2023, 08:00 AM
Last Post: Gribouillis
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,381 Apr-06-2023, 11:15 AM
Last Post: AlphaInc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020