我正在尝试将信息从Twits(screen_name,created_at和text)保存到pandas DataFrame中,然后将DataFrame保存为csv文件。
我收到编码错误
import tweepy
from tweepy import OAuthHandler
consumer_key = 'bla'
consumer_secret = 'bla'
access_token = 'bla'
access_secret = 'bla'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
import pandas as pd
import numpy as np
import datetime
import sys
encoding = sys.stdout.encoding or 'utf-8'
columns = ['Screen_Name', 'Time_Stamp', 'Tweet']
todays_date = datetime.datetime.now().date()
tweetDF = pd.DataFrame(columns=columns)
for tweet in tweepy.Cursor(api.search, q="manhattan", lang="en").items(10):
lenDF = len(tweetDF)
tweetDF.loc[lenDF] = [tweet.user.screen_name, tweet.created_at, tweet.text]
tweetDF.to_csv("C:/tweetDF")
这里的错误:
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-11-c0aa5e7ee620> in <module>()
---> 34 tweetDF.to_csv("C:/tweetDF")
C:\Anaconda\lib\site-packages\pandas\core\frame.pyc in to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, quoting, quotechar, line_terminator, chunksize, tupleize_cols, date_format, doublequote, escapechar, decimal, **kwds)
1187 escapechar=escapechar,
1188 decimal=decimal)
-> 1189 formatter.save()
1190
1191 if path_or_buf is None:
C:\Anaconda\lib\site-packages\pandas\core\format.pyc in save(self)
1465
1466 else:
-> 1467 self._save()
1468
1469 finally:
C:\Anaconda\lib\site-packages\pandas\core\format.pyc in _save(self)
1565 break
1566
-> 1567 self._save_chunk(start_i, end_i)
1568
1569 def _save_chunk(self, start_i, end_i):
C:\Anaconda\lib\site-packages\pandas\core\format.pyc in _save_chunk(self, start_i, end_i)
1592 quoting=self.quoting)
1593
-> 1594 lib.write_csv_rows(self.data, ix, self.nlevels, self.cols, self.writer)
1595
1596 # from collections import namedtuple
pandas\lib.pyx in pandas.lib.write_csv_rows (pandas\lib.c:17992)()
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2666' in position 7: ordinal not in range(128)
我尝试了各种编码增强功能,但未成功
答案 0 :(得分:6)
我找到了解决问题的方法并希望分享:
radiobutton .t.r1 -text "Default" -variable reptype -value 0
radiobutton .t.r2 -text "Custom" -variable reptype -value 1
.t.r1 select
place .t.r1 -x 20 -y 30
place .t.r2 -x 20 -y 50
entry .t.ecustom -width 70
place .t.ecustom -x 100 -y 50
if { $reptype == 0 } {
.t.ecustom configure -state normal
} elseif { $reptype == 1 } {
.t.ecustom configure -state disabled
}