คำสั่งไลบรารี gzip บน Python
คำสั่งเปิดไฟล์ zip
[python]
gzip.open(ไฟล์, โหมดการเปิดไฟล์)
[/python]
คำสั่งบีบอัดข้อมูล (มีใน Python 3.2 ขึ้นไป)
[python]
gzip.compress(data, compresslevel=9)
[/python]
คำสั่งคลายข้อมูล (มีใน Python 3.2 ขึ้นไป)
[python]
gzip.decompress(data)
[/python]
การใช้งาน
[python]
import gzip
[/python]
เข้ามาเมื่อเรียกใช้งานคำสั่งของไลบรารี gzip
เปิดไฟล์และอ่านไฟล์ GZIP
[python]
import gzip
with gzip.open('/home/joe/file.txt.gz', 'rb') as f:
file_content = f.read()
[/python]
สร้างไฟล์และเพิ่มข้อมูลเข้าไปในไฟล์ GZIP
[python]
import gzip
content = b"Lots of content here"
with gzip.open('/home/joe/file.txt.gz', 'wb') as f:
f.write(content)
เพิ่มข้อมูลจากไฟล์ txt เข้าไปยังไฟล์ GZIP
[python]
import gzip
with open('/home/joe/file.txt', 'rb') as f_in:
with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
f_out.writelines(f_in)
[/python]
บีบอัดข้อมูลไบต์สตริงด้วย GZIP
[python]
import gzip
s_in = b"Lots of content here"
s_out = gzip.compress(s_in)
[/python]
อ่านเอกสารเพิ่มเติมได้ที่ https://docs.python.org/3/library/gzip.html
ติดตามบทความต่อไปนะครับ
ขอบคุณครับ
0 ความคิดเห็น:
แสดงความคิดเห็น
แสดงความคิดเห็นได้ครับ :)