AttributeError:'str'对象没有属性'parse'

时间:2016-12-21 18:40:45

标签: python excel pandas

我正在学习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'

我该怎么做才能解决这个问题?任何其他反馈将不胜感激。

2 个答案:

答案 0 :(得分:1)

试试这个:

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

答案 1 :(得分:0)

您只需要将解析作为urllib库的方法导入:

import urllib.parse