Last active
October 2, 2019 13:55
-
-
Save fernandojunior/b4e587f5aaf29be16827e087065f103b to your computer and use it in GitHub Desktop.
read lines from zipped file stored in google cloud file system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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