sf61
sf61

Reputation: 55

How to create a csv file?

I am saving my data that I have extracted from different excel files in a CSV file here's the code :

import pandas as pd
def save_frames(frames, output_path):

        pd.DataFrame( data_mosul_df).to_csv
        output_path= r'C:\Users\Sarah\Desktop\tt' 

        for frame in frames:
            frame.to_csv(output_path, mode='a', header=False)

if __name__ == '__main__':
       frames = []
       save_frames(frames, r'C:\Users\Sarah\Desktop\tt\datam.csv')

The problem is that the code is running, however, the CSV file is not created , anyone has an idea how to fix this? Thank you

Upvotes: 0

Views: 85

Answers (2)

Pitto
Pitto

Reputation: 8579

Please also make sure that you already have this file in the specified path:

C:\Users\Sarah\Desktop\tt\datam.csv

If the file (please check for file, not only for foldeR) is not present please create it in advance (append may not create the file for you).

In your example frames is an empty list, please make sure that in your real code frames is populated correctly.

I've added a quick debug print in order to help you finding out.

The following lines do not make 100% sense to me, so I removed them:

pd.DataFrame( data_mosul_df).to_csv
output_path= r'C:\Users\Sarah\Desktop\tt' 

Can you please explain what you were trying to do there so I can edit my answer in a more helpful way?

Please try the following code:

import pandas as pd
def save_frames(frames, output_path):
        print("Frames content:")
        print(frames)
        for frame in frames:
            frame.to_csv(output_path, mode='a', header=False)

if __name__ == '__main__':
       frames =[pd.DataFrame(data_mosul_df).to_csv]
       save_frames(frames, r'C:\Users\Sarah\Desktop\tt\datam.csv')

Upvotes: 0

cmxu
cmxu

Reputation: 972

You set

frames = []

so the for loop never executes, so no file is created.

Upvotes: 1

Related Questions