Exporting multiple csv files with dynamic naming

Question:

I created about 200 csv files in Python and now need to download them all.

I created the files from a single file using:

g = df.groupby("col")
for n,g in df.groupby('col'):
    g.to_csv(n+'stars'+'.csv')

When I try to use this same statement to export to my machine I get a syntax error and I’m not sure what I’m doing wrong:

g = df.groupby("col")
for n,g in df.groupby('col'):
    g.to_csv('C:UsersegagneDownloads'n+'stars'+'.csv'')

Error:

  File "<ipython-input-27-43a5bfe55259>", line 3
    g.to_csv('C:UsersegagneDownloads'n+'stars'+'.csv'')
                                                 ^
SyntaxError: invalid syntax

I’m in Jupyter lab, so I can download each file individually but I really don’t want to have to do that.

Asked By: LMGagne

||

Answers:

g.to_csv('C:UsersegagneDownloads'n+'stars'+'.csv'')
needs to be
g.to_csv('C:\Users\egagne\Downloads\'+n+'stars.csv').

There were two things wrong — the backslash is an escape character so if you put a ' after it, it will be treated as part of your string instead of a closing quote as you intended it. Using \ instead of a single escapes the escape character so that you can include a backslash in your string.

Also, you did not pair your quotes correctly. n is a variable name but from the syntax highlighting in your question it is clear that it is part of the string. Similarly you can see that stars and .csv are not highlighted as part of a string, and the closing '' should be a red flag that something has gone wrong.

Edit: I addressed what is causing the problem but Ami Tavory’s answer is the right one — though you know this is going to run on windows it is a better practice to use os.path.join() with directory names instead of writing out a path in a string. str(n) is also the right way to go if you are at all unsure about the type of n.

Answered By: MoxieBall

You’re possibly mixing up integers and strings, and the use of backslash in literals is dangerous anyway. Consider using the following

import os

inside the loop

    f_name = os.path.join('C:', 'users', ' egagne', 'Downloads', str(n), 'stars.csv')
    g.to_csv(f_name)

with os.path.join taking care of the backslashes for you.

Answered By: Ami Tavory

This really work!!! Thanks for sharing,MoxieBall.

Answered By: Melanie Lee