加入csv文件中的每4行

时间:2017-08-16 08:54:40

标签: python python-2.7 csv

我想加入第4行,所以我认为这样的事情会起作用:

import csv

filename = "mycsv.csv"
f = open(filename, "rb")

new_csv = []
count = 1

for i, line in enumerate(file(filename)):
    line = line.rstrip()
    print line
    if count % 4 == 0:
        new_csv.append(old_line_1 + old_line_2 + old_line_3+line)
    else:
        old_line_1 = line[i-2]
        old_line_2 = line[i-1]
        old_line_3 = line
    count += 1

print new_csv

但是我认为line[i-1]line[i-2]不会占用当前行-1和-2。那么如何访问当前行-1和-2?

2 个答案:

答案 0 :(得分:2)

这应该按照您的要求进行

join_every_n = 4
all_lines = [line.rstrip() for line in file(filename)]  # note the OP uses some unknown func `file` here
transposed_lines = zip(*[all_lines[n::join_every_n] for n in range(join_every_n)])
joined = [''.join([l1,l2,l3,l4]) for (l1,l2,l3,l4) in transposed_lines]

同样你也可以做

joined = map(''.join, transposed_lines)

解释

这将返回your_list中每个第i个元素,偏移量为n

your_list[n::i]

然后,您可以将range(4)组合在一起,为列表中的每4行生成一次,以便获得

[[line0, line3, ...], [line1, line4, ...], [line2, line6, ...], [line3, line7, ...]]

然后需要transposed_lines来转置这个数组,使它变得像

[[line0, line1, line2, line3], [line4, line5, line6, line7], ...]

现在您可以简单地解压缩并加入每个单独的列表元素

实施例

all_lines = map(str, range(100))
transposed_lines = zip(*[all_lines[n::4] for n in range(4)])
joined = [''.join([l1,l2,l3,l4]) for (l1,l2,l3,l4) in transposed_lines]

给出

['0123',
 '4567',
 '891011',
...

答案 1 :(得分:2)

变量line仅包含当前迭代的行,因此访问line[i-1]只会在当前行中为您提供一个字符。 The other answer可能是最简洁的方法,但是,基于你的代码,你可以做这样的事情:

import csv

filename = "mycsv.csv"
with open(filename, "rb") as f:
    reader = csv.reader(f)
    new_csv = []
    lines = []
    for i, line in enumerate(reader):
        line = line.rstrip()
        lines.append(line)
        if (i + 1) % 4 == 0:
            new_csv.append("".join(lines))
            lines = []

print new_csv