简体   繁体   中英

xlrd in python doesn't read file as it's seen in excel

I have a couple of files which look the same when opened in Excel, but only some can be read properly with xlrd. Here's the code:

import xlrd
book = xlrd.open_workbook(path)
sheet = book.sheet_by_index(0) # only 1 sheet in the file
for i in range(0, sheet.nrows):
    line = sheet.row_values(i)
    print line
    ...

For the good files, this is printed:

[u'ATP', u'Location', u'Tournament', u'Date', u'Series', u'Court', u'Surface', u'Round', u'Best of', u'Winner', u'Loser', u'WRank', u'LRank', u'WPts', u'LPts', u'W1', u'L1', u'W2', u'L2', u'W3', u'L3', u'W4', u'L4', u'W5', u'L5', u'Wsets', u'Lsets', u'Comment', u'B365W', u'B365L', u'EXW', u'EXL', u'LBW', u'LBL', u'SJW', u'SJL', u'UBW', u'UBL']

for bad ones:

[u'ATP', u'Start Date', u'Tournament', u'Venue', u'Location', u'Series', u'Court', u'Surface', u'Players']

Again, lines look exactly the same in excel, but read different in python (first output is correct). Is there a way to read it properly?

Solution: copy-paste everything into a new blank file and read from there, for some reason this works.

Observations: There are only 9 column headings in the "bad" data. Three of these (Start Date, Venue, and Players) do not occur in the "good" data.

Suggestion: Each "bad" file contains TWO sheets, the first being "bad" and the second being "good". Excel would be expected to show the active ("good") sheet.

If this doesn't explain your problem, consider making sample files available for inspection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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