Skip to content

Instantly share code, notes, and snippets.

@fernandojunior
Last active October 2, 2019 13:55
Show Gist options
  • Save fernandojunior/b4e587f5aaf29be16827e087065f103b to your computer and use it in GitHub Desktop.
Save fernandojunior/b4e587f5aaf29be16827e087065f103b to your computer and use it in GitHub Desktop.
read lines from zipped file stored in google cloud file system
import zipfile
import gcsfs
fs = gcsfs.GCSFileSystem(project='project-name')
with fs.open('file.zip') as f:
z = zipfile.ZipFile(f)
for filename in z.namelist():
if not os.path.isdir(filename):
# read the file
with z.open(filename) as f:
for line in f:
if line.decode('utf8').count('"') > 2:
print(line)
import csv
from io import TextIOWrapper
from zipfile import ZipFile
fnzip = 'filecsv.zip'
with fs.open(fnzip) as f:
with ZipFile(f) as zf:
with zf.open('filecsv.csv', 'r') as infile:
reader = csv.reader(TextIOWrapper(infile))
try:
linenumber = 1
for row in reader:
# process the CSV here
print(linenumber)
linenumber += 1
except Exception as e:
print (("Error line %d: %s %s" % (linenumber, str(type(e)), e.message)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment