替换一系列Unicode字符/ Python / Twitter

时间:2016-03-23 01:36:53

标签: python-3.x twitter unicode python-unicode

我正在使用Twitter API和Python 3.3从推文中提取文本,并且我正在进入推文的部分,其中推特在推文中放置了三个符号。它们如下所示。 The two flags and the thumbs up

这两个标志和竖起大拇指似乎导致了这个问题。以下是纯文本推文。

@HttpContext.Current.Session.SessionID

以下是我正在使用的代码。

RT @John_Hunt07: Just voted for @marcorubio is Florida! I am ready for a New American Century!! #FLPrimary \ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8\ud83d\udc4d

正如您所看到的,我一直在使用UnicodeFilter函数来尝试过滤掉十六进制中的unicode字符。该函数在处理单个unicode字符时有效,但遇到多个unicode字符放在一起时,此方法失败并出现以下错误:

  

'字符表'编解码器无法对位置107-111中的字符进行编码:字符映射到“未定义”

您是否有任何关于如何解决此问题的想法?

更新:我尝试了Andrew Godbehere's解决方案,但我仍遇到同样的问题。但是,我决定看看是否有任何特定的字符导致问题,所以我决定逐字符地将字符打印到控制台。这给了我如下错误:

  

'字符表'编解码器无法对字符' \ U0001f1fa'进行编码。在位置0:字符映射到' undefined'

看到这个后,我将其添加到UnicodeFilter函数并继续测试。我在逐个字符地打印推文时遇到了多种同类错误。但是,我不想继续做这些例外。例如,请参阅修订后的UnicodeFilter函数:

import json
import mysql.connector
import sys
from datetime import datetime
from MySQLCL import MySQLCL

class Functions(object):
"""This is a class for Python functions"""

@staticmethod
def Clean(string):
    temp = str(string)
    temp = temp.replace("'", "").replace("(", "").replace(")", "").replace(",", "").strip()
    return temp

@staticmethod
def ParseTweet(string):
    for x in range(0, len(string)):
        tweetid = string[x]["id_str"]
        tweetcreated = string[x]["created_at"]
        tweettext = string[x]["text"]
        tweetsource = string[x]["source"]
        tweetsource = tweetsource
        truncated = string[x]["truncated"]
        inreplytostatusid = string[x]["in_reply_to_status_id"]
        inreplytouserid = string[x]["in_reply_to_user_id"]
        inreplytoscreenname = string[x]["in_reply_to_screen_name"]
        geo = string[x]["geo"]
        coordinates = string[x]["coordinates"]
        place = string[x]["place"]
        contributors = string[x]["contributors"]
        isquotestatus = string[x]["is_quote_status"]
        retweetcount = string[x]["retweet_count"]
        favoritecount = string[x]["favorite_count"]
        favorited = string[x]["favorited"]
        retweeted = string[x]["retweeted"]
        if "possibly_sensitive" in string[x]:
            possiblysensitive = string[x]["possibly_sensitive"]
        else:
            possiblysensitive = ""
        language = string[x]["lang"]

        #print(possiblysensitive)
        print(Functions.UnicodeFilter(tweettext))
        #print(inreplytouserid)
        #print("INSERT INTO tweet(ExTweetID, TweetText, Truncated, InReplyToStatusID, InReplyToUserID, InReplyToScreenName, IsQuoteStatus, RetweetCount, FavoriteCount, Favorited, Retweeted, Language, TweetDate, TweetSource, PossiblySensitive) VALUES (" + str(tweetid) + ", '" + Functions.UnicodeFilter(tweettext) + "', " + str(truncated) + ", " + Functions.CheckNull(inreplytostatusid) + ", " + Functions.CheckNull(inreplytouserid) + ", '" + Functions.CheckNull(inreplytoscreenname) + "', " + str(isquotestatus) + ", " + str(retweetcount) + ", " + str(favoritecount) + ", " + str(favorited) + ", " + str(retweeted) + ", '" + str(language) + "', '" + Functions.ToSQL(tweetcreated) + "', '" + Functions.ToSQL(tweetsource) + "', " + str(possiblysensitive) + ")")
        #MySQLCL.Set("INSERT INTO tweet(ExTweetID, TweetText, Truncated, InReplyToStatusID, InReplyToUserID, InReplyToScreenName, IsQuoteStatus, RetweetCount, FavoriteCount, Favorited, Retweeted, Language, TweetDate, TweetSource, PossiblySensitive) VALUES (" + str(tweetid) + ", '" + tweettext + "', " + str(truncated) + ", " + Functions.CheckNullNum(inreplytostatusid) + ", " + Functions.CheckNullNum(inreplytouserid) + ", '" + Functions.CheckNull(inreplytoscreenname) + "', " + str(isquotestatus) + ", " + str(retweetcount) + ", " + str(favoritecount) + ", " + str(favorited) + ", " + str(retweeted) + ", '" + language + "', '" + str(Functions.FormatDate(tweetcreated)) + "', '" + str(Functions.UnicodeFilter(tweetsource)) + "', " + str(possiblysensitive) + ")")

