How to split up data from a column in a csv file into two separate output csv files?

2024/7/8 6:11:18

I have a .csv file, e.g.:

ID NAME CATEGORIES
1, x,   AB
2, xx,  AA
3, xxx, BA

How would I get this to form two output .csv files based on the category e.g.:

File 1:

ID NAME CATEGORY
1, x,   A
2, xx,  A
3, xxx, B

File 2:

ID NAME CATEGORY
1, x,   B
2, xx,  A
3, xxx, A

I have the input and output set up, but just an empty for loop where I'm stumped:

records = [line for line in csv.reader(open('test_input.csv', 'rt'), delimiter=',')]
outfile = open('test_output1.csv', 'wt')
outfileWriter = csv.writer(outfile, delimiter=',')for record in records:#something!outfileWriter.writerow(record)outfile.close()

I'd appreciate any help!

Answer
import csvrecords = [line for line in csv.reader(open('test_input.csv', 'rt'), delimiter=',')]
outfile1 = open('test_output1.csv', 'wt')
outfile2 = open('test_output2.csv', 'wt')
outfileWriter1 = csv.writer(outfile1, delimiter=',')
outfileWriter2 = csv.writer(outfile2, delimiter=',')# headers always the same
outfileWriter1.writerow(records[0])
outfileWriter2.writerow(records[0])for record in records[1:]:cat = record[-1].strip() # get category in form "AB"new_record = recordnew_record[-1] = "\t%s" % cat[0] # set category for file 1 with tab as a prefixoutfileWriter1.writerow(new_record)new_record[-1] = "\t%s" % cat[1] # set category for file 2 with tab as a prefixoutfileWriter2.writerow(new_record)outfile1.close()
outfile2.close()
https://en.xdnf.cn/q/119707.html

Related Q&A

Discord.py spellcheck commands

Recently, I looked up Stack Overflow and found this code which can check for potential typos: from difflib import SequenceMatcher SequenceMatcher(None, "help", "hepl").ratio() # Ret…

Django Model Form doesnt seem to validate the BooleanField

In my model the validation is not validating for the boolean field, only one time product_field need to be checked , if two time checked raise validation error.product_field = models.BooleanField(defau…

For loop only shows the first object

I have a code that loops through a list of mails, but it is only showing the first result, even though there are also other matches. The other results require me to loop over the mails again only to re…

IndexError: pop from empty list

I need help. I have no idea why I am getting this error. The error is in fname = 1st.pop()for i in range(num) :fname = lst.pop()lTransfer = [(os.path.join(src, fname), os.path.join(dst, fna…

Cannot import name StandardScalar from sklearn.preprocessing [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

unable to solve strptime() issue even after trying all the formats

Im using the following code:data[Input_volTargetStart][1]>time.strptime(data[Dates][1], "%d %b $y")When I try to run it, I get this error:ValueError: time data 04-Jun-99 does not match for…

OSError. locateOnScreen not working in pyautogui

import pyautoguipyautogui.locateOnScreen(photo.png)Error: OSError: Failed to read photo.png because file is missing, has improper permissions, or is an unsupported or invalid format

Insert into table using For In Range and keys of the value

I have a query (sql1) that populates data, and I am trying to insert the outcome of this data (sql1) as well as other inputs into same table.Here is first query (sql1).sql1 = Select Creator_Id, Record…

Django - how to follow some object (not user) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…

Python : wildcard query in Elasticsearch

I want to query a field using wildcard in Elasticsearch but the problem is that the search string is stored in a variable and not available statically. The intended query is : body={"query": …