Ethereum: Reading ZIP file from Url generates Bad Zip File error

Ethereum: Reading ZIP file from URL generates an inappropriate zip file error

============================================== ======== =========

Introduction

————-

When you try to download historical crypto data from [www.data.binance.vision] ( meet an error when reading ZIP files using the pd.red_csv method. This problem is probably due to how ZIP files are managed by URL.

In this article, we will explore why the ZIP file containing historic data from Binance could be considered bad and how to solve this problem using Python.

Why bad zip files?

—————

A bad zip file contains an incorrect or malformed zip archive. This can happen when the ZIP file is corrupt, not properly compressed or has an invalid signature. In our case, we suspect that ZIP files are served by Binance’s servers are defective.

Solution

——

To solve this problem, we must make sure that the ZIP files are downloaded from the Binance server are correct and well formed. One way to do this is to use the “Requests” library to download the ZIP file directly and then extract its contents using Python.

Here is an example of a code fragment that demonstrates how to achieve this:

`Python

Import requests

Import Zipfile

imports the panda as pd

Def Download_Data_from_binance (URL):

Reply = Requests.Get (URL)

zip_file = zipfile.zipfile (respond.content, 'r')

For file names in zip_file.namelist ():

If not file name.endswith ('. csv'):

continue

Ethereum: Reading ZIP file from Url generates Bad Zip File error

Check if the file has a .zip extension

If not file name.endswith ('. zip'):

continue

Extract the contents of the ZIP file

Filepath = os.patch.join (os.Getcwd (), file name)

with open (filepath, "wb") as f:

For names in zip_file.Namelist ():

If not name.endswith ('. csv'):

continue

Check if the file has a .csv extension

If not name.endswith ('. csv'):

continue

Read the CSV file from the ZIP archive

with zipfile.zipfile (zip_file, 'r') as zip_ref:

Zip_ref.extraCall (filepath)

Save CSV data extracted in a temporary file

F.write (zip_file.namelist () [name])

Turn the file

Specify the URL of the Binance server

URL = "

Download crypto -historical data from the specified URL

download_filepath = download_data_from_binance (URL)

If it is downloaded_filepath:

Print (F "Download success. The following files were extracted:")

with open (download_filepath, 'r') as f:

for the line in f:

print (line.strip ())

Otherwise:

Printing ("Failed to download crypto -historical data.")

In this code fragment:

  • We use theCerimesslibrary to download the ZIP file from Binance's server.

  • Then we extract its contents using a zipfile.zipfile.

  • We tear through each file name in the extracted ZIP archive and check if it has a .csv` extension. If not, we omit this step.

  • For each CSV file name found, read the appropriate CSV file from the ZIP archive and save it in a temporary location.

Note that this approach assumes that crypto -historical data files are located in the root director of the ZIP archive. You may need to adjust the code if your files are stored elsewhere.

Conclusion

———-

In conclusion, reading ZIP files from URLs can generate bad ZIP file errors due to malformed corruption or zip archives. Using a direct download approach and checking the correct file extensions, you should be able to solve this problem when downloading the historic crypto data from the Binance server using Python.

Ethereum Find Senders Hash

Leave a Comment

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
Compare