已经搜索这个问题已经有一段时间了,但是似乎找不到解决方案。我使用excelfiles函数创建所有excelfile的列表,包括在指定目录中名称中的“ tst”。之后,我想使用locate_vals函数从每个文档中读取某些单元格,但似乎无法从文件列表中读取文件。也许有一个真正简单的解决方案,我只是看不到?我收到的错误消息在底部。

这是我昨天寻求帮助的一项较大任务的一部分(“ 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


错误信息:

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'

最佳答案

您的回溯显示的问题确实是这样的:

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()

关于python - 从文件列表中读取文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17292001/

10-14 17:58
查看更多