2015-05-15 25 views
1

研究蟒蛇與this tutorialpycharm控制檯UNICODE到可讀的字符串

問題是,當我試圖讓西裏爾字母i的pycharm控制檯中看到的Unicode。

enter image description here

import requests 
from bs4 import BeautifulSoup 
import operator 
import codecs 

def start(url): 
    word_list = [] 
    source_code = requests.get(url).text 
    soup = BeautifulSoup(source_code) 

    for post_text in soup.findAll('a', {'class': 'b-tasks__item__title js-set-visited'}): 
     content = post_text.string 

     words = content.lower().split() 
     for each_word in words: 
      word_list.append(each_word) 
    clean_up_list(word_list) 



def clean_up_list(word_list): 
    clean_word_list = [] 
    for word in word_list: 
     symbols = "[email protected]#$%^&*()_+{}|:<>?,./;'[]\=-\"" 
     for i in range(0, len(symbols)): 
      word = word.replace(symbols[i], "") 
     if len(word) > 0: 
      clean_word_list.append(word) 
    create_dictionary(clean_word_list) 



def create_dictionary(clean_word_list): 
    word_count = {} 
for word in clean_word_list: 
    if word in word_count: 
     word_count[word] += 1 
    else: 
     word_count[word] = 1 

for key, value in sorted(word_count.items(), key=operator.itemgetter(1)): 
    print(key, value) 

當我改變打印(鍵,值)打印(key.decode( 'utf-8'),值)我得到「UnicodeEncodeError: 'ASCII' 編解碼器不能在0-7位置編碼字符:順序不在範圍內(128)」

enter image description here

開始( 'https://youdo.com/tasks-all-opened-all-moscow-1') 在互聯網上有一些關於在某些文件中更改編碼的建議 - 實際上並沒有得到它。我不能在控制檯中讀取它嗎? OSX

UPD key.encode( 「UTF-8」)enter image description here

+0

*編碼*,不解碼。 –

+0

endcode並沒有幫助,我忘了告訴https://leto12h.storage.yandex.net/rdisk/7542b806b8131d628b2d2c130db803ca7d95f555b0fd6e0ea9c2bcacc898b38d/inf/FbgCBUYw8cjaFgtIj_Ts4tmTlk4y02jJxQHa2XBfel9TkrFy7Db3bI6xxGIrZg6jBbEUGeCswV_4vfTEJl5bLQ==?uid=0&filename=2015-05-15%2014-00-47% 201.py%20-%20untitled%20-%20%5B%7E%20PycharmProjects%20untitled%5D.png&處置=直列&散列=&極限= 0&CONTENT_TYPE =圖像%2Fpng&tknv = v2和rtoken = b1577401cae5f5d42e2af1c8929d3b7d&force_default =無 – one2gov

回答

0

UTF-8是有時是痛苦的。我用拉丁文字符創建了一個文件,另一個文件用俄語創建。下面的代碼:

# encoding: utf-8 

with open("testing.txt", "r", encoding='utf-8') as f: 
    line = f.read() 
    print(line) 

在PyCharm輸出

enter image description here

注意兩個encoding

因爲你是從網頁中獲取數據,你必須確保你使用正確的編碼。在PyCharm下面的代碼

# encoding: utf-8 
r = requests.get('http://www.pravda.ru/') 
r.encoding = 'utf-8' 
print(r.text) 

輸出作爲

enter image description here

請注意,您必須專門設置編碼相匹配的頁面之一。

相關問題