XLRDError: 没有张贴的名字 <'Sheet1'> 在 python

我正在努力转换 xls 到文件。 csv, 使用 pandas 在 python. 但我得到以下类型错误 'XLRDError: 没有张贴的名字 <'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//


请帮助我解决这个错误。 先感谢您。
已邀请:

郭文康

赞同来自:

我发现了同样的问题 python 3.6 和 pandas 版本 0.25.1.

必须工作以下内容:


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///

小姐请别说爱

赞同来自:

嗨,我尝试了以下代码,他为我工作。

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///

要回复问题请先登录注册