4

I had the following code working to convert a csv file to a dictionary.

with open(filename, encoding='utf-8') as file:
    dictReader = csv.DictReader(file)

    for row in dictReader: 
        #Do Work

These csv files arrive zipped and so I figured I'd attempt to modify the code to read the zipped csv files into a dictionary. Things get tricky since as I understand it, when opening a file inside a zip archive, the file is opened as bytes rather than text. I had it working where I was reading the entire file in, and converting it to text, but then I hit a large file and had a memory error. So I updated the code to read the file in one line at a time and now I suspect that I'll have to build the dictionary manually, but I figured it was worth asking here.


Thanks to pmsh.93, I now have it working.

with zipfile.ZipFile(filename) as zipFile:
    for fname in zipFile.infolist():
        with zipFile.open(fname) as file:
            file = io.TextIOWrapper(file, encoding="utf-8")

            for row in csv.DictReader(file):
                #Do Work
  • Can you also share the code that reads the zip file, *converts it to text* ( uncompresses it) and converts it to a dictionary. – Ajay Brahmakshatriya Apr 07 '17 at 03:51
  • http://stackoverflow.com/questions/5627954/py3k-how-do-you-read-a-file-inside-a-zip-file-as-text-not-bytes This thread must answer your doubts. – pmsh.93 Apr 07 '17 at 04:02

0 Answers0