Reputation: 131
I'm uber new to Python. I have searched through the database and did not find an exact answer to my question. Here is my basic problem:
I have a csv file of the form:
Header1, Header2, Header3
1, a, DF FD
2, b, DD FD
that I need to write out as a csv in the following form:
Header1, Header2, Header3
1, a, DF
1, a, FD
2, b, DD
2, b, FD
Right now I am here with my code:
import csv
with open('InputFile.csv', newline='') as f:
reader = csv.reader(f, delimiter = ',')
for row in reader:
if row[0] != '':
print(row)
I'm using print just to see what I have. My question now is how do I split the space-separated values in column three and get to the output I am trying to achieve? My actual file is more complicated, but I think an answer to this will get me in the right direction.
Upvotes: 0
Views: 2605
Reputation: 3806
I think the following does what you mean:
import csv
import sys
# Index (starts at 0) of the column that needs to be split up
splitColIndex = 2
with open('InputFile.csv') as f:
reader = csv.reader(f, delimiter = ',')
for row in reader:
# Get the element that needs splitting
splitElem = row[splitColIndex]
for subElem in splitElem.split():
# Iterate over the row elements
# (actually the indices)
for idx in range(len(row)):
# If the column doesn't need splitting, we
# can just copy it, otherwise we need to use
# one of the split up values
if idx != splitColIndex:
elem = row[idx]
else:
elem = subElem
if idx > 0:
sys.stdout.write(",")
sys.stdout.write(elem)
sys.stdout.write("\n")
Here, I used the split
function to transform e.g. "DF FD"
into a list [ "DF", "FD" ]
. I also used sys.stdout.write
instead of print
to have slightly more control over the formatting of the output.
Upvotes: 1