0

I'm trying to unzip a gunzip file in to .csv file , I tried different methods, still it is not getting the result i want, if any one make me understand how it is being unzipped in back end, that would be a great help.

I also tried this code:

csv_file = open("xyz.csv", 'wb')
writer = csv.DictWriter(csv_file, fieldnames=df.columns.to_list())
writer.writeheader()
writer.writerows(df.to_dict())
csv_file.close()

this above code is throwing bad zip file error
Rakesh D
  • 1
  • 1
  • Are you looking to read from the zip file or write to it? – JonSG Sep 01 '21 at 18:47
  • @JonSG first i'm reading so that i will create dataframe, with that i can create a csv file. main aim is here to unzip the csv file from it. In second approach i tried to create it using csv module. – Rakesh D Sep 02 '21 at 08:12
  • Ah, so your main objective is to unzip an existing zip file. If you have an existing OS utility, one easy way would be to use `subprocess.call()`. If you want to do this all in python, check out https://stackoverflow.com/questions/31028815/how-to-unzip-gz-file-using-python – JonSG Sep 02 '21 at 13:34
  • Please provide the first 30 bytes of the file in hexadecimal, so we can see what it really is. – Mark Adler Sep 02 '21 at 15:50
  • guys thanks for your support, it worked with this code: went ahead read properly pandas documentation, df = pd.read_csv('xyz.cxv.gz', compression='gzip', header=0, sep='|', quotechar='"') df.to_csv("xyz.csv",mode='w', header=True,index=False, sep='|', quotechar='"') – Rakesh D Sep 07 '21 at 06:43

0 Answers0