2016-04-19 12 views
0

xlsをpythonでpandasを使ってcsvファイルに変換しようとしています。しかし、私は 'XLRDErrorのような次のエラーが発生しています:Sheet1'> 'という名前の<シートはありません。シート名を確認しましたが、上記と同じですが、このエラーを修正する方法はありません。以下の私のコードを見つけてください。XLRDError:シート名が<'Sheet1'>で、Pythonで

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() 

このエラーを解決するために私を助けてください。前もって感謝します。

+1

複数のシートがありますか? 'sheet1' - ' data_xls = pd.read_excel(r'c:\ delivery \ file1.xls '、index_col = None) ' – jezrael

+0

パンダのどのバージョンを使用すればいいですか? 'print pd.show_versions()' – jezrael

+0

@jezrael:返信ありがとう、私はパンダを使用しています:0.14.1。シートが1枚しかなく、シート名を削除すると次のエラーが表示されます。 "***いいえCODEPAGEレコード、encoding_overrideなし: 'ascii'を使用します。 – user3827728

答えて

0

こんにちは私はそれが私のために働いた次のコードを試してみました。

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()) 
関連する問題