0

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!

Community
  • 1
  • 1
YPCrumble
  • 26,610
  • 23
  • 107
  • 172
  • 1
    what machine do you think the file went to? :) it'll be wherever you ran the python shell from. – Eevee Mar 28 '14 at 02:48

2 Answers2

1

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.

Larry Lustig
  • 49,320
  • 14
  • 110
  • 160
0

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
Aaron
  • 2,341
  • 21
  • 27
  • I actually want to download it so that I can email to someone using Excel who specifically wants it as a .csv file. – YPCrumble Mar 28 '14 at 02:41
  • Well, then what you have created is in fact a csv file. If you save it with the .csv extension and email it to someone with Excel, they'll be able to open it. – Aaron Mar 28 '14 at 02:42
  • Thanks - I don't understand, where is said file? It's on my machine somewhere? – YPCrumble Mar 28 '14 at 02:43
  • If you run a script which writes a csv file and it's called 'filename' or 'filename.csv' then it should be in the same directory as the script you've executed. – Aaron Mar 28 '14 at 02:46
  • Got it, thank you, so it automatically saves into the directory from which I ran the shell. – YPCrumble Mar 28 '14 at 03:01