Хранение данных
# -*- encoding: utf-8 -*-
"""
Чтение и запись CSV формата
"""
import csv
reader = csv.reader(open("some.csv", "rb"))
for row in reader:
print row
#Reading a file with an alternate format:
import csv
reader = csv.reader(open("passwd", "rb"), delimiter=':', quoting=csv.QUOTE_NONE)
for row in reader:
print row
import csv
writer = csv.writer(open("some.csv", "wb"))
writer.writerows(someiterable)
# -*- encoding: utf-8 -*-
"""
Сжатие
"""
import zipfile
import glob, os
# open the zip file for writing, and write stuff to it
file = zipfile.ZipFile("test.zip", "w")
for name in glob.glob("./*"):
file.write(name, os.path.basename(name), zipfile.ZIP_DEFLATED)
file.close()
# open the file again, to see what's in it
file = zipfile.ZipFile("test.zip", "r")
for name in file.namelist():
data = file.read(name)
print name, len(data), repr(data[:10])
for info in file.infolist():
print info.filename, info.date_time, info.file_size, info.compress_size
# -*- encoding: utf-8 -*-
"""
Постоянное хранилище языковых объектов
"""
import shelve
d = shelve.open(filename) # open -- file may get suffix added by low-level library
d[key] = data # store data at key (overwrites old data if using an existing key)
data = d[key] # retrieve a COPY of data at key (raise KeyError if no such key)
del d[key] # delete data stored at key (raises KeyError if no such key)
flag = d.has_key(key) # true if the key exists
klist = d.keys() # a list of all existing keys (slow!)
# as d was opened WITHOUT writeback=True, beware:
d['xx'] = range(4) # this works as expected, but...
d['xx'].append(5) # *this doesn't!* -- d['xx'] is STILL range(4)!
# having opened d without writeback=True, you need to code carefully:
temp = d['xx'] # extracts the copy
temp.append(5) # mutates the copy
d['xx'] = temp # stores the copy right back, to persist it
d=shelve.open(filename,writeback=True)
d['xx'].append(5)
d.close() # close it