Use Pandas’ ExcelWriter to write to Excel
import pandas as pd
xlsx = pd.ExcelWriter('diff.xlsx')
for i in range(1,5):
df = pd.DataFrame(data=[(1,2,3)])
df.to_excel(xlsx, sheet_name='a', index=False)
df = pd.DataFrame(data=[(1,2,3)])
df.to_excel(xlsx, sheet_name='a', index=False)
Another excel write outside the for loop will report an error: Exception caught in workbook destructor. Explicit close() may be require.
this error is generally due to two reasons:
-
- needs to be saved and closed after writing the file. Add that the xlsx.save() file already exists and is open. That is to close the file and run the program
- again. This time, the code encountered a situation 1, which required xlsx.save() outside the for loop, and then write.
Still report an error after modification. A virtual environment was later created using python3.6 and still requires xlsx.save() to run successfully. The
modified python3.6 code is as follows
import pandas as pd
xlsx = pd.ExcelWriter('diff.xlsx')
for i in range(1,5):
df = pd.DataFrame(data=[(1,2,3)])
df.to_excel(xlsx, sheet_name='a', index=False)
df = pd.DataFrame(data=[(1,2,3)])
df.to_excel(xlsx, sheet_name='a', index=False)
xlsx.save()
Read More:
- [Solved] Python Read bam File Error: &&OSError: no BGZF EOF marker; file may be truncated
- How to Fix pandas.errors.ParserError Error tokenizing data C error Buffer overflow caught
- [Solved] PyTorch Caught RuntimeError in DataLoader worker process 0和invalid argument 0: Sizes of tensors mus
- Python3.7 Capture exception error: Too broad exception clause
- [Solved] ParserError: NULL byte detected. This byte cannot be processed in Python‘s native csv library
- Autograd error in Python: runtimeerror: grad can be implicitly created only for scalar outputs
- [Solved] CUDA unknown error – this may be due to an incorrectly set up environment
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- python3 Execute except Exception, e Error [How to Solve]
- Python: How to Use try exception to Display Abnormal Error Information
- How to Solve Python Xlwt ValueError: More than 4094 XFs (styles)
- [Solved] Python Error: TypeError: write() argument must be str, not bytes
- Python: How to Reshape the data in Pandas DataFrame
- Python: Django exception when executing migrate operation: django.db.utils.OperationalError: (1051, “Unknown table’xxx’”)
- [Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘
- [Solved] ValueError: only one element tensors can be converted to Python scalars
- [Solved] Python-selenium locates an element cannot be clicked error: ElementClickInterceptedException
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- RuntimeWarning: overflow encountered in ubyte_Scalars pixel addition and subtraction overflow exception