简体   繁体   中英

How do I download a .csv file I created in Python?

I've created a .csv file using this code in a Python shell ( https://stackoverflow.com/a/21872153/2532070 ):

mylist = ['''list_of_strings''']

import csv

with open('filename', 'wb') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
    wr.writerow(mylist)

How do I then download the 'myfile' csv to my machine so I can email it as a .csv attachment? Thanks!

If that is really the exact code that you ran, then the file is named filename (with no extension) and is in the folder that you were in when you ran the script (probably the same folder the script file itself is stored in). The file will not be called myfile , that's just an identifier in the script.

Probably what you wanted to do was

 with open('your_data_in_here.csv', 'wb') as myfile:

which would create a file called your_data_in_here.csv in the current working folder.

I don't think you mean download , I expect you want to read the csv you've just created. For this we'll use csv.reader

>>> import csv
>>> with open('filename', 'rb') as csvfile:
...     spamreader = csv.reader(csvfile)
...     for row in spamreader:
...         print ', '.join(row)
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam

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