Python脚本,特殊字符

2024-04-26 13:16:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用这个脚本对地址进行地理编码。脚本工作正常,但是输出文件将特殊字符(如中央区Athénée)转换为乱码。i、 e

中央区->;中央区

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

输入文件是保存在MAC excel中的UTF-8.CSV文件。脚本正在使用熊猫来处理数据。如何支持上述特殊字符?你知道吗

完整脚本的代码可以在以下位置找到: 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')

Tags: 文件csvthenameimport脚本inputoutput
2条回答

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

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

如果我插入行:

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

解码和重新编码的输入-然后输出成为。你知道吗

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

如果有人能以此为基础的话,我想这离正确的方向又近了一步?你知道吗

相关问题 更多 >