无法写入新的Excel文件

时间:2017-02-28 17:41:13

标签: python-3.x

我是python的新手并且有一个任务要求我:

设计自己的代码,在这里做一些事情,以保存标题,ID,共享计数 和Excel(.xls)文件的分隔列中每个新闻媒体的注释计数。

设计您自己的代码,以便从Excel中读取共享计数和注释计数 在步骤3中创建的文件,并计算平均份额和评论计数 那些新闻媒体网站。

这是我目前的代码:

from urllib import request
import json
from pprint import pprint
import xlwt
'''
import xlrd
from xlutils import copy
'''
website_list = [
                'http://www.huffingtonpost.com/',
                'http://www.cnn.com/',
                'https://www.nytimes.com/',
                'http://www.foxnews.com/',
                'http://www.nbcnews.com/'
                ] # place your list of website urls, e.g., http://jmu.edu

for website in website_list:
    url_str = 'https://graph.facebook.com/'+website # create the url for facebook graph api

    response = request.urlopen(url_str) # read the reponse into computer

    html_str = response.read().decode("utf-8") # convert the reponse into string

    json_data = json.loads(html_str) # convert the string into json
    pprint (json_data) 

book = xlwt.Workbook()
sheet_test = book.add_sheet('keys')

sheet_test.write(0,0,'Title')
sheet_test.write(0,1,'ID')
sheet_test.write(0,2,'Share Count')
sheet_test.write(0,3,'Comment Count')

for i in range(0,5):
    for website in website_list[i]:
        sheet_test.write(i,0,json_data['og_object']['title'])
        sheet_test.write(i,1,json_data['id'])
        sheet_test.write(i,2,json_data['share']['share_count'])
        sheet_test.write(i,3,json_data['share']['comment_count'])

book.save('C:\\Users\\stinesr\\Downloads\\Excel\\keys.xls')
'''
reading_book = xlrd.open_workbook('C:\\Users\\stinesr\\Downloads\\Excel\\key.xls')
sheet_read = reading_book.sheet_by_name('keys')
num_record = sheet_read.nrows
writing_book = copy(reading_book)
sheet_write = writing_book.get_sheet(0)
print(sheet_write.name)
for i in range(num_record):
    row = sheet_read.row_values(i)
    if i == 0:
        sheet_write.write(0,4,'Share Count Average')
        sheet_write.write(0,5,'Comment Count Average')
    else: 
        sheet_write.write(i,4,row[2])
        sheet_write.write(i,5,row[3])
writing_book.save('C:\\Users\\stinesr\\Downloads\\Excel\\keys.xls')    
'''

感谢所有的帮助,谢谢。

1 个答案:

答案 0 :(得分:0)

Traceback错误在lines 40-45上的嵌套for循环中表示您试图覆盖前一行中的row 0。您需要从行1开始,因为行0已包含标题。

但在此之前,json_data仅保留最后一个回复,您需要为list创建append个“回复”和list每个回复。

第40行只需要一个for循环:

总结:

website_list = [
            'http://www.huffingtonpost.com/',
            'http://www.cnn.com/',
            'https://www.nytimes.com/',
            'http://www.foxnews.com/',
            'http://www.nbcnews.com/'
            ] # place your list of website urls, e.g., http://jmu.edu

json_list = []

for website in website_list:
    url_str = 'https://graph.facebook.com/' + website # create the url for facebook graph api

    response = request.urlopen(url_str) # read the reponse into computer

    html_str = response.read().decode("utf-8") # convert the reponse into string

    json_data = json.loads(html_str) # convert the string into json

    json_list.append(json_data)

pprint (json_list)

book = xlwt.Workbook()
sheet_test = book.add_sheet('keys')

sheet_test.write(0,0,'Title')
sheet_test.write(0,1,'ID')
sheet_test.write(0,2,'Share Count')
sheet_test.write(0,3,'Comment Count')

for i in range(len(json_list)):
    sheet_test.write(i+1, 0, json_list[i]['og_object']['title'])
    sheet_test.write(i+1, 1, json_list[i]['id'])
    sheet_test.write(i+1, 2, json_list[i]['share']['share_count'])
    sheet_test.write(i+1, 3, json_list[i]['share']['comment_count'])

book.save('C:\\Users\\stinesr\\Downloads\\Excel\\keys.xls')

应该为您提供类似于:

的Excel文档

excel document output