2011-03-31 5 views
1

저는 Python을 처음 접했고 인코딩 문제가 있습니다. 일부 라인을 가지고Python의 인코딩 문제

# -*- coding: utf-8 -*- 
import config # Ficheiro de configuracao 
import twitter 
import random 
import sqlite3 
import time 
import bitly_api #https://github.com/bitly/bitly-api-python 
import feedparser 

class TwitterC: 
    def logToDatabase(self, tweet, timestamp): 
     # Will log to the database 
     database  = sqlite3.connect('database.db') # Create a database file 
     cursor  = database.cursor() # Create a cursor 
     cursor.execute("CREATE TABLE IF NOT EXISTS twitter(id_tweet INTEGER AUTO_INCREMENT PRIMARY KEY, tweet TEXT, timestamp TEXT);") # Make a table 
     # Assign the values for the insert into 
     msg_ins  = tweet 
     timestamp_ins = timestamp 
     values  = [msg_ins, timestamp_ins] 
     # Insert data into the table 
     cursor.execute("INSERT INTO twitter(tweet, timestamp) VALUES(?, ?)", values) 
     database.commit() # Save our changes 
     database.close() # Close the connection to the database 

    def shortUrl(self, url): 
     bit = bitly_api.Connection(config.bitly_username, config.bitly_key) # Instanciar a API 
     return bit.shorten(url) # Encurtar o URL 

    def updateTwitterStatus(self, update): 
     short = self.shortUrl(update["url"]) # Vou encurtar o URL 
     update_str = update["msg"] + " " + short['url'] # Mensagem em bruto, sem tratamento de contagem de caracteres 
     # I will see how much characters have the message, if more than 140, delete some chars 
     length_message = len(update_str) 
     if length_message > 140: 
      length_url = len(short['url']) 
      count_message = 136 - length_url 
      shorten_msg = update["msg"][0:count_message] + '... ' 
      update_str = shorten_msg + short['url'] 
     # Will post to twitter and print the posted text 
     api  = twitter.Api(consumer_key=config.consumer_key, 
           consumer_secret=config.consumer_secret, 
           access_token_key=config.access_token_key, 
           access_token_secret=config.access_token_secret) 
     status = api.PostUpdate(update_str) # Fazer o update 
     msg  = status.text # Vou gravar o texto enviado para a variavel 'msg' 
     # Vou gravar p a Base de Dados 
     self.logToDatabase(msg, time.time()) 
     print msg # So p mostrar o texto enviado. Comentar esta linha de futuro. 

# Exemplo base   
#x = TwitterC() 
#x.updateTwitterStatus({"url": "http://xyz.com/?cat=28", "msg": "Some tips about PostgreSQL Administration?"}) 

# Solucao para um misto de feeds e frases feitas 
# Vou escolher uma fonte ao acaso 
p = range(2) # tem o 0 e o 1 
p = random.choice(p) 

if p == 0: # Escolhe TEXT UPDATES 
    # Vou escolher um text update ao acaso 
    text_a_enviar = random.choice(config.text_updates) 
    update_to_send = text_a_enviar  
elif p == 1: # Escolhe FEEDS UPDATES 
    '''# Vou escolher um feed ao acaso 
    feed_a_enviar = random.choice(config.feeds_updates) 
    # Vou apanhar o conteudo do feed 
    d = feedparser.parse(feed_a_enviar["feedurl"]) 
    # Vou definir quantos feeds quero ter no i 
    i = range(8) 
    # Vou meter para "updates" 10 entradas do feed 
    updates = [] 
    for i in range(8): 
     updates.append([{"url": feed_a_enviar["linktoourpage"], "msg": d.entries[i].summary + ", "}]) 
    # Vou escolher ums entrada ao acaso 
    update_to_send = random.choice(updates)''' 

# Vou postar p o Twitter  
x = TwitterC() 
x.updateTwitterStatus({"url": "http://xyz.com/", "msg": "favoritos à distancia"}) 

코드 그러나 문제는이 라인에 있습니다 :

코드를 참조하십시오이 줄은 악센트 "à"이 원인으로 문자를

x.updateTwitterStatus({"url": "http://xyz.com/", "msg": "favoritos à distancia"}) 

문제는 여기에 :이 줄 더 정확하게

def updateTwitterStatus(self, update): 
    short = self.shortUrl(update["url"]) # Vou encurtar o URL 
    update_str = update["msg"] + " " + short['url'] # Mensagem em bruto, sem tratamento de contagem de caracteres 
    ... 

:

이 문제를 해결하는 방법에 대한

x.updateTwitterStatus({"url": "http://xyz.com", "msg": "favoritos à distancia"}) 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 48: ordinal not in range(128) 

모든 단서 : 6,

update_str = update["msg"] + " " + short['url'] # Mensagem em bruto, sem tratamento de contagem de caracteres 

오류의 출력이 무엇입니까?

+5

http://farmdev.com/talks/unicode/ –

+0

http://www.joelonsoftware.com/articles/Unicode.html – brandizzi

답변

1

파일 상단에 from __future__ import unicode_literals을 추가하십시오. 또는 모든 문자열 앞에 'u '를 붙일 수 있습니다. 즉, u"favoritos à distancia"

파일이 실제로 utf-8로 저장되었는지 확인하십시오!