读取文本文件并在Python中创建csv文件

时间:2018-12-12 11:21:58

标签: python shell csv text sh

我想读取多个文本文件并创建一个csv文件。 它的标题行很常见,可以从每个文件读取数据并在csv中创建一行。

输入文件就像。

readme_1006999-111418.html0000664077736200007070000001676613373210077014576

\--------------GENERAL INFO --------------------
CR ID #: 1006999-111418
Product Version: 10.1.10.1.5
Case ID #: 1012440-102118
\--------------Short description----
DS | Cleanup TEST| crashed on after run the admin command
\------ New/Modified files ------
Customer.cpp
input.cpp
output.cpp
\------ Affected executables and libraries ------
classes.jar
info.so
function.so
messageinfo.so

我想要输出类似

GENERAL INFO,Short description,Affected executables and libraries
CR ID #: 1006999-111418 Product Version: 10.1.10.1.5 Case ID #: 1012440-102118, DS | Cleanup TEST| crashed on after run the admin command,Customer.cpp input.cpp output.cpp,classes.jar info.so function.so messageinfo.so

在下一行,我要在“ GENERAL INFO”下输入三行,然后用逗号分隔,其他 标头。

由于我是python的新手,所以我在google的帮助下尝试了一些小代码。

import csv
import os
import glob

#for filename in glob.glob(os.path.join('/bd_users/abpwrk1/OM/AM/Lab', '*.txt')):
with open('test.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line.split(",") for line in stripped if line)
    with open('output.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('GENERAL INFO', 'Short description','New/Modified files','Affected executables and libraries','Affected executables and libraries','DETAILED INFO','Detailed Problem Description','Solution','Testing Instructions','ADDITIONAL IMPACT','Database Changes','Environment Notes','XPI/Topology Changes'))
       writer.writerows(lines)

有人可以帮忙编写一个逻辑,该逻辑将在单个单元格中打印header(封闭的------- header ----)下的所有行。

1 个答案:

答案 0 :(得分:0)

尝试类似的方法,希望对您有帮助

Parse a plain text file into a CSV file using Python

import csv
import itertools

with open('extracted.txt', 'r') as in_file:
    stripped = (line.strip() for line in in_file)
    lines = (line for line in stripped if line)
    grouped = itertools.izip(*[lines] * 3)
    with open('extracted.csv', 'w') as out_file:
        writer = csv.writer(out_file)
        writer.writerow(('title', 'intro', 'tagline'))
        writer.writerows(grouped)