现在谷歌搜索这个问题已经有一段时间了,但似乎无法找到解决方案。我使用excelfiles-function创建所有excelfiles的列表,包括在指定目录中的名称中的“tst”。之后我想用locate_vals-function读取每个文档中的某些单元格,但我似乎无法从文件列表中重新扫描文件。也许有一个非常简单的解决方案,我只是看不到?我得到的错误消息是在底部。
这是昨天我要求帮助的更大任务的一部分(“Search through directories for specific Excel files and compare data from these files with inputvalues”),但我似乎无法找到这个问题的任何答案我认为它可能最好给它一个自己的线程。如果我错了,请纠正我,我会将其删除:)
import xlrd
import os, fnmatch
#globals
start_dir = 'C:/eclipse/TST-folder'
def excelfiles(pattern):
file_list = []
for root, dirs, files in os.walk(start_dir):
for filename in files:
if fnmatch.fnmatch(filename.lower(), pattern):
if filename.endswith(".xls") or filename.endswith(".xlsx") or filename.endswith(".xlsm"):
file_list.append(os.path.join(root, filename))
return file_list
file_list = excelfiles('*tst*') # only accept docs hwom title includes tst
for i in file_list: print i
'''Location of each val from the excel spreadsheet'''
def locate_vals():
val_list = []
for file in file_list:
wb = xlrd.open_workbook(os.path.join(start_dir, file))
sheet = wb.sheet_by_index(0)
for vals in file:
weightvalue = file_list.sheet.cell(3, 3).value
lenghtvalue = sheet.cell(3, 2).value
speedval = sheet.cell(3, 4).value
ERRORMESSAGE:
Traceback (most recent call last):
File "C:\Users\Håvard\Documents\Skulearbeid\UMB\4. Semester Vår\Inf120 Programmering og databehandling\Workspace\STT\tst_mainsheet.py", line 52, in <module>
print locate_vals()
File "C:\Users\Håvard\Documents\Skulearbeid\UMB\4. Semester Vår\Inf120 Programmering og databehandling\Workspace\STT\tst_mainsheet.py", line 48, in locate_vals
weightvalue = file_list.sheet.cell(3, 3).value
AttributeError: 'list' object has no attribute 'sheet'
答案 0 :(得分:2)
错误表示您需要的一切:
AttributeError:'list'对象没有属性'sheet'
file_list
是文件名列表,list在python中没有sheet
属性。
所以,你只需要替换:
weightvalue = file_list.sheet.cell(3, 3).value
与
weightvalue = sheet.cell(3, 3).value
答案 1 :(得分:2)
您的追溯显示的问题确实是这样:
weightvalue = file_list.sheet.cell(3, 3).value
应该是这样的:
weightvalue = sheet.cell(3, 3).value
但是,您的代码中存在更多问题。我做了一些小修改,并在评论中标记了它们:
import xlrd
import os, fnmatch
start_dir = 'C:/eclipse/TST-folder'
def excelfiles(pattern):
file_list = []
for root, dirs, files in os.walk(start_dir):
for filename in files:
if fnmatch.fnmatch(filename.lower(), pattern):
if filename.endswith(".xls") or filename.endswith(".xlsx") or filename.endswith(".xlsm"):
file_list.append(os.path.join(root, filename))
return file_list
file_list = excelfiles('*tst*') # only accept docs hwom title includes tst
for i in file_list: print i
'''Location of each val from the excel spreadsheet'''
def locate_vals():
val_dict = {}
for filename in file_list:
wb = xlrd.open_workbook(os.path.join(start_dir, filename))
sheet = wb.sheet_by_index(0)
# problem 2: extract these values once per sheet
weightvalue = sheet.cell(3, 3).value
lengthvalue = sheet.cell(3, 2).value
speedvalue = sheet.cell(3, 4).value
# problem 3: store them in a dictionary, keyed on filename
val_dict[filename] = [weightvalue, lengthvalue, speedvalue]
# dictionary keyed on filename, with value a list of the extracted vals
return val_dict
print locate_vals()
答案 2 :(得分:1)
变化:
weightvalue = file_list.sheet.cell(3, 3).value
为:
weightvalue = sheet.cell(3, 3).value