首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Dataframe不输出到Excel

Dataframe不输出到Excel
EN

Stack Overflow用户
提问于 2018-04-11 18:18:56
回答 1查看 57关注 0票数 1

我正在发布我的代码片段,以尝试调试。我正尝试将df4输出到Excel中,但输出中总是出现空白。我已经试着自己解决了几个小时的问题,但还是不能解决问题。

代码语言:javascript
复制
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文件的示例数据

代码语言:javascript
复制
<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

EN

回答 1

Stack Overflow用户

发布于 2018-04-11 19:10:45

试试这个:

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49772495

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档