简体   繁体   中英

How to fix (what I think is) an encoding issue when exporting python dictionary to .csv file using pandas DataFrame?

I'm new to python, and I'm trying to scrape soccer transfers from a website ( https://www.transfermarkt.co.uk ). I wrote a bunch of code cleaning up the scraped data and now I've tried exporting to a .csv file using DataFrame. When I export the data from a dictionary, some characters (like tilde ñ) are automatically capitalized and have what seems to be a completely random special character in front of them (like '¡' or '@').

I've imported DataFrame from pandas. I'm using windows excel to open the .csv file. When printed by the python console, all letters appear normal (not capitalized and without the special character). All my code works, the issue is when exporting it to the .csv.

df = pd.DataFrame(dict_players)

file_path = dirname + '/' + league + '_' + date + ".csv"

export_csv = df.to_csv (file_path, index = None, header=True)

Here is an example from the .csv file that I copied:

"Michaël"

This has to do with the encoding it is using. The default is utf-8 and it has a byte structure. Some of the values not included in utf-8 are latin small letters i with diaeresis, right-pointing double angle quotation mark, inverted question mark. Therefore you can try changing your encoding to latin-1.

export_csv = df.to_csv(file_path, index = None, header=True,encoding='latin-1')

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