搜索â,Â等。。。如何在python中修复奇怪的编码字符

时间:2018-12-23 05:03:20

标签: python-3.x character-encoding

我尝试使用API​​从Google+检索数据。当我将数据写入csv文件时,我观察到了奇怪的奇怪字符,例如ðŸ〜€ðŸ〜„ðŸ〜šðŸ〜‰ðŸ〜’ 谷歌搜索后,我得出结论,这是一个编码问题。

要在文件中写入检索到的数据,我使用了以下代码:

file = open('filename, 'a', encoding='utf-8')
writer = csv.writer(file)
writer.writerow(values)

为了检查终端编码,我使用了

import sys
sys.getdefaultencoding()

输出为:utf-8

不知道问题出在哪里?

1 个答案:

答案 0 :(得分:0)

您的 minimal, reproducible example 似乎最小完整且可验证。无论如何,它看起来像 double mojibake:

using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;

namespace WeatherApp.Droid
{
    [Activity(Label = "WeatherApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
    }
}
<块引用>
value = "‘😀😄😚😉😠’" ### gathered from the question
print(value.encode('cp1252','backslashreplace').decode('utf-8','backslashreplace'))