简体   繁体   中英

Using Python to write a CSV file with delimiter

I'm new to programming, and also to this site, so my apologies in advance for anything silly or "newbish" I may say or ask.

I'm currently trying to write a script in python that will take a list of items and write them into a csv file, among other things. Each item in the list is really a list of two strings, if that makes sense. In essence, the format is [[Google, http://google.com], [BBC, http://bbc.co.uk]], but with different values of course.

Within the CSV, I want this to show up as the first item of each list in the first column and the second item of each list in the second column.

This is the part of my code that I need help with:

with open('integration.csv', 'wb') as f:
    writer = csv.writer(f, delimiter=',', dialect='excel')
    writer.writerows(w for w in foundInstances)

For whatever reason, it seems that the delimiter is being ignored. When I open the file in Excel, each cell has one list. Using the old example, each cell would have "Google, http://google.com ". I want Google in the first column and http://google.com in the second. So basically "Google" and "http://google.com", and then below that "BBC" and "http://bbc.co.uk". Is this possible?

Within my code, foundInstances is the list in which all the items are contained. As a whole, the script works fine, but I cannot seem to get this last step. I've done a lot of looking around within stackoverflow and the rest of the Internet, but I haven't found anything that has helped me with this last step.

Any advice is greatly appreciated. If you need more information, I'd be happy to provide you with it.

Thanks!

In your code on pastebin, the problem is here:

foundInstances.append(['http://' + str(num) + 'endofsite' + ', ' + desc])

Here, for each row in your data, you create one string that already has a comma in it. That is not what you need for the csv module. The CSV module makes comma-delimited strings out of your data. You need to give it the data as a simple list of items [col1, col2, col3] . What you are doing is ["col1, col2, col3"] , which already has packed the data into a string. Try this:

foundInstances.append(['http://' + str(num) + 'endofsite', desc])

I just tested the code you posted with

foundInstances = [[1,2],[3,4]]

and it worked fine. It definitely produces the output csv in the format

1,2
3,4

So I assume that your foundInstances has the wrong format. If you construct the variable in a complex manner, you could try to add

import pdb; pdb.set_trace()

before the actual variable usage in the csv code. This lets you inspect the variable at runtime with the python debugger. See the Python Debugger Reference for usage details.

As a side note, according to the PEP-8 Style Guide , the name of the variable should be found_instances in Python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM