简体   繁体   English

摆脱特殊字符和 unicode 字节 object 转换为字符串 Python

[英]Getting rid of special characters and unicode in byte object converted to string in Python

I'm having difficulty getting ride of special characters and unicode in Pandas dataframe.我很难在 Pandas dataframe 中乘坐特殊字符和 unicode。

Here is an example of one of the byte objects:这是其中一个字节对象的示例:

b"Asana Check out your two-week recap. \n \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \n\xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \n\xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \n\n\n View in browser

I tried using regex to eliminate the repeating special characters with this:我尝试使用正则表达式来消除重复的特殊字符:

re.sub(r"""|b"|(\\n)+|b|\\xe2\\x80\\x8c '""", "", string.decode('utf-8'))

When I run this code, I get:当我运行这段代码时,我得到:

Asana Check out your two-week recap. \n \u200c \u200c

I can't remove the special unicode characters with regex, so tried doing我无法使用正则表达式删除特殊的 unicode 字符,所以尝试这样做

string.encode("ascii", "ignore")

That works, But: then I still have some special characters in the string:那行得通,但是:然后我在字符串中仍然有一些特殊字符:

b"Asana Check out your two-week recap. \n

But, when I try to use但是,当我尝试使用

re.sub(r"""|b"|(\\n)+|b'""", "", string)

I get an error that I can't use.sub on a byte object. If I add .decode('utf-8') the \n don't get replaced.我得到一个错误,我不能在字节 object 上使用 .sub。如果我添加.decode('utf-8') \n不会被替换。

Can someone help explain what's going on and how to fix this?有人可以帮助解释发生了什么以及如何解决这个问题吗? I'm used to just dealing with plain text strings.我习惯于只处理纯文本字符串。

Here's some code below, but I'm not sure how to create the same kind of 'byte' object that I currently have in the pandas dataframe. So, it's a simple string below:下面是一些代码,但我不确定如何创建与我目前在 pandas dataframe 中拥有的相同类型的“字节”object。因此,它是下面的一个简单字符串:

string = 'b"Asana Check out your two-week recap. \n \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \n\xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \n\xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \xe2\x80\x8c \n\n\n View in browser'

string = re.sub(r"""|b"|(\\n)+|b'""", "", string.decode('utf-8'))
    string
    
    string = string.encode("ascii", "ignore")
    
    string
    re.sub(r"""|b"|\\n|b'""", "", string.decode('utf-8'))

Thanks and please let me know how I can make this question easier to answer!谢谢,请告诉我如何使这个问题更容易回答!

You can use您可以使用

df['col'] = df['col'].apply(lambda x: " ".join(x.decode('utf-8').replace('\u200C','').strip().split()))

Details :详情

  • x.decode('utf-8') - decode the byte string as a UTF8 string x.decode('utf-8') - 将字节字符串解码为 UTF8 字符串
  • .replace('','') - remove the zero-width joiner symbol .replace('','') - 删除零宽度连接符
  • .strip() - remove leading/trailing whitespace .strip() - 删除前导/尾随空格
  • .split() - split with whitespace .split() - 用空格分割
  • " ".join(...) - join the resulting list with a space " ".join(...) - 用空格连接结果列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM