简体   繁体   English

为什么变量有效,但没有在python中使用xlrd打开工作簿的列表?

[英]Why does a variable work, but not a list for opening a workbook using xlrd in python?

I'm extremely new to python so I appreciate any help. 我对python非常陌生,所以我感谢任何帮助。 I've been attacking this from every angle I can imagine for the last 4 days in hopes I could figure it out myself, but I'm stumped. 从过去4天我能想到的每一个角度来看,我一直在攻击这个,希望我能自己弄明白,但我很难过。

I'm trying to create a list of files from a specific directory with the extension .xlsx. 我正在尝试使用扩展名.xlsx从特定目录创建文件列表。 Then I want to take those file names from the list and run some function, passing the discovered file name into the function, and iterating it with each file name. 然后我想从列表中获取这些文件名并运行一些函数,将发现的文件名传递给函数,并使用每个文件名进行迭代。 I've tried to do this in multiple ways, but for some reason I it won't accept the file names. 我试图以多种方式做到这一点,但由于某种原因我不接受文件名。

import os
import xlrd

mypath = "some path to files"
fileslist = []

def find_files():
    for file in os.listdir(mypath):
        if file.endswith(".xlsx")
            fileslist.append(file)

def other_function():    
    book = xlrd.open_workbook(fileslist)

I can print fileslist and show that it's populated with the correct info. 我可以打印fileslist并显示它填充了正确的信息。 I've done this in both the main part of the script and also within the other_function() area. 我已经在脚本的主要部分和other_function()区域内完成了这项工作。 I also tried testing out naming a variable fileslist with a valid file name in the directory, let's say "file1.xlsx" and that works. 我也试过在目录中测试一个带有有效文件名的变量fileslist,让我们说“file1.xlsx”,这样可行。 Once I input it into a list, even if the only entry in the list is "file1.xlsx" I get the following error 一旦我将其输入到列表中,即使列表中的唯一条目是“file1.xlsx”,我也会收到以下错误

book = xlrd.open_workbook(fileslist)
  File "/Library/Python/2.7/site-packages/xlrd/__init__.py", line 110, in open_workbook
    filename = os.path.expanduser(filename)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 254, in expanduser
    if not path.startswith('~'):
AttributeError: 'list' object has no attribute 'startswith'

xlrd is looking for a file path to open. xlrd正在寻找要打开的文件路径。 A list (of files) is 1) not a filepath, but multiple file paths and 2) not a string, which is the specific error you're getting. 列表(文件)是1)不是文件路径,而是多个文件路径和2)不是字符串,这是您获得的特定错误。 A string (which is a Python object) has a method .startswith which allows xlrd to check if the first part of the filepath (you're supposed to give open_workbook ) is a ~ or not. 一个字符串(它是一个Python对象)有一个方法.startswith允许xlrd检查文件路径的第一部分(你应该给open_workbook )是否是一个~ xlrd probably did this because that would affect where it looks for the file. xlrd可能这样做是因为这会影响它查找文件的位置。

xlrd.open_workbook is essentially trying to double click on the filepath you send it, you are (essentially) trying to click on all of the files in your list at the same time, which might be possible if you could have X different computer mice with a hand for each, but isn't really possible the way computers are typically built. xlrd.open_workbook本质上是试图双击你发送它的文件路径,你(基本上)试图同时点击列表中的所有文件,如果你可以拥有X个不同的计算机鼠标,这可能是可能的每个人的手,但通常不可能像计算机通常建立的方式。

If you want to make a dictionary of the different workbooks you have, but opened with xlrd you can use this: 如果要创建不同工作簿的字典,但使用xlrd打开,可以使用:

xlrd_wbs = dict()
for my_file in filelist:
    xlrd_wbs[my_file] = xlrd.open_workbook(my_file)

and then access the different files with: 然后使用以下命令访问不同的文件:

xlrd_wbs[whatever_file_path_you_used]

I would use a dictionary here because it allows you to access which file you want more reliably, if you just want a list though, you can do this: 我会在这里使用字典,因为它允许你更可靠地访问你想要的文件,如果你只想要一个列表,你可以这样做:

xlrd_wbs = []
for my_file in filelist:
    xlrd_wbs.append(xlrd.open_workbook(my_file))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM