網站首頁 編程語言 正文
Python 讀取 .gz 文件
讀取.gz 文件需要使用gzip 包,如果沒有安裝可以自行在終端安裝
pip install gzip
import gzip
path = "" #你的文件路徑
f = gzip.open(path, 'rb')
?
for line in f.readlines(): # 按行進行讀取
? ? s = line.decode() # 讀取之后要進行解碼
? ? print(s) # s 為string類型,就是我們讀取的文件中的一行
也可以批量讀取,批量讀取文件使用os包對文件夾中的所有文件進行
import gzip
import os\
?
path = "" #表示你要打開的文件夾
files = os.listdir(path) #files 是path中存放的所有文件名集合
for file in files:
? ? f = gzip.open(path+file, 'rb')
? ? for line in f.readline():
? ? ? ? print(line)
Python 讀取gz文件,字符串與字節串的相互轉換
首先是字節串轉字符串,也就是str:
b = b'some byte array'
?
str(b, encoding = "utf-8") ?
#or
bytes.decode(b)
然后是字符串轉為字節串:
s = 'some string'
?
bytes(s, encoding = "utf8") ?
#or
str.encode(s)
fastq.gz文件讀取
with gzip.open(fq,'r') as fastq:
?? ?try:
?? ??? ?while True:
?? ??? ??? ?line1 = next(fastq).decode() ?# 字節轉字符串
?? ??? ??? ?line2 = next(fastq).decode()
?? ??? ??? ?line3 = next(fastq).decode()
?? ??? ??? ?line4 = next(fastq).decode()
?? ??? ??? ?
?? ?except:
?? ??? ??? ?pass
原文鏈接:https://blog.csdn.net/qq_39129717/article/details/123380595
相關推薦
- 2022-04-11 c#?理解csredis庫實現分布式鎖的詳細流程_C#教程
- 2022-07-15 go使用consul實現服務發現及配置共享實現詳解_Golang
- 2022-03-16 c#語言程序構建基塊_C#教程
- 2022-06-01 C語言超詳細講解字符串相乘_C 語言
- 2023-07-08 如何讓element-ui的el-row內容居中(水平垂直)
- 2022-04-28 SpringBoot?整合mongoDB并自定義連接池的示例代碼_MongoDB
- 2022-12-08 pycharm?無法加載文件activate.ps1的原因分析及解決方法_python
- 2023-05-16 Golang函數這些神操作你知道哪些_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支