Ethereum: Reading in the Zip file from URL generates a bad zip file error
============================================ === ==== = =========.
Introduction
————
When you try to download the historical data of cryptocurrencies from [www.data.binance.vision] ( you are confronted with the pd.read_csvmethod. This problem is probably due to how the ZIP files treat URL.
In this article, we examine why the ZIP file containing historical binance data can be considered bad and how to solve this problem with Python.
Why are Bad Zip files?
-----------------
The BAD ZIP file contains an incorrect or inadequate ZIP archive. This can occur when the ZIP file is damaged, incorrectly compressed, or inappropriate signature. In our case, we suspect that the ZIP files served by Binance Servers are incorrect.
The decision
--------
To solve the problem, we need to make sure that the ZIP files downloaded from the Binance server are correct and well formed. One way to do this is to use the question directory to directly download the zip file and pull out its contents using Python.
Here is a code fragment that shows how to achieve this:
Python
Import issues
An imported zipfile
Import pandas as a pd
Def download_data_from_binance (URL):
Answer = Interests.get (URL)
zip_file = zipile.zipile (resperse.content, ‘r’)
Filename zip_file.namelist ():
If not the filename.endswith (‘csv’):
to continue
Check that file contains .zip extension
If not the file name.endswith (‘zip’):
to continue
Pull out the contents of the zip file
Filapath = os.path.jin (os.getcwd (), filename)
open (Filapath, ‘WB’) as f:
For the name zip_file.namelist ():
If not the name.endswith (‘csv’):
to continue
Check that file contains .CSV extension
If not the name.endswith (‘csv’):
to continue
Read the csv file from the zip archive
zipile.zipilee (zip_file, ‘r’) as zip_ref:
zip_ref.extractall (Filapath)
Save the drawn csv data to a temporary file
F.write (zip_file.namelist () [name])
recurry
Check Binance Server URL
URL = “
Download Historical cryptocurrency data from the specified URL
Download_filepath = download_data_from_binance (URL)
If you downloaded_filepath:
Printed (F “Download Successful. These files were extracted:”)
Open (download_filepath, ‘r’) as f:
To the line F:
print (line.strip ())
Otherwise:
Print (“Unable to download historic cryptocurrency data.”).
`
In this codex:
- To download the ZIP file, use Query Library from the Binance Server.
- Then we pull out its contents using “zipfile.zipile”.
- Repeat each file names in the pulled zip archive and check that there is an extension. If not, this step is missing.
- Read the right CSV file from the ZIP archive and temporarily saves the right CSV file for all the names found in the found CSV file.
Keep in mind that this method assumes that the historic cryptocurrency data files are in the main library of the ZIP archive. You may need to change the code if the files are stored elsewhere.
The conclusion
———-
In summary, reading ZIP files from URL can be generated by corruption or inadequate ZIP archives with bad ZIP file defects. With a direct download method and checking the appropriate file extensions, you should be able to download this problem by downloading historic cryptocurrency data from Binance Server using Python.
Leave a Reply