You can use Python's csv module as follows:
import csv
with open('input.csv', newline='') as f_input, open('output.csv', 'w', newline='') as f_output:
csv_input = csv.reader(f_input, skipinitialspace=True)
csv_output = csv.writer(f_output, quoting=csv.QUOTE_NONNUMERIC)
for row_input in csv_input:
row_output = []
for col in row_input:
try:
row_output.append(int(col))
except ValueError, e:
row_output.append(col)
csv_output.writerow(row_output)
This reads each row in from input.csv and attempts to convert each entry in an int. If this fails it is stored as a string. Each row is then written to output.csv, giving the following type of output:
1,3434,789459,"bdgvdjhjdhf","nagesd232 ","2yuyfudyf","#123 abc calony bangalore"
If your csv also contains columns in float format, the following approach can be used:
import csv
with open('input.csv', newline='') as f_input, open('output.csv', 'w', newline='') as f_output:
csv_input = csv.reader(f_input, skipinitialspace=True)
csv_output = csv.writer(f_output, quoting=csv.QUOTE_NONNUMERIC)
for row_input in csv_input:
row_output = []
for col in row_input:
try:
row_output.append(int(col))
except ValueError, e:
try:
row_output.append(float(col))
except ValueError, e:
row_output.append(col)
csv_output.writerow(row_output)
If Python 2.x is being used, use:
with open('input.csv', 'rb') as f_input, open('output.csv', 'wb') as f_output: