[Note] How to deal with unicode error and No such file or directory (output table to excel file with pandas)

[Note] How to deal with unicode error and No such file or directory

What to do about the error you faced when generating the file with the pandas to_excel method.

You can output two or more sheets in one file, Use "Excel Writer" to add a sheet to an existing file.

At this time, the above error occurred in the path setting.

I was addicted to it, so make a note of the cause and solution.


## Conclusion (Countermeasure) Note the following when specifying the path in Excel Writer.

-The "~" that points to the home directory cannot be used. └ Described in "C: / Users /" ・ When using a backslash (or yen mark), describe it with "//" (\) └ Escape with one └ "/" can be used.

It seems that "~" can be used with to_excel and read_excel, but it cannot be used with Excel Writer.


①No such file or directory ** Occurs when a path is specified using "~" **.

It can be used when specifying the path with the read_excel method, but it cannot be used with Excel Writer.


** ▼ Error ** For example, if you specify the path as follows

code


df2 = df.copy()
with pd.ExcelWriter('~/Desktop/GA-demo.xlsx') as writer:  
    df.to_excel(writer, sheet_name='AAA')
    df2.to_excel(writer, sheet_name='BBB')

error


FileNotFoundError: [Errno 2] No such file or directory: '~/Desktop/GA-demo.xlsx'

▼ Even if you change the slash to a backslash, an error will occur as if there was a "~".

code


df2 = df.copy()
with pd.ExcelWriter('~\\Desktop\\GA-demo.xlsx') as writer:  
    df.to_excel(writer, sheet_name='AAA')
    df2.to_excel(writer, sheet_name='BBB')

error


FileNotFoundError: [Errno 2] No such file or directory: '~/Desktop/GA-demo.xlsx'

** ▼ After correction **

OK


df2 = df.copy()
with pd.ExcelWriter('C:/Users/name/Desktop/GA-demo3.xlsx') as writer:  
    df.to_excel(writer, sheet_name='AAA')
    df2.to_excel(writer, sheet_name='BBB')

②unicode error

I get this error when I copy and paste the folder path.

** ▼ Error ** For example, if you specify the path as follows

code


df2 = df.copy()
with pd.ExcelWriter('C:\Users\name\Desktop\GA-demo.xlsx') as writer:  
    df.to_excel(writer, sheet_name='AAA')
    df2.to_excel(writer, sheet_name='BBB')

error


SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

** Backslash "" is for escaping. If you want to recognize it as a character, you need to use "\". ** **


** ▼ After correction **

OK!


df2 = df.copy()
with pd.ExcelWriter('C:\\Users\\name\\Desktop\\GA-demo2.xlsx') as writer:  
    df.to_excel(writer, sheet_name='AAA')
    df2.to_excel(writer, sheet_name='BBB')

The description can be either of the following. 「C:\Users\name\」 「C://Users//name//」

Recommended Posts

[Note] How to deal with unicode error and No such file or directory (output table to excel file with pandas)
[Python] How to output a pandas table to an excel file
[Python] How to read excel file with pandas
[Python] How to deal with pandas read_html read error
How to deal with "Type Error: No matching signature found" error when using pandas fillna
How to read an Excel file (.xlsx) with Pandas [Python]
How to install pandas on EC2 (How to deal with MemoryError and PermissionError)
EC2 / Amazon Linux2: What to do if you get an "unable to execute'gcc': No such file or directory" error with pip install
Import Error: libffi.so.6: cannot open shared object file: No such file or directory
Addressed "Python .h: No such file or directory" when installing uWSGI with Python 3.8
How to deal with module'tensorflow' has no attribute'〇〇'
How to deal with "No module named'〇〇'" error in Jupyter Notebook | Install with! Pip!
What to do when [Errno 2] No such file or directory appears in Python
[Pandas] How to check duplicates and delete duplicates in a table (equivalent to deleting duplicates in Excel)
What to do if ENOENT: no such file or directory appears during npm install
Import Error: libffi.so.6: cannot open shared object file: No such file or directory
No such file or directory:'chromedriver': Resolve'chromedriver'
pip install --upgrade pip will result in No such file or directory
-bash: ./ {file name} .sh: /usr/bin/bash: bad interpreter: No such file or directory
Addressed "Python .h: No such file or directory" when installing uWSGI with Python 3.8
What to do when [Errno 2] No such file or directory appears in Python
What to do if ENOENT: no such file or directory appears during npm install
When I try to run the pip command after updating pip, I get "No such file or directory"
[Note] How to deal with unicode error and No such file or directory (output table to excel file with pandas)
How to extract null values and non-null values with pandas
How to output CSV of multi-line header with pandas
How to convert JSON file to CSV file with Python Pandas
How to convert Json file to CSV format or EXCEL format
How to deal with the problem that pandas 1.1.0 or later build fails on Alpine Linux
[AWS] How to deal with "Invalid codepoint" error in CloudSearch
How to create dataframes and mess with elements in pandas
When I try to run the pip command after updating pip, I get "No such file or directory"
A story about what to do when a bad interpreter: Not such file or directory appears in Anaconda3 and how to investigate the cause.
How to deal with python installation error in pyenv (BUILD FAILED)
How to paste a CSV file into an Excel file using Pandas
Points to note when making pandas read csv of excel output
pip install --upgrade pip will result in No such file or directory
[Python] How to add rows and columns to a table (pandas DataFrame)
How to deal with errors when installing whitenoise and deploying to Heroku
How to deal with errors when installing Python and pip with choco
-bash: ./ {file name} .sh: /usr/bin/bash: bad interpreter: No such file or directory
Output to csv file with Python
How to deal with imbalanced data
How to deal with imbalanced data
Output cell to file with Colaboratory
How to deal with DistributionNotFound errors
No such file or directory:'chromedriver': Resolve'chromedriver'
How to deal with OAuth2 error when using Google APIs from Python
How to deal with SSL error when connecting to S3 with boto of Python
[AWS] How to deal with WordPress "An error occurred when cropping an image."
When bus = smbus.SMBus (1) gets IOError: [Errno 2] No such file or directory when trying to start the GrovePi starter kit
[Python] How to deal with the is instance error "is instance () arg 2 must be a type or tuple of types"