Issue
I have a CSV file that I read like below:
with open ("ann.csv", "rb") as annotate:
for col in annotate:
ann = col.lower().split(",")
print ann[0]
My CSV file looks like below:
H1,H2,H3
da,ta,one
dat,a,two
My output looks like this:
da
dat
but I want a comma separated output like (da,dat). How can I do that?
Solution
First, in Python you have the csv
module - use that.
Second, you're iterating through rows, so using col
as a variable name is a bit confusing.
Third, just collect the items in a list and print that using .join()
:
import csv
with open ("ann.csv", "rb") as csvfile:
reader = csv.reader(csvfile)
reader.next() # Skip the header row
collected = []
for row in reader:
collected.append(row[0])
print ",".join(collected)
Answered By - Tim Pietzcker
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.