user4001386
user4001386

Reputation:

Loop a list and write to excel in Python

Im trying to write to an excel based of a list that I have. The problem is that I cannot get it into a list (A1, A2, A3, A4 etc) in excel, all values is written in one cell.

Any thoughts? I cannot find any good example, is "writing python list to excel" the wrong explanation?

import requests
from xlwt import Workbook

url = 'some website'

r = requests.get(url)
split_text = r.text.split()

exit_address = []
for idx, val in enumerate(split_text):
  if split_text[idx-1] == 'ExitAddress':
    exit_address.append(val)

wb = Workbook()
sheet1 = wb.add_sheet('Sheet 1')
sheet1.write(0,0, exit_address)

wb.save('nodes.xlsx')

Thanks in advance

Upvotes: 0

Views: 2032

Answers (1)

drum
drum

Reputation: 5651

That's because you specified to write the entire list to the first cell:

sheet1.write(0,0, exit_address)

You can try something like this instead:

import requests
from xlwt import Workbook

url = 'some website'

r = requests.get(url)
split_text = r.text.split()

exit_address = []
for idx, val in enumerate(split_text):
  if split_text[idx-1] == 'ExitAddress':
    exit_address.append(val)

wb = Workbook()
sheet1 = wb.add_sheet('Sheet 1')

for i, text in enumerate(exit_address):
    sheet1.write(i, 0, text)

wb.save('nodes.xlsx')

https://xlwt.readthedocs.io/en/latest/api.html

Upvotes: 1

Related Questions