有没有办法将.gz文件中的字段复制到txt文件中,而无需在Windows命令或Python中解压缩?
答案 0 :(得分:0)
这可以使用Python的gzip库来读取.gz
文件,csv
库来解析内部的CSV文件,并使用operator.itemgetter()
从每个文件中提取所需的项目。行:
from operator import itemgetter
import csv
import gzip
with gzip.open('input.gz', 'rb') as f_gz, open('output.csv', 'wb') as f_output:
csv_input = csv.reader(f_gz)
csv_output = csv.writer(f_output)
req_cols = itemgetter(1, 2, 3, 5, 6, 10, 11, 12, 20)
csv_output.writerow(req_cols(next(csv_input)))
for row in csv_input:
if row[0] == 'SVR':
csv_output.writerow(req_cols(row))
因此,如果您的CSV包含以下内容:
c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,c21,c22
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
SVR,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
3,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
SVR,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
您将获得包含以下内容的输出CSV文件:
c2,c3,c4,c6,c7,c11,c12,c13,c21
2,3,4,6,7,11,12,13,21
2,3,4,6,7,11,12,13,21
在Python 2.x上测试过。对于Python 3.x,文件需要打开为:
with gzip.open('input.gz', 'rt', newline='') as f_gz, open('output.csv', 'w', newline='') as f_output: