简体   繁体   English

加入csv文件中的第4行

[英]joining every 4th line in csv-file

I'd like to join every 4th line together so I thought something like this would work: 我想将每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

But line[i-1] and line[i-2] does not take current line -1 and -2 as I thought. 但是line[i-1]line[i-2]并不像我想的那样采用当前的-1和-2。 So how can I access current line -1 and -2? 那么,如何访问当前的-1和-2行?

This should do as you require 这应该按照您的要求进行

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]

likewise you could also do 同样,你也可以

joined = map(''.join, transposed_lines)

Explanation 说明

This will return every i'th element in a your_list with an offset of n 这将返回your_list每个第i个元素,其偏移量为n

your_list[n::i]

Then you can combine this across a range(4) to generate for every 4 lines in a list such that you get 然后,您可以将其跨range(4)组合以为列表中的每4行生成一次,从而得到

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

Then the transposed_lines is required to transpose this array so that it becomes like 然后transposed_lines是必需的以便它变得像转置该数组

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

Now you can simple unpack and join each individual list element 现在,您可以简单地解压缩并加入每个单独的列表元素

Example

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]

gives

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

The variable line contains only the line for the current iteration, so accessing line[i-1] will only give you one character within the current line. 变量line仅包含当前迭代的行,因此访问line[i-1]将仅在当前行中给您一个字符。 The other answer is probably the tersest way to put it but, building on your code, you could do something like this instead: 另一个答案可能是放置它的最好方法,但是,在代码的基础上,您可以执行以下操作:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM