Search code examples
pythoncsvzip

How to read from a csv file in zip folder and save data from csv file in database?


  import glob
  import os
  import csv
  import zipfile 
  from io import StringIO
  for name in glob.glob('C:/Users/RAMESH SANTHA/Downloads/download-NIFTY 50-01012020.zip'):
      base = os.path.basename(name)
      filename = os.path.splitext(base)[0]

  datadirectory = 'C:/Users/RAMESH SANTHA/Downloads/'
  dataFile = filename
  archive = '.'.join([dataFile, 'zip'])
  fullpath = ''.join([datadirectory, archive])   
  csv_file = '.'.join([dataFile, 'csv']) #all fixed

  filehandle = open(fullpath, 'rb')
  zfile = zipfile.ZipFile(filehandle)
  data = StringIO.StringIO(zfile.read(csv_file)) 
  reader = csv.reader(data)
  for row in reader:   
     print (row)

I tried following code to read data from zip folder which contains csv file and print rows but got error:

data = StringIO.StringIO(zfile.read(csv_file)) 

AttributeError: type object '_io.StringIO' has no attribute 'StringIO'

Solution

  • There is no StringIO.StringIO() but io.StringIO()

    import io
    
    data = io.StringIO(...)
    

    With your import it will be even without io.

    from io import StringIO
    
    data = StringIO(...)
    

    BTW: I think you overcomplicated code using glob and join(). And you can use filename directly with ZipFile without open()

    import os
    import csv
    import zipfile 
    import io
    
    zip_fullname = 'C:/Users/RAMESH SANTHA/Downloads/download-NIFTY 50-01012020.zip'
    zip_file = os.path.basename(zip_fullname)
    csv_file = zip_file.replace('.zip', '.csv')
    
    print(zip_file)  # download-NIFTY 50-01012020.zip
    print(csv_file)  # download-NIFTY 50-01012020.csv
    
    zfile = zipfile.ZipFile(zip_fullname)
    data = io.StringIO(zfile.read(csv_file).decode('utf-8')) # bytes needs to be converted to string
    reader = csv.reader(data)
    for row in reader:   
        print(row)
    

    But with pandas it should be even simpler

    import pandas as pd
    
    df = pd.read_csv('C:/Users/RAMESH SANTHA/Downloads/download-NIFTY 50-01012020.zip')
    
    print(df)