pandas XLRDError:python 中没有名为 <'Sheet1'> 的工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36716242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:04:32  来源:igfitidea点击:

XLRDError: No sheet named <'Sheet1'> in python

python-2.7pandas

提问by user3827728

I am trying to convert the xls into csv file using pandas in python. But I am getting the following error like 'XLRDError: No sheet named <'Sheet1'>'. I have verified the sheet name and it is same as specified above, but I don't how to correct this error. Please find my code below.

我正在尝试使用 python 中的 Pandas 将 xls 转换为 csv 文件。但我收到以下错误,如“XLRDError: No sheet named <'Sheet1'>'。我已经验证了工作表名称,它与上面指定的相同,但我不知道如何更正此错误。请在下面找到我的代码。

CODE:

代码:

 def xls_2_csv():

 import pandas as pd
 data_xls = pd.read_excel(r'c:\delivery\file1.xls','Sheet1', index_col=None)
 data_xls.to_csv(r'C:\test\file1.csv', encoding='utf-8',index=None)

 xls_2_csv()

Please help me in solving this error. Thanks in advance.

请帮我解决这个错误。提前致谢。

回答by user3827728

Hi I tried the following code it worked for me.

嗨,我尝试了以下代码,它对我有用。

CODE:

代码:

import logging
import time
import traceback
import xlrd
import csv
import sys
import re

logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')

xls = input file path
target = output file path

logging.info("Start converting: From '" + xls + "' to '" + target + "'. ")

try:
    start_time = time.time()
    wb = xlrd.open_workbook(xls)
    sh = wb.sheet_by_index(0)

    csvFile = open(target, 'wb')
    wr = csv.writer(csvFile, quoting=csv.QUOTE_ALL)

    for row in xrange(sh.nrows):
        rowValues = sh.row_values(row)

        newValues = []
        for s in rowValues:
            if isinstance(s, unicode):
                strValue = (str(s.encode("utf-8")))
            else:
                strValue = (str(s))

            isInt = bool(re.match("^([0-9]+)\.0$", strValue))

            if isInt:
                strValue = int(float(strValue))
            else:
                isFloat = bool(re.match("^([0-9]+)\.([0-9]+)$", strValue))
                isLong  = bool(re.match("^([0-9]+)\.([0-9]+)e\+([0-9]+)$", strValue))

                if isFloat:
                    strValue = float(strValue)

                if isLong:
                    strValue = int(float(strValue))

            newValues.append(strValue)

        wr.writerow(newValues)

    csvFile.close()

    logging.info("Finished in %s seconds", time.time() - start_time)

except Exception as e:
    print (str(e) + " " +  traceback.format_exc())

回答by u13530253

I found the same problem in python 3.6 and pandas version is 0.25.1.

我在 python 3.6 中发现了同样的问题,pandas 版本是 0.25.1。

The following should work:

以下应该工作:

import pandas as pd
file = 'your excel file path'
# the file is endswith '.xls' and there is multiple sheets

# error method
df_sheet1 = pd.read_excel(file, sheet_name='Sheet1')
df_sheet2 = pd.read_excel(file, sheet_name='Sheet2')
# when read Sheet1 had no error, but when read Sheet2, had an error: 
# xlrd.biffh.XLRDError: No sheet named <'Sheet2'>


# right method 
with pd.ExcelFile(file) as xls:
    for sheet_name in xls.sheet_names:
        df = pd.read_excel(xls, sheet_name=sheet_name)
        print(df.head())