Python 正在解析多列文件

Python parsing multi column file

我有一个文件,我需要使用 python

从多行中解析并构造成单行
NAME ID

TITLE DEP

USER1  0023

S1  SALES

USER2  0022

A2  ACCOUNT

正如您在这里看到的,文件头是 NAME、ID、TITLE、DEP

我想像下面这样打印输出,这样我就可以轻松地阅读 csv 文件并轻松地做其他事情。

NAME, ID, TITLE, DEP
USER1,0023,S1,SALES
USER2,0022,A2,ACCOUNT

下面是我开始使用但无法到达我想要的位置的代码。 我尝试了不同的选项来拆分和替换但没有奏效。

import csv
file =open('test_file_parse.csv','r')
out_file=open('test_out.csv','w')
lines = file.readlines()
file.close()
for line in lines:
    line=line.strip()
    print (line)

感谢任何帮助

将所有单词都放在一个名为 file.txt 的文件中,代码如下:


# read all the words
with open('file.txt') as f:
    words = f.read().split()

# convert to groups of 4-s
groups4 = [words[i:i+4] for i in range(0, len(words), 4)]

# convert to lines with commas using join()
lines = [', '.join(lst) for lst in groups4]

# and here is the result
for line in lines:
    print(line)

输出:

NAME, ID, TITLE, DEP
USER1, 0023, S1, SALES
USER2, 0022, A2, ACCOUNT
infile = open('test_file_parse.csv','r')
def custom_func(x):
    return next(x).strip().split()
while infile:
    try:
       print ','.join(reduce(lambda x, y: x + y, (custom_func(infile) for z in range(4))))
    except TypeError:
       break
infile.close()

如果您知道所有内容都是两行对并且您知道可以忽略空行,则可以执行以下操作:

infile = open('test_file_parse.csv', 'r')

# A generator that yields the non-empty lines, without newlines.
lines = (l.strip() for l in infile if l.strip())

# An iterator to iterate over the yielded lines.
line_iter = iter(lines)

# A generator to yield space-separated combined lines.
new_lines = (' '.join(l_pair) for l_pair in zip(line_iter, line_iter))

# Lastly, a generator to yield proper csv for the lines.
csv_lines = (','.join(l.split()) for l in new_lines)

for line in csv_lines:
    print line