Gabriel Gabriel - 1 year ago 62
Python Question

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.

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.

The zipped lists are of equal length (I checked using
, and the other items are not empty (also checked using
). What have I done wrong?

Answer Source

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.

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

I did not see = +1 at the first time. But the value of row never changes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download