【问题标题】:AttributeError: 'str' object has no attribute 'parse'AttributeError:“str”对象没有属性“parse”
【发布时间】:2017-05-07 06:57:05
【问题描述】:

我正在学习 Python,并且也是第一次尝试使用 pandas。 我有一个目录,其中包含大约 50 个 Excel 工作簿,我正在尝试将其合并为一个。

import openpyxl
import pandas as pd
import numpy as np
import glob
import os
import sys

#path = "\\\\mtrjesmith\\Service Parts Photography Project\\STERISForms"
files = os.listdir("\\\\mtrjesmith\\Service Parts Photography Project\\STERISForms")
outf = "C:\\Python27\\Scripts\\steris_forms\\compiled.xls", "w+b"
#print(files)

frame = [x.parse(x.sheet_names[0], header=None,index_col=None) for x in files]
frame[1:] = [df[1:] for df in frame[1:]]
combined = pd.concat(frame)
combined.to_excel("C:\\Python27\\Scripts\\steris_forms\\compiled.xls", "w+b", header=False, index=False)

我收到以下错误:

Traceback (most recent call last):
File "C:\Python27\Scripts\steris_forms.py", line 18, in <module>
    frame = [x.parse(x.sheet_names[0], header=None,index_col=None) for x in files]
AttributeError: 'str' object has no attribute 'parse'

我能做些什么来解决这个问题?任何其他反馈将不胜感激。

【问题讨论】:

  • 问题在于x.parsex是一个字符串,没有解析方法。
  • 您希望x.parse 做什么?
  • 尝试 pd.ExcelFile.parse
  • 我想知道这一行 -- outf = "C:\\Python27\\Scripts\\steris_forms\\compiled.xls", "w+b"
  • @DineshPundkar 我会检查一下,看看我能不能弄明白。

标签: python excel pandas


【解决方案1】:

你只需要导入 parse 作为 urllib 库的方法:

import urllib.parse

【讨论】:

    【解决方案2】:

    试试这个:

    frame = [pd.read_excel(x, header=None, index_col=None) for x in files]
    

    【讨论】:

    • 解决了这个问题,但现在又得到了另一个:TypeError: unbound method parse() must be called with ExcelFile instance as first argument (get str instance) 在同一行。我认为它将“x”视为一个字符串......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    • 2021-10-04
    • 2019-12-02
    • 2021-09-25
    • 2014-03-04
    • 2013-09-22
    相关资源
    最近更新 更多