@staticmethod
def ToBool(variable):
    if variable.lower() == 'true':
        return True
    elif variable.lower() == 'false':
        return False

@staticmethod
def CheckNullNum(var):
    if var == None:
        return "0"
    else:
        return str(var)

@staticmethod
def CheckNull(var):
    if var == None:
        return ""
    else:
        return var

@staticmethod
def ToSQL(var):
    temp = var
    temp = temp.replace("'", "")
    return str(temp)

@staticmethod
def UnicodeFilter(var):
    temp = var
    temp = temp.replace(chr(0x2019), "")
    temp = temp.replace(chr(0x003c), "(lessthan)")
    temp = temp.replace(chr(0x003e), "(greaterthan)")
    temp = temp.replace(chr(0xd83c), "")
    temp = temp.replace(chr(0xddfa), "")
    temp = temp.replace(chr(0xddf8), "")
    temp = temp.replace(chr(0xd83d), "")
    temp = temp.replace(chr(0xdc4d), "")
    temp = Functions.ToSQL(temp)
    return temp

@staticmethod
def FormatDate(var):
    temp = var
    dt = datetime.strptime(temp, "%a %b %d %H:%M:%S %z %Y")
    retdt = str(dt.year) + "-" + str(dt.month) + "-" + str(dt.day) + "T" + str(dt.hour) + ":" + str(dt.minute) + ":" + str(dt.second)
    return retdt

我不想为每个导致问题的角色添加新行。通过这种方法,我已经能够传递多个推文,但是这个问题重新出现在每个包含不同符号的推文上。是否没有可以过滤掉所有这些字符的解决方案?是否可以过滤掉不在utf-8字符集中的所有字符?

3 个答案:

答案 0 :(得分:0)

尝试内置的unicode编码/解码错误处理功能:str.encode(errors='ignore')

例如:

problem_string = """\
RT @John_Hunt07: Just voted for @marcorubio is Florida! I am ready for a New American Century!! #FLPrimary \ud83c\uddfa\ud83c\uddf8\ud83c\uddfa\ud83c\uddf8\ud83d\udc4d
"""
print(problem_string.encode(errors='ignore').decode('utf-8'))

忽略错误会删除有问题的字符。

> RT @John_Hunt07: Just voted for @marcorubio is Florida! I am ready for a New American Century!! #FLPrimary 

其他错误处理选项可能会引起关注。 例如xmlcharrefreplace会产生:

> RT @John_Hunt07: Just voted for @marcorubio is Florida! I am ready for a New American Century!! #FLPrimary ����������

如果您需要UnicodeFilter功能所暗示的自定义过滤功能,请参阅Python documentation on registering an error handler

答案 1 :(得分:0)

Python提供了一个有用的堆栈跟踪,因此您可以了解错误的来源。 使用它,您会发现print导致异常。

print()失败是因为您从Windows控制台运行Python,默认情况下,它仅支持您的本地8位charmap。您可以添加支持:https://github.com/Drekin/win-unicode-console

您也可以直接将数据写入文本文件。使用以下命令打开文件:

open('output.txt', 'w', encoding='utf-8')

答案 2 :(得分:0)

找到答案。问题在于推文中有一系列字符导致问题。一旦我找到了正确的Unicode字符范围,我实现了for循环来替换该范围内任何Unicode字符的出现。在实现之后,我能够在没有任何格式或MySQL错误的情况下发送数千条推文。

@staticmethod
def UnicodeFilter(var):
    temp = var
    temp = temp.replace(chr(0x2019), "'")
    temp = temp.replace(chr(0x2026), "")
    for x in range(127381, 129305):
        temp = temp.replace(chr(x), "")
    temp = MySQLCL.ToSQL(temp)
    return str(temp)