Reputation: 47
I've managed to split a row of numbers separated by commas into new columns with each number in a row. However,i am not sure how to remove the double quote from the start and end of each row.
This is my code :
import csv
from csv import writer
COLUMNS = 6
with open("Winning No - Sheet1.csv", "r") as input:
with open("output_file.csv", "w") as f:
output = writer(f, delimiter=";")
output.writerow(["Col {}".format(i+1) for i in xrange(COLUMNS)])
for row in input:
output.writerow(row.split(','))
Output that i get:
COL1 COL2 COL3 COL4 COL5 COL6
"22 23 25 32 33 36"
p/s: Need to remove the double quotes from col1 and col6.
I edited my code into, but i still didn't get the output that i wanted :
output = writer(f, delimiter=";", quoting=csv.QUOTE_NONE, doublequote=False, escapechar=' ')
Upvotes: 1
Views: 5931
Reputation: 47
This code worked for me :
output.writerow(row.replace('\"','').split(','))
Thank you so much for everyone.
Upvotes: 1
Reputation: 19
use this for the output.
var str="test"
str.replace(/^"|"$/g, '')
Upvotes: 0
Reputation: 27574
Just use the .strip()
method, which you call on a string, and which takes a single argument - the string sequence you want to remove from the left and right sides of a string. E.g.
s = '$$cat$$'
s.strip('$') # results in 'cat'
Your example:
import csv
from csv import writer
COLUMNS = 6
with open("Winning No - Sheet1.csv", "r") as input:
with open("output_file.csv", "w") as f:
output = writer(f, delimiter=";")
output.writerow(["Col {}".format(i+1) for i in xrange(COLUMNS)])
for row in input:
output.writerow(row.strip('"').split(','))
Upvotes: 1