- CSV file write Chinese garbled problem and read-write error
In python2.7, the following error occurred after running the program.
1 Traceback (most recent):213File"c:/users/administrator/desktop/python_code/untitled0.py", line 57,inch<module>4File=open ('Da.csv','W', newline="')5TypeError:'NewLine' isAn invalid keyword argument forThis function
Through the degree of Niang search only know, this is due to the python3.x version of the Open () function is not the same usage.
1 # Change the File=open (' Da.csv ', ' W ', newline= ') to the following to resolve 2 file=open ('da,csv','wb') # python2.x using "WB" You can avoid empty lines 3 file.write (codecs. BOM_UTF8) # Solve Chinese garbled problem
To solve this problem you need to start with the following line of code:
1 Import SYS 2 Import Codecs 3 Reload (SYS) 4 sys.setdefaultencoding ('utf-8')
Summarize:
Although it is a small mistake, but did not find out before still think for a long while, can not locate the cause of the mistake.
The ability to program is still to be improved, and more hands-on exercises are needed to gradually grow up in one mistake.
The best way to resolve anxiety is to act immediately.
Python Error Summary