1

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" 
0

3 Answers 3

4

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:
Sign up to request clarification or add additional context in comments.

1 Comment

I've been searching for a solution like this! I can get this to work on a single file, but I'm trying to iterate it through a directory of files (about 600). Is it possible to do this? I have a question open here - stackoverflow.com/questions/55369361/…
0
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))

1 Comment

You should explain your solution, not just post a block of code.
0

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:
    .....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.