David Metcalfe
David Metcalfe

Reputation: 2411

Issues outputting strings vs lists from CSV

So I have the below code that's meant to take a CSV file of 4 columns, add in one or two pipes (for MediaWiki table formatting), and spit them out ready for copy/paste usage.

import csv

with open("example.csv", 'r') as CSVfile:
    for row in csv.reader(CSVfile):
        row[0] = "|" + str(row[0])
        row[1] = "||" + str(row[1])
        row[2] = "||" + str(row[2])
        row[3] = "||" + str(row[3])
        f = open("finished.txt", "a")
        ', '.join(row)
        f.write(str(row))

The input looks like the following:

Advertising,Mobile phone App,Coupon,Android

And when I refer to the outputted contents, it appears as so:

['|Advertising', '||Mobile phone App', '||Coupon', '||Android']

Now it's quite likely that I've simply gone about the goal terribly with the above code, but I'd like to know how best to output without the brackets, commas, and apostrophes.

Upvotes: 1

Views: 34

Answers (1)

Anshul Goyal
Anshul Goyal

Reputation: 76907

You need to correct your f.write statement. As it currently stands, you are writing the string representation of the row, which happens to be a list, and not the join of it in to the file. Though you have done a join on the string, the row remains unaffected.

f.write(', '.join(row))

Upvotes: 2

Related Questions