3

I am trying to write a function in python that essentially splits the original CSV file into multiple csv files and saves their names. The code that I have written so far looks like this:

def split_data(a, b, name_of_the_file):
    part = df.loc[a:b-1]
    filename = str(name_of_the_file)
    part.to_csv(r'C:\path\to\file\filename.csv', index=False)

The main intention of the code is to name each file a different name, which is the input (name_of_the_file). The code seems to work, but only saves the file as filename.csv.

3 Answers 3

1

Your function is saving the file(s) with the name filename.csv because you only specify that name in the following line:

part.to_csv(r'C:\path\to\file\filename.csv', index=False)

To change the name you need to change the string to take the filename variable:

part.to_csv(f'C:\path\to\file\{filename}.csv', index=False)

Notice how the string has a f in the beginning of it -- this is called an f-string and it allows you to add Python variables directly into the string by using curly brackets ({filename}).

Sign up to request clarification or add additional context in comments.

5 Comments

You can do raw-strings and format-strings simultaneously like this: rf'C:\path\to\file\{filename}.csv'. If you miss this, python will parse '\t' as a tab character, etc.
Did you add a f before the quotations of the string? It should be: f'C:\path\to\file\{filename}.csv', and not 'C:\path\to\file\{filename}.csv'.
Glad it did! Make sure to mark either my or @Christian's answer as correct via the green marker next to the answers to mark your question as answered for the rest of the board.
Could you please tell me where the green marker is? I couldn't find it.
Left off the answers, underneath the up/down votes.
0

Welcome to Stackoverflow.

I think what you need to do is to user string interpolation.

https://www.programiz.com/python-programming/string-interpolation

Example from the linked page:

name = 'World'
program = 'Python'
print(f'Hello {name}! This is {program}')

In your case something like

def split_data(a, b, name_of_the_file):
    part = df.loc[a:b-1]
    filename = str(name_of_the_file)
    part.to_csv(r'C:\path\to\file\{filename}.csv', index=False)

I have added the curly braces to your code as in my initial example.

I hope that helps.

2 Comments

Hello, thank you for your answer. I tried doing this, but now, the file is being saved as {name_of_the_file}. It is not taking in my input name.
Yes. Do you think it could because of my input? Looks something like this: split_data(8, 999, 'some_name') But still getting saved as {filename}
0

Another approach to saving multiple csv files

# create a list of dataframes (example below assumes a dictionary of dataframes)
lst_of_dfs = [x for x in dfs]

# path to save files
path = 'c:/location_to_save_files/'

# create a list of filenames you would like to use
fnames = ['A', 'B', 'C', 'D', 'E']

# output the data
# this outputs the files using the index, i, + fnames
for i, j in enumerate(fnames, 0):
    lst_of_dfs[i].to_csv(path'+str(i)+str(j)+'.csv')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.