excel - How to download a .xls into a Python object via FTP with Python ftplib -
i new python, forgive me if asking wrong question. have .xls ftp , save python object. currently, taking awkward approach [step 1] first ftp file , save .txt (i know .xls->.txt bad idea!), , [step 2] open , save .txt file in python shell. is,
step 1:
## import necessary module ftplib import ftp ftp = ftp('webftp.vancouver.ca') ftp.login() ftp.cwd('opendata/xls') filename = 'new_food_vendor_locations.xls' ftp.retrbinary('retr %s' % filename, open('mylovelynewfile.txt', 'w').write) ftp.quit() this allows me create new file 'mylovelynewfile.txt' in current directly. in python shell, have:
step 2:
mylovelypython = open('mylovelynewfile.txt','r') line in mylovelypython.readlines(): print line mylovelypython.close() although python can run these command lines, printed lines not have correct inputs due forceful conversion xls txt. furthermore, save ftp-ed object directly python object (rather first saving .txt outside python). there easy solution problem?
although looking way directly purse ftp-ed excel file python object, if allows saving excel file in directory , re-read again, following works:
ftp = ftp('webftp.vancouver.ca') ftp.login() ftp.cwd('opendata/xls') filename = 'new_food_vendor_locations.xls' ftp.retrbinary('retr %s' % filename, open('mylovelynewfile.xls', 'w').write) ftp.quit() workbook = xlrd.open_workbook('mylovelynewfile.xls') worksheet = workbook.sheet_by_name('query_vendor_food') num_rows = worksheet.nrows - 1 curr_row = -1 while curr_row < num_rows: curr_row += 1 row = worksheet.row(curr_row) print row (i thank @jacoppens advice.)
Comments
Post a Comment