1

一直在谷歌上搜索这个问题很长一段时间,但似乎找不到解决方案。我使用 excelfiles 函数在指定目录中创建所有 excelfiles 的列表,包括名称中的“tst”。之后,我想使用 locate_vals 函数从每个文档中读取某些单元格,但我似乎无法从文件列表中读取文件。也许有一个我看不到的非常简单的解决方案?我得到的错误信息在底部。

这是我昨天寻求帮助的一项更大任务的一部分(在目录中搜索特定的 Excel 文件并将这些文件中的数据与输入值进行比较),但由于我似乎找不到这个问题的任何答案我想最好给它一个自己的线程。如果我错了,请纠正我,我会删除它:)

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

错误信息:

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'
4

3 回答 3

2

该错误说明了您需要的一切:

AttributeError:“列表”对象没有属性“表”

file_list是文件名列表,列表在 python 中没有sheet属性。

所以,你只需要更换:

weightvalue = file_list.sheet.cell(3, 3).value

weightvalue = sheet.cell(3, 3).value
于 2013-06-25T07:59:37.317 回答
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()
于 2013-06-25T08:00:54.590 回答
1

改变:

weightvalue = file_list.sheet.cell(3, 3).value

至:

weightvalue = sheet.cell(3, 3).value
于 2013-06-25T08:01:14.740 回答