-1

这是我的代码,

In [13]: test = """<div dir="ltr" style="text-align: left;" trbidi="on"><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">It’s hard to say what’s happening to us is something happy or not-and to what extend that particular thing is ‘a’ happy thing or not…..don’t believe it???</span><br style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;" /><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">Well, when I was relaxing and indulging myself with watchin’ my favourite Chinese drama series, ‘square pegs’; my bro-in law asked me to buy him express bus ticket. And pastu, zuhadi called me up ajak aku gi KLIA hantar Mia g German..lgpun Arep pun nk ikut..I agreed..without knowin, that something will come up later…&lt;/span><br style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;" /><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">I waited for Zuhadi at Jelatek LRT Putra Station..and after sometimes, he came and told me that Arep tak jd dtg….I knew it already bcoz I had already called Arep n he said that Mia had told(or wanted to tell) Zuhadi tak payah gi KLIA sbb die pun gi ngn KLIA Transit jer n better hantar duit dier yg zuhadi pinjam kat umh jer kan..</span><br style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;" /><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">Since Arep pun tak dtg and Zuhadi pun seemed like havin another plan that was to go to Nini’s(Zuhadi’s fren who we mistakenly took her as Zuhadi gf) house(to get sumthin’). When we arrived at her house..but she wasn’t there( she was in Klang)..so I guess u know what we did-went there!</span><br style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;" /><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">Along the way we talked bout our planning-Zuhadi get a job at AIA-congrats to him!!!..but me????..no what-so-ever plan yet!..we chatted..chat..chat..and then…..Gek..Gek…Gekk!..</span><br style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;" /><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">We heard an awkward sound as we were passing a tol plaza…Then we realized that..THE RIGHT BACK TYRE HAD PUNCTURED!!!</span><br style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;" /><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">IT took us almost an hour or so to change it!..we looked around for a car jack..but it was juz not there..luckily there was another car who broke down a long the road..so Zuhadi went and asked’em for that eject thingy..dunno what it called….Baru separuh gune benda alah tu..owner dia ckp yg kembara (keta abg Zuhadi) selalunye letak eject kat co-pilot punyer place..then we found it..hahahaha..tp punye le payah nk gune.sampai patah screw-driver yg Zuhadi gune!!..siap bleeding lg tangan die..kesian siot!!!</span><br style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;" /><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">Last skali kitorg tukar tempat nk eject tuh..barulerr terangkat keta tuh!!!(hey..dun blame me!!!..i know nothin’ bout cars ok!)..we tought the ordeal was over, but nahhh!!!!..misery was so sad and tak sampai hat ink leave kitorg!!....Sampai nk mampus kitorg try nk bukak screw tayar yg pancit tu..tak gak bukak2!!!..then I went to the plaza tol and asked for peronda highway..pastu bl dh gi kat kat keta balik..barulerr ble bukak tayar pancit tuh!!!..AKHIRNYA!!!!!</span><br style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;" /><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">So we continued our journey in meeting Nini n took her n Hazani(her fren) back to her house (Klang is where she’s havin her practicum). And I arrived home at almost 2a.m.</span><br style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;" /><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">Well..why am I tellin’ this???...hahahha..you may think it was a terrible ordeal..bad experience..pengalaman pahit..but nahhh..i found it was very interesting, becoz we really had a good time—becoz if I were not there..Zuhadi will hav to face the prob alone and I was surely gonna be boring and depressing !!..i admit I didn’t do nothing much..but at least I was there to accompany him n jd penghibur tidak rasmi!..and if I didn’t go out wif him..i’d never face that kind of situation-b4 this..i never experienced tayar pancit!! hehehhee…&lt;/span><br style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;" /><span style="background-color: #1c1c1c; color: #cccccc; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 15px; line-height: 20px;">"And you know what????...i got it all in my digicam !"</span></div>
   ....: 2013-10-09 18:30:00.001000
   ....: O’ happy day!!!"""

In [14]: type(test)
Out[14]: str

In [15]: test.encode('utf-8')

我得到了这样的输出,

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 209: ordinal not in range(128)

由于测试引发的上述错误已经编码。但我使用 type 关键字它显示 str。我的问题是字符串和 ascii 或 (utf-8) 的组合?我需要找到给定的字符串是编码的还是正常的str。如果它被编码,则什么也不做,否则转换编码。谢谢...

4

1 回答 1

3

在 Python 2 中,已经str编码。

因为您正在尝试对已经编码的值进行编码,所以 Python 会首先尝试使用默认的 ASCII 编解码器为您解码它,unicode但失败了。

值的编码str取决于您输入字符串文字的方式。在终端中,它将使用终端编解码器。在源文件中,这取决于您的编辑器使用什么编解码器来保存文件。如果从另一个源(外部文件、流或网络套接字)加载,它在很大程度上取决于该通信通道的标准使用了什么编解码器,以及您的程序是否可以检索使用的确切编解码器。有时它是未知的并且不能可靠地提取。

您无法检测到“正常”str数据和编码数据之间的差异,因为所有 str值都是编码数据。您通常会查找unicode值并使用.encode()这些来获取str值:

if isinstance(value, unicode):
    # encode to UTF8 first
    value = value.encode('utf8')
于 2013-10-10T06:48:11.627 回答