coder3521
coder3521

Reputation: 2646

Write values to a file in python by adding double quotes to the string with spaces

I am trying to write the values of below list in a txt file , in a specific way

valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']

sample.txt

1,Age,"Address has spaces",employee,98363,"hobbies has space in it "

Here if you see valList is a heterogeneous list , my only intention is write the values which had spaces in it surrounded by double quotes, rest all values should be written as it is . So far I have tried :

valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']
# Output = 1,Age,"Address has spaces",employee,98363,"hobbies has space in it "
valstr = ",".join(valList)
with open('sample.txt','wb') as file:
    file.write(valstr)
    file.write('\n' + " \"" + valList[2] + "\" ") 

Using file.write('\n' + " \"" + valList[2] + "\" ") line I am able to write a single value with quotes , but I need a generic way to write values with spaces in it for the given list .

Any help would be highly appreciated .

Upvotes: 1

Views: 6902

Answers (2)

Van Peer
Van Peer

Reputation: 2167

valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']

file = open('sample.txt', 'w')
for item in valList:
    if ' ' in item:
        file.write('"%s",' % item)
    else:
        file.write("%s," % item)

Output

1,Age," Address has spaces",employee,98363,"hobbies has space in it ",

Upvotes: 4

Martijn Pieters
Martijn Pieters

Reputation: 1121306

You have CSV data. Use the csv module and it'll automatically use quoting where needed:

import csv
valList = ['1', 'Age', ' Address has spaces', 'employee', '98363', 'hobbies has space in it ']
with open('sample.txt', 'wb') as file:
    writer = csv.writer(file)
    writer.writerow(valList)

This will quote values as needed; when there is a comma in the field value, or a newline, for example. This doesn't quite fit your exact requirements but a compliant CSV reader will be able to get the exact same values from the file again when reading.

If you MUST have quotes on columns with spaces, you can disable the automatic quoting and add the quotes manually:

import csv
valList = ['1','Age',' Address has spaces','employee','98363','hobbies has space in it ']
with open('sample.txt', 'wb') as file:
    writer = csv.writer(file, quoting=csv.QUOTE_NONE, quotechar='')
    writer.writerow(['"{}"'.format(v) if ' ' in v else v for v in valList])

The list comprehension wraps anything with a space in the value in double quotes. Note that because we disabled quoting here, values with commas or newlines in them will be refused by the csv.writer() as it now can't quote them.

Demo of both methods:

>>> import sys
>>> import csv
>>> valList = ['1', 'Age', ' Address has spaces', 'employee', '98363', 'hobbies has space in it ']
>>> writer = csv.writer(sys.stdout)
>>> writer.writerow(valList)
1,Age, Address has spaces,employee,98363,hobbies has space in it
>>> writer = csv.writer(sys.stdout, quoting=csv.QUOTE_NONE, quotechar='')
>>> writer.writerow(['"{}"'.format(v) if ' ' in v else v for v in valList])
1,Age," Address has spaces",employee,98363,"hobbies has space in it "

Upvotes: 5

Related Questions