2014-11-15 1 views
0

그래서 저는 reddit에서 게시물을 가져 와서 트윗합니다. 이 게시물 중 일부는 latin-1 문자를 포함합니다.UnicodeEncodeError : 'latin-1'코덱은 u ' u015f'위치 21에 인코딩 할 수 없습니다. 서수는 범위 (256)가 아닙니다.

print post.encode('latin-1') + " " + post_dict[post].encode('latin-1') + " #python"

이 캐릭터에게 ê 완벽하게 벌금을 출력합니다. 그러나 링크에서 볼 수 있듯이 라틴어 -1 문자 인 ş은 인쇄되지 않습니다.

스크립트 코드 :

import praw 
import json 
import requests 
import tweepy 
import time 

access_token = 'REDACTED' 
access_token_secret = 'REDACTED' 
consumer_key = 'REDACTED' 
consumer_secret = 'REDACTED' 

def strip_title(title): 
    if len(title) < 94: 
     return title 
    else: 
     return title[:93] + "..." 

def tweet_creator(subreddit_info): 
    post_dict = {} 
    post_ids = [] 
    print "[bot] Getting posts from Reddit" 
    for submission in subreddit_info.get_hot(limit=20): 
     post_dict[strip_title(submission.title)] = submission.url 
     post_ids.append(submission.id) 
    print "[bot] Generating short link using goo.gl" 
    mini_post_dict = {} 
    for post in post_dict: 
     post_title = post 
     post_link = post_dict[post]   
     short_link = shorten(post_link) 
     mini_post_dict[post_title] = short_link 
    return mini_post_dict, post_ids 

def setup_connection_reddit(subreddit): 
    print "[bot] setting up connection with Reddit" 
    r = praw.Reddit('yasoob_python reddit twitter bot ' 
       'monitoring %s' %(subreddit)) 
    subreddit = r.get_subreddit(subreddit) 
    return subreddit 

def shorten(url): 
    headers = {'content-type': 'application/json'} 
    payload = {"longUrl": url} 
    url = "https://www.googleapis.com/urlshortener/v1/url" 
    r = requests.post(url, data=json.dumps(payload), headers=headers) 
    link = json.loads(r.text)['id'] 
    return link 

def duplicate_check(id): 
    found = 0 
    with open('posted_posts.txt', 'r') as file: 
     for line in file: 
      if id in line: 
       found = 1 
    return found 

def add_id_to_file(id): 
    with open('posted_posts.txt', 'a') as file: 
     file.write(str(id) + "\n") 

def main(): 
    subreddit = setup_connection_reddit(‘python’) 
    post_dict, post_ids = tweet_creator(subreddit) 
    tweeter(post_dict, post_ids) 

def tweeter(post_dict, post_ids): 
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret) 
    auth.set_access_token(access_token, access_token_secret) 
    api = tweepy.API(auth) 
    for post, post_id in zip(post_dict, post_ids): 
     found = duplicate_check(post_id) 
     if found == 0: 
      print "[bot] Posting this link on twitter" 
      print post.encode('latin-1') + " " + post_dict[post].encode('latin-1') + " #python" 
      api.update_status(post+" "+post_dict[post]+" #python") 
      add_id_to_file(post_id) 
      time.sleep(30) 
     else: 
      print "[bot] Already posted" 

if __name__ == '__main__': 
    main() 

역 추적 :

Traceback (most recent call last): 
File "twitter.py", line 83, in <module> 
main() 
File "twitter.py", line 64, in main 
tweeter(post_dict, post_ids) 
File "twitter.py", line 74, in tweeter 
print post.encode('latin-1') + " " + post_dict[post].encode('latin-1') + " #python" 
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u015f' in position 21:  
ordinal not in range(256) 

참고 : 저는 파이썬 초보자 오전, 나는 그것을 배우려고 노력하고 있습니다.

+0

링크의 어느 부분입니까? "Latin Extended-A"라고하는 부분 은요? –

답변

관련 문제