【问题标题】:Pandas tweet json weird characters u'�'熊猫推特 json 奇怪的字符 u'�'
【发布时间】:2017-02-14 04:37:01
【问题描述】:

不知道为什么,但是当我将 json 文件中的推文加载到 pandas 时,我得到了很多奇怪的字符。

 for file_name in files:
        if '.json' in file_name:
            file_path = WORKING_DIR + '/data/' + file_name

            # Reading the json as a dict
            with open(file_path) as json_d:
                data = json.load(json_d, encoding='utf8')
                json_df = pd.DataFrame.from_dict(data)
                dfs.append(json_df)

【问题讨论】:

  • u'�' 表示使用'utf-8' 编码解码字符失败。如果你应用严格解码,你会得到错误而不是 u'�'。文件是否采用其他编码?

标签: python json twitter


【解决方案1】:

尝试使用encoding='utf-16'encoding='utf-8'

对于文件中的文件名: 如果文件名中有“.json”: 文件路径 = WORKING_DIR + '/data/' + 文件名

        # Reading the json as a dict
        with open(file_path) as json_d:
            data = json.load(json_d, encoding='utf-16')
            json_df = pd.DataFrame.from_dict(data)
            dfs.append(json_df)

正如@MYGz 建议的“u'�' 意味着它无法用 'utf-8' 解码字符” 所以尝试使用其他编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多