Python脚本,Pandas特殊字符

时间:2018-09-21 16:16:25

标签: python pandas

我正在使用此脚本对地址进行地理编码。该脚本可以正常工作,但是输出文件将诸如中央区Athénée之类的特殊字符转换为乱码。即

中央区-> 中央区

Athénée-> Athénée

输入文件是保存在MAC excel中的UTF-8 .CSV。该脚本正在使用Pandas处理数据。我如何支持上述特殊字符?

完整脚本的代码可以在这里找到: https://github.com/shanealynn/python_batch_geocode/blob/master/python_batch_geocoding.py

 import pandas as pd
    import requests
    import logging
    import time

    #------------------ CONFIGURATION -------------------------------

    # Set your output file name here.
    output_filename = '/Users/_Library/Python/geobatch/res1000_output.csv'
    # Set your input file here
    input_filename = "/Users/_Library/Python/geobatch/res1000.csv"
    # Specify the column name in your input data that contains addresses here
    address_column_name = "Address"
    # Return Full Google Results? If True, full JSON results from Google are included in output
    RETURN_FULL_RESULTS = False

    #------------------ DATA LOADING --------------------------------

    # Read the data to a Pandas Dataframe
    data = pd.read_csv(input_filename, encoding='utf8')

    addresses = data[address_column_name].tolist()


    # All done
    logger.info("Finished geocoding all addresses")
    # Write the full results to csv using the pandas library.
    pd.DataFrame(results).to_csv(output_filename, encoding='utf8')

2 个答案:

答案 0 :(得分:0)

如果我插入一行:

data['Address'] = data['Address'].map(lambda x: x.encode('unicode-escape').decode('utf-8'))

对输入进行解码和重新编码-然后输出变为。

中央区-> \u4e2d\u592e\u533a而不是中央区

如果有人可以在此基础上迈出正确的一步,那又是正确的方向?

答案 1 :(得分:0)

显然这是我正在使用的解决方案:

# Write the full results to csv using the pandas library.
pd.DataFrame(results).to_csv(output_filename, encoding='utf-8-sig')