[英]For loop writes only one letter into a range of cells using xlsxwriter
I am working on a project to conjugate all the verbs into a single excel file.我正在做一个项目,将所有动词组合成一个 Excel 文件。
I am using this sample website for reference: https://konjugator.reverso.net/konjugation-deutsch-verb-machen.html我正在使用此示例网站作为参考: https ://konjugator.reverso.net/konjugation-deutsch-verb-machen.html
When I run this code:当我运行此代码时:
blueBox = soup.find_all('div', class_='blue-box-wrap')
for i in blueBox[0].find_all('i', class_='verbtxt'):
print(i.text)
It prints:它打印:
mache
machst
macht
machen
macht
machen
But when I run this code in order to write into excel file:但是当我运行此代码以写入 excel 文件时:
from bs4 import BeautifulSoup
import xlsxwriter
import requests
workbook = xlsxwriter.Workbook('Python/Verben/verben.xlsx')
worksheet = workbook.add_worksheet()
html_doc = open('Python/Verben/machen_konjugation.html', 'r', encoding='utf-8')
soup = BeautifulSoup(html_doc, 'html.parser')
blueBox = soup.find_all('div', class_='blue-box-wrap')
for i in blueBox[0].find_all('i', class_='verbtxt'):
for col_num, data in enumerate(i.text):
worksheet.write(1, col_num, data)
workbook.close()
The excel file looks like this: excel文件如下所示:
m
a
c
h
e
n
Basically every cell has one letter from the first iteration.基本上每个单元格都有一个来自第一次迭代的字母。 I can solve this by first writing the loop into a list than write the list into the range of cells, but is there a cleaner solution?
我可以通过首先将循环写入列表而不是将列表写入单元格范围来解决此问题,但是有更清洁的解决方案吗?
Thanks in advance提前致谢
You can replace:您可以更换:
for i in blueBox[0].find_all('i', class_='verbtxt'):
for col_num, data in enumerate(i.text):
worksheet.write(1, col_num, data)
with:和:
for col_num, i in enumerate(blueBox[0].find_all('i', class_='verbtxt')):
worksheet.write(1, col_num, i.text)
basically taking your working code, and replacing print()
with worksheet.write()
and grabbing col_num
info from enumerate()
on the sequence where you find the items to write.基本上采用您的工作代码,并将
print()
替换为worksheet.write()
并从enumerate()
中获取col_num
信息,以查找要写入的项目。
I would recommend using Pandas for data persistence.我建议使用Pandas进行数据持久性。 It is more flexible and allows you to save in various formats, such as cvs, json, etc. So just install:
它更灵活,允许你以各种格式保存,例如 cvs、json 等。所以只需安装:
pip install pandas
pip install openpyxl
Then past your code:然后通过您的代码:
import requests
from bs4 import BeautifulSoup
import pandas as pd
url = 'https://konjugator.reverso.net/konjugation-deutsch-verb-machen.html'
response = requests.get(url)
indicatives = ['Indikativ Präsens', 'Indikativ Präteritum', 'Indikativ Futur I']
soup = BeautifulSoup(response.text, 'lxml')
results = {}
for indicative in indicatives:
html = soup.find('div', {'mobile-title': indicative})
results['Übersetzung im Kontext'] = [verb.getText() for verb in html.find_all('i', class_='graytxt')]
results[indicative] = [verb.getText() for verb in html.find_all('i', class_='verbtxt')]
df = pd.DataFrame(results).set_index('Übersetzung im Kontext')
df.to_excel('Python/Verben/verben.xlsx')
And enjoy result:并享受结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.