简体   繁体   English

xlrd:想要读取多个 xl 文件的表格并存储在一个列表/数组中? (更好的方法?)

[英]xlrd: want to read sheets of several xl files and store in one list/array? (better way?)

I don't have much xp with xlrd/xlwt but I have managed to access one of the files I want to collect data from.我没有太多 xlrd/xlwt 的 xp,但我设法访问了我想从中收集数据的文件之一。 I want to collect data from all files in the directory and move it to one sheet.我想从目录中的所有文件中收集数据并将其移动到一张纸上。 I was thinking if there is someway I can store it all in one array/list it would be easy to output to a csv.我在想如果有什么办法可以将它们全部存储在一个数组/列表中,那么将 output 转换为 csv 会很容易。 If this is too much work and there is a simple way plz help, otherwise I am using Idle to play around with ideas and have come up with this so far:如果这是太多的工作并且有一个简单的方法请帮助,否则我正在使用空闲来玩弄想法并且到目前为止已经想出了这个:

>>> import xlrd, xlwt
>>> book = xlrd.open_workbook('c:\excelTry\Papineau.csv.xls')
>>> book.sheet_names()
[u'Charge Codes', u'Month']
>>> sh = book.sheet_by_index(1)
>>> #produces:
>>> sh.book
<xlrd.Book object at 0x01213BF0>
>>> for x in range(0, 10):
        sh.row_values(x)
[u'William Papineau', u'Pay Period 11', '', '', u' ', u' ', '', '', '', u'Weekly Total', '', '', u' ', '', '', '', '', u'Weekly Total', u'Biweekly', u'Percent of Effort']
[u'Index Number', u'Index Description', 40678.0, 40679.0, 40680.0, 40681.0, 40682.0, 40683.0, 40684.0, '', 40685.0, 40686.0, 40687.0, 40688.0, 40689.0, 40690.0, 40691.0, '', u'Total', '']
[u'E45776', u'Seat Belt Study', '', 8.0, 8.0, 8.0, 8.0, u' ', '', 32.0, '', '', '', '', '', u' ', '', 0.0, 32.0, 0.4155844155844156]
[u'E43457', u'MultiScaleWaterQuality', '', '', '', '', '', 8.0, '', 8.0, '', 5.0, 8.0, u' ', '', '', '', 13.0, 21.0, 0.2727272727272727]
[u'E45125', u'GLOSS', '', '', '', '', '', '', '', 0.0, '', '', '', 8.0, 8.0, '', '', 16.0, 16.0, 0.2077922077922078]
[u'E45131', u'GLOS AOC Trib Monitoring', '', '', '', '', '', '', '', 0.0, '', '', '', '', '', 8.0, '', 8.0, 8.0, 0.1038961038961039]

this produces what looks like a list object but every attempt I have made to manipulate or append it produces errors saying not scriptable or iterable.这会产生看起来像列表 object 但我所做的每一次操作尝试或 append 它都会产生错误,指出不可编写脚本或可迭代。 The file iteration will be handled with the os module using os.listdir(path) and a for loop.文件迭代将通过 os 模块使用 os.listdir(path) 和 for 循环来处理。 Any help would be greatly appreciated!任何帮助将不胜感激!

So far in your code you don't appear to be doing anything with the values you get from the worksheet.到目前为止,在您的代码中,您似乎没有对从工作表中获得的值做任何事情。 Maybe some of the code didn't get pasted into the question...也许有些代码没有粘贴到问题中......

Would you be able to include the output of that last line of code?你能包含最后一行代码的 output 吗?

You say that you want to store it all in one list.你说你想把它全部存储在一个列表中。
Try something like this:尝试这样的事情:

final = []
for rowx in xrange(sh.nrows):
    final.extend(sh.row_values(rowx))

Also:还:
Be careful with Windows paths.小心 Windows 路径。 Single-backslashes will work only if the following letter does not, with the backslash, form an escape sequence (eg \t or tab).单反斜杠仅在以下字母与反斜杠不构成转义序列(例如\t或制表符)时才有效。 Other options (option 3 is probably best; unless there is a specific reason not to use it):其他选项(选项 3 可能是最好的;除非有特定原因不使用它):

  1. Raw strings: book = xlrd.open_workbook(r'c:\excelTry\Papineau.csv.xls')原始字符串: book = xlrd.open_workbook(r'c:\excelTry\Papineau.csv.xls')
  2. Forward-slashes: book = xlrd.open_workbook('c:/excelTry/Papineau.csv.xls')正斜杠: book = xlrd.open_workbook('c:/excelTry/Papineau.csv.xls')
  3. os.path.join : os.path.join
    book = xlrd.open_workbook(os.path.join('c:','excelTry','Papineau.csv.xls'))
data = []
for i in xrange(sh.nrows):
    data.append(sh.row_values(i))
it will append each rows from xls file into list "data".
eg: [['a','b'],['c','d'],['e','f']] like this .

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

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