2017-05-15 3 views
0

私はSAPアプリケーションから少数の販売データセットをダウンロードしました。 SAPは自動的にデータを.XLSファイルに変換しました。python:壊れたxlsファイルを変換する

私はそれがあるMSEXCELを使用して.xlsファイルを開いた
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '\xff\xfe\r\x00\n\x00\r\x00' 

がポップアップを示して、私は「はい」をクリックしたときfile is corrupt or unsupported extension do you want to continueが正しいデータを示すことを言って:私はそれは私が次のエラーを取得していますPandasライブラリを使用して開くたび。 msexcelを使ってファイルを.xlsとして再度保存したとき、私はPandasを使ってそれを使用することができました。

私はos.rename()を使用してファイルの名前を変更しようとしましたが、それはうまく動作しません。私はファイルを開き、\xff\xfe\r\x00\n\x00\r\x00を削除しようとしましたが、それはまた、dintの作業です。

解決策は、MSEXCELを開いて手動で.xlsとしてファイルを保存することです。これを自動化する方法はありますか。親切に助けてください。

+0

私は質問をチェックして、私の質問は別の形式に変換することです。 – Jeril

+0

@downshiftファイルがMS Excelで開かれていません。 – Jeril

+0

私はMS Excelの 'save-as'に似た何かをしたいが、手動ではしたくない。何か方法はありますか? – Jeril

答えて

0

最後に、破損した.xlsを正しい.xlsファイルに変換しました。コードは次のとおりです。

# Changing the data types of all strings in the module at once 
from __future__ import unicode_literals 
# Used to save the file as excel workbook 
# Need to install this library 
from xlwt import Workbook 
# Used to open to corrupt excel file 
import io 

filename = r'SALEJAN17.xls' 
# Opening the file using 'utf-16' encoding 
file1 = io.open(filename, "r", encoding="utf-16") 
data = file1.readlines() 

# Creating a workbook object 
xldoc = Workbook() 
# Adding a sheet to the workbook object 
sheet = xldoc.add_sheet("Sheet1", cell_overwrite_ok=True) 
# Iterating and saving the data to sheet 
for i, row in enumerate(data): 
    # Two things are done here 
    # Removeing the '\n' which comes while reading the file using io.open 
    # Getting the values after splitting using '\t' 
    for j, val in enumerate(row.replace('\n', '').split('\t')): 
     sheet.write(i, j, val) 

# Saving the file as an excel file 
xldoc.save('myexcel.xls') 

import pandas as pd 
df = pd.ExcelFile('myexcel.xls').parse('Sheet1') 

エラーはありません。

関連する問題