“无法写入csv文件”
import simplejson as json
from urllib.request import urlopen
import sys
import time
import csv
import os
import io
os.chdir(r'C:\Users\adity\Desktop\data science')
csvFile =open('test1.csv',"w")
csvFile.write("comments\n")
STAGGER_TIME = 1
打开网址和屏幕名称(网页名称是要返回结果的用户的网页名称)
url = "https://www.googleapis.com/youtube/v3/commentThreads?key=AIzaSyCYkTUjKgFGcKDnkNQMgSBbb4obnqIzUEM&textFormat=plainText&part=snippet&videoId=Ye8mB6VsUHw&maxResults=20"
这将获取一个python对象并将其转储到一个JSON
字符串url1=urlopen(url)
#data = json.load(urllib2.urlopen(url))
result = json.load(url1)
# print the result
itemList= result.get("items")
length=len(itemList)
for i in range(0,length):
results= result["items"][i].get('snippet').get("topLevelComment").get('snippet').get("textDisplay")
print(results)
results=results.replace(",", "")
#print (result["items"][i].get('snippet').get("topLevelComment").get('snippet').get("textDisplay")).encode("utf-8")
#writer.writerow((result["items"][i].get('snippet').get("topLevelComment").get('snippet').get("textDisplay")).encode("utf-8"))
csvFile.write(results)
csvFile.write('\n')
time.sleep(STAGGER_TIME)
csvFile.close()
“得到错误”
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-4-cbf9fd267c8a> in <module>()
28 print(results)
29 results=results.replace(",", "")
---> 30 csvFile.write(results)
31 csvFile.write('\n')
32 time.sleep(STAGGER_TIME)
~\Anaconda3\lib\encodings\cp1252.py in encode(self, input, final)
17 class IncrementalEncoder(codecs.IncrementalEncoder):
18 def encode(self, input, final=False):
---> 19 return codecs.charmap_encode(input,self.errors,encoding_table)[0]
20
21 class IncrementalDecoder(codecs.IncrementalDecoder):
UnicodeEncodeError: 'charmap' codec can't encode characters in position 20-23: character maps to <undefined>
.encode(utf-8)给出了一个typerror
results= result["items"][i].get('snippet').get("topLevelComment").get('snippet').get("textDisplay").encode(utf-8) did help as after using it as it gives
34 results=results.replace(",", "")
TypeError: a bytes-like object is required, not 'str'