Prasanna Kumar
Prasanna Kumar

Reputation: 41

How to add double quotes to strings in csv using Python

I have set data in a CSV file and I want to add double quotes to text and alphanumeric data, with the remaining integer, float and decimals not in double quotes.

Can any one help on this?

Sample data:

1, 3434, 789459 ,bdgvdjhjdhf, nagesd232 ,2yuyfudyf, #123 abc calony bangalore 

The expected result would be:

1, 3434, 789459 ,"bdgvdjhjdhf", "nagesd232" ,"2yuyfudyf", "#123 abc calony bangalore" 

Upvotes: 1

Views: 5083

Answers (3)

Martin Evans
Martin Evans

Reputation: 46789

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:

Upvotes: 4

Paweł Babicz
Paweł Babicz

Reputation: 81

You can check what class have any of your values:

list_val = [1, 3434, 789459 ,bdgvdjhjdhf, nagesd232 ,2yuyfudyf, #123 abc calony bangalore]
for i in list_val:
    if i.__class__ = str:
    .....

Upvotes: 0

Yen
Yen

Reputation: 69

input_data = '1, 3434, 789459 ,bdgvdjhjdhf, nagesd232 ,2yuyfudyf, #123 abc calony bangalore'
output = []
for i in input_data.split(','):
    i = i.strip()
    if not i.isnumeric():
        output.append('"%s"' % i)
    else:
        output.append(i)
print(', '.join(output))

Upvotes: 0

Related Questions