简体   繁体   English

为什么只将列表中的第一项和最后一项写入文件?

[英]Why are only the first and last items of my list being written to file?

I've written my very first webscraper, and I am now attempting to write the data to excel files. 我已经写了我的第一个网络爬虫,现在正尝试将数据写入excel文件。

This is the relevant part of my program: 这是我程序的相关部分:

with xlsxwriter.Workbook('test 2.xlsx') as workbook:
    worksheet = workbook.add_worksheet("Doctissimo's diabetes sub-forum")

    row = 0
    col = 0

    for time, handle, post, URL in list(zip(time_stamps, handles, post_contents, URLs)):

       print(time, handle, URL)

       worksheet.write(row, col, time)
       worksheet.write(row, col+1, handle)
       worksheet.write(row, col+2, post)
       worksheet.write(row, col+3, URL)

       row = +1

My problem is that only the first and last item of each list (time_stamps, handles, post_contents, URLs) are being written to file. 我的问题是每个列表中只有第一和最后一项(time_stamps,handles,post_contents,URL)被写入文件。

The zipped lists are of equal length (I checked using print(len(list)) , and the other items are not empty (also checked using print ). What have I done wrong? 压缩列表的长度相等(我使用print(len(list))检查了其他项目,但其他项目也不为空(也使用print检查了)。我做错了什么?

One way to systematically find some bug is to use the print statement, row are supposed to be increased by 1 every loop, if you put a print statement after row, you should be able to see what is wrong. 一种系统地查找错误的方法是使用print语句,应该将行在每个循环中增加1,如果将print语句放在行之后,您应该能够看到问题所在。

row = +1
print "row = %d" % row

I did not see = +1 at the first time. 我没有第一次看到= +1。 But the value of row never changes. 但是row的值永远不会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM