gpt4 book ai didi

python - 数据框未输出到 Excel

转载 作者:行者123 更新时间:2023-12-01 09:33:13 25 4
gpt4 key购买 nike

我发布了一段代码来尝试调试。我正在尝试将 df4 输出到 Excel,但输出中始终显示空白。我已经尝试自己排除故障几个小时,但无法找出问题所在。

from bs4 import BeautifulSoup
import os
import glob
import pandas as pd

os.chdir('C:/Users/PCTR261010/Desktop/Attribute Convert')
FileList = glob.glob('*.txt')

for fname in FileList:
soup = BeautifulSoup(open(fname), 'html.parser')

data = soup.find('data').text

for partno in data.split('\n'):
df4 = pd.DataFrame([partno.split('\t')])

sheet_name = ('MPCC@' + soup.find('leafname').text)
writer = pd.ExcelWriter(os.path.join(fname + '.xlsx'), engine='xlsxwriter')
df4.to_excel(writer, sheet_name=sheet_name, startrow=0, startcol=0, index=False, header=False)

writer.save()

sudo XML 文件的示例数据

<DATA>
PF0W20DEXQT Yes NAPA Proformer 1 qt 0W20 Synthetic SN ILSAC GF-5 Dexos 1 Gen 2 NAPA PROFORMER 0W20 SYNTHETIC DEXOS® MOTOR OIL is a superiorquality synthetic motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
PF0W20SYQT Yes NAPA Proformer 1 qt 0W20 Synthetic SN ILSAC GF-5 NAPA PROFORMER 0W20 SYNTHETIC MOTOR OIL is a superior?quality synthetic motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
PF10W30COQT NAPA Proformer 1 qt 10W30 Conventional SN ILSAC GF-5 NAPA PROFORMER 10W30 MOTOR OIL is a premiumquality conventional motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
PF10W30HDBQT Yes NAPA Proformer 1 qt 10W30 Synthetic Blend CK-4 NAPANAPA PROFORMER 10W30 SYNTHETIC BLEND DIESEL MOTOR OIL is a superiorquality synthetic blend diesel motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
PF10W30HDQT NAPA Proformer 1 qt 10W30 Conventional CJ-4 / SN NAPA PROFORMER 10W30 DIESEL MOTOR OIL is a premiumquality conventional diesel motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
PF15W40HDQT NAPA Proformer 1 qt 15W40 Conventional CK-4 / SN NAPA PROFORMER 15W40 DIESEL MOTOR OIL is a premiumquality conventional diesel motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
PF5W20COQT NAPA Proformer 1 qt 5W20 Conventional SN ILSAC GF-5 NAPA PROFORMER 5W20 MOTOR OIL is a premiumquality conventional motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
PF5W20SYQT Yes NAPA Proformer 1 qt 5W20 Synthetic SN ILSAC GF-5 NAPA PROFORMER 5W20 SYNTHETIC MOTOR OIL is a superior?quality synthetic motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
PF5W30COQT NAPA Proformer 1 qt 5W30 Conventional SN ILSAC GF-5 NAPA PROFORMER 5W30 MOTOR OIL is a premiumquality conventional motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
PF5W30DEXQT Yes NAPA Proformer 1 qt 5W30 Synthetic SN ILSAC GF-5 Dexos 1 Gen 2 NAPA PROFORMER 5W30 SYNTHETIC DEXOS® MOTOR OIL is a superiorquality synthetic motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
PF5W30SYQT Yes NAPA Proformer 1 qt 5W30 Synthetic SN ILSAC GF-5 NAPA PROFORMER 5W30 SYNTHETIC MOTOR OIL is a superior?quality synthetic motor oil specially formulated from 100% pure virgin base oil and highperformance additives. Bottle
</DATA>

需要输出

Output Image

enter image description here

最佳答案

试试这个:

def f(fname):
soup = BeautifulSoup(open(fname), 'html.parser')
data = soup.find('data').text
sheet_name = ('MPCC@' + soup.find('leafname').text)

df = pd.read_csv(io.StringIO(data), sep='\t', header=None)
df.to_excel((os.path.join(fname + '.xlsx'), sheet_name=sheet_name, index=False, header=False)

# process all files
for fname in FileList:
f(fname)

关于python - 数据框未输出到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49772495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com