2017-02-11 2 views
2

J'ai recherché haut et bas pour une réponse et il ne semble pas y avoir de solution définitive. Ici va:XML vers XLSX en Python

from selenium import webdriver 

chromedriver_path = ("localchromedrive/chromedriver.exe") 
chromeOptions = webdriver.ChromeOptions() 
MSCI_dir = ("mylocaldrive") 
prefs = {"download.default_directory" : MSCI_dir} 
chromeOptions.add_experimental_option("prefs", prefs) 
driver = webdriver.Chrome(chromedriver_path,chrome_options=chromeOptions) 
url = "https://www.ishares.com/us/239637/fund-download.dl" 
driver.get(url) 

Le fichier est maintenant téléchargé dans un chemin d'accès local et enregistré comme suit:

temp_path = "mylocaldrive\iShares-MSCI-Emerging-Markets-ETF_fund.xls" 

Ce fichier est enregistré en tant que type de fichier « .xls », mais il est clairement un XML fichier. Voir ci-dessous pour le fichier ouvert dans NotePad. enter image description here

J'ai essayé xlrd:

import xlrd 
book = xlrd.open_workbook(temp_path) 
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\xef\xbb\xbf<?xml' 

J'ai essayé xml.etree:

import xml.etree.ElementTree as ET 
tree = ET.parse(temp_path) 
File "<string>", line unknown 
ParseError: mismatched tag: line 16, column 2` 

J'ai essayé xlwings:

wb = xw.Book(temp_path) 
wb.save(xlsx_path) 
wb.close()` 

qui ressemble à ce fonctionne, mais quand j'essaie d'utiliser des pandas, je reçois ceci:

pd.read_excel(xlsx_path) 
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'\xef\xbb\xbf<?xml'` 

J'ai essayé BeautifulSoup

from bs4 import BeautifulSoup` 
soup = BeautifulSoup(open(temp_path), "xml")` 

In [1]: soup 
Out[1]: <?xml version="1.0" encoding="utf-8"?>` 

In [2]: soup.contents 
Out[2]: []` 

In [3]: soup.get_text() 
Out[3]: ''` 

Je cherche la façon définitive d'accéder à ce fichier avec pandas géants. Faites-moi savoir de quelle information vous avez besoin de moi que j'ai manqué.

+0

a couru dans le même problème. À la fin, j'ai dû lire le fichier en tant que fichier XML et reconstruire le fichier XML dans un fichier xlsx. Vous devriez consulter ce post: http://stackoverflow.com/questions/36387312/how-to-read-excel-xml-file-in-python –

Répondre