eric
eric

Reputation: 25

writing to csv but not getting desired formatting

I want to remove quoting all together and when I write to my csv file I'm getting an extra \ between the name and ip.

with open('test.csv', 'w') as csvfile:
        info = csv.writer(csvfile, quoting=csv.QUOTE_NONE, delimiter=',', escapechar='\\')
        for json_object in json_data:
            if len(json_object['names']) != 0:
                name = json_object['names'][0]
                ip = json_object['ip_address']
                combined = (name + ',' + ip)
                print combined
                info.writerow([combined])

this is what I'm seeing in my csv file:

ncs1.aol.net\,10.136.0.2

the formatting i'm trying to achieve is:

ncs1.aol.net,10.136.0.2

Upvotes: 0

Views: 55

Answers (2)

codebrotherone
codebrotherone

Reputation: 581

You could also just strip your combined expression:

combine=combined.strip('\')

Upvotes: -1

Reut Sharabani
Reut Sharabani

Reputation: 31339

You don't need to write the row yourself, thats the point of using csv. Instead of creating combined just use:

info.writerow([name, ip])

Currently you're writing a single item to the row and that means the module is escaping , for you and you get \,.

Upvotes: 5

Related Questions