简体   繁体   English

如何在不解压缩的情况下读取gzip格式的文本文件,使用python将那行写入excel?

[英]How to read the text files in gzip format without unzipping it write that line to excel using python?

Problem Statement : I have a directory with gzip files , and each gzip file contains a text file. 问题陈述:我有一个包含gzip文件的目录,每个gzip文件都包含一个文本文件。 I have written a code in such a way that it unzips all the gzip files and then used to read each unzipped text file and then combined that output to one text file, then applied a condition , if that condition meets then it writes to excel. 我以这样一种方式编写了代码:将所有gzip文件解压缩,然后用于读取每个解压缩的文本文件,然后将输出合并到一个文本文件,然后应用条件,如果满足该条件,则将其写入excel。 The above process is bit tedious and lengthy. 上面的过程有点繁琐和冗长。 Can anyone please help me out in writing the code where the data is read directly from the txt file which is gzipped and write it contents to excel. 任何人都可以帮助我编写代码,直接从压缩后的txt文件中读取数据并将其内容写入excel。

IIUC you can use pandas using first read_csv : IIUC您可以使用first read_csv来使用熊猫:

df = read_csv('yourfile.gzip', compression='gzip')

then apply your conditions on df and write back the dataframe to excel using to_excel : 然后在df上应用您的条件,并使用to_excel将数据帧写回excel:

df.to_excel(file.xls)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 在不解压(压缩格式)的情况下读取 zip 文件内容 - How to read zip file content without unzipping (in compressed format) using Python 如何读取文本文件格式并将其写入python中的以下格式中提到的excel文件中 - How to read the text file format and write the same in to excel file as mentioned in the below format in python 如何在python中的特定目录中读取多个.gz文件而不解压缩它们 - how to read multiple .gz files in a particular directory in python without unzipping them 如何编写for循环将excel文件读入Python - How to write a for loop to read in excel files into Python 如何使用Python读取文件并将其完全写入几个文本文件? - How to read a file and write it entirely to several text files using Python? 如何从Excel读取数据并逐行将其写入文本文件? - How to read data from Excel and write it to text file line by line? 使用Python读写固定格式(MODFLOW)文本文件 - Read and write fixed format (MODFLOW) text files with Python 使用 Python 将多个 .gz 文件解压缩为单个文本文件 - Unzipping multiple .gz files into single text file using Python 使用python解压缩大文件 - unzipping large files using python 如何编写Python脚本读取文本并以XML格式编写 - How to write Python script read text and write in XML format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM