简体   繁体   English

循环列表并在Python中写入Excel

[英]Loop a list and write to excel in Python

Im trying to write to an excel based of a list that I have. 我正在尝试根据我拥有的列表来写一个excel。 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. 问题是我无法将其放入excel的列表(A1,A2,A3,A4等)中,所有值都写入一个单元格中。

Any thoughts? 有什么想法吗? I cannot find any good example, is "writing python list to excel" the wrong explanation? 我找不到任何好的例子,“编写python列表以表现出色”是错误的解释吗?

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 提前致谢

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 https://xlwt.readthedocs.io/en/latest/api.html

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

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