简体   繁体   中英

Python--Create Excel File

I am using the code below to create a file using Python. I don't get any error message when I run it but at the same time no file gets created

df_csv = pd.read_csv (r'X:\Google Drive\Personal_encrypted\Training\Ex_Files_Python_Excel\Exercise Files\names.csv', header=None)

df_csv.to_csv = (r"C:\temp\modified_names.csv")

You are setting df_csv.to_csv to a tuple, which is not how you call methods in python.

Solution:

df_csv.to_csv(r"C:\temp\modified_names.csv")

DataFrame.to_csv documentation here: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_csv.html

Edit: I also noticed the title says "Create Excel File"

To do that you would do the following:

df_csv.to_excel(r"C:\temp\modified_names.xlsx")

Documentation: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_excel.html

I usually make the .csv file like this:

import csv
with open(FILENAME, 'w') as file:
 csv_write = csv.writer(file,delimiter='\t')
 csv_write.writerow(LINE)

LINE : is an array of row you want to write

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