我发布了我的代码片段以尝试调试。我正在尝试将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>
OUPUT需要
答案 0 :(得分:1)
试试这个:
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)