在列之间添加,跳过并保留一些行/列

2024-10-02 14:18:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我是编程新手,但我已经开始研究Python和Perl。你知道吗

我在两个输入文件中寻找部分是CSV的数据,选择其中一些并放入新的输出文件。你知道吗

也许Python CSV或Pandas可以在这里提供帮助,但是当涉及到跳过/保留行和列时,我有点卡住了。你知道吗

而且,我的列没有任何标题。你知道吗

输入文件1:

-- Some comments
KW1
'Z1' 'F' 30 26 'S'
KW2
'Z1' 30 26 1 1 5 7 /
'Z1' 30 26 2 2 6 8 /
'Z1' 29 27 4 4 12 13 /

输入文件2:

-- Some comments
-- Some more comments
KW1
'Z2' 'F' 40 45 'S'
KW2
'Z2' 40 45 1 1 10 10 /
'Z2' 41 45 2 2 14 15 /
'Z2' 41 46 4 4 16 17 /

所需输出文件:

KW_NEW
'Z_NEW' 1000 30 26 1 /
'Z_NEW' 1000 30 26 2 /
'Z_NEW' 1000 29 27 4 /
'Z_NEW' 1000 40 45 1 /
'Z_NEW' 1000 41 45 2 /
'Z_NEW' 1000 41 46 4 /

所以我想做的是:

  • 在我到达KW2

  • KW2替换为KW_NEW

  • 替换第一列中的Z1' orZ2withZ撸u NEW`

  • 添加一个新的第二列,其值为常量,例如1000

  • 按原样复制下三列

  • 在打印末尾的斜杠/之前,请删除所有剩余的列

有没有人能给我一些大概的提示/技巧来解决这个问题?你知道吗


Tags: 文件csv数据pandasnew编程somecomments
3条回答

您的文件不是“部分csv”(看不到逗号);它们是(部分)空格分隔的。您可以逐行读取文件,使用Python的.split()方法将相关字符串转换为子字符串列表,然后根据需要重新排列这些片段。拆分和重新组装可能如下所示:

input_line = "'Z1' 30 26 1 1 5 7 /"  # test data
input_items = input_line.split()
output_items = ["'Z_NEW'", '1000']
output_items.append(input_items[1])
output_items.append(input_items[2])
output_items.append(input_items[3])
output_items.append('/')
output_line = ' '.join(output_items)
print(output_line)

最后的print()语句显示结果字符串是

'Z_NEW' 1000 30 26 1 /

下面是使用Perl的一种方法:

#!/usr/bin/perl
use strict;
use warnings;

# initialize output array
my @output = ('KW_NEW');

# proceed first file
open my $fh1, '<', 'in1.txt' or die "unable to open file1: $!";
while(<$fh1>) {
    # consider only lines after KW2
    if (/KW2/ .. eof) {
        # Don't treat KW2 line
        next if /KW2/;
        # split the current line on space and keep only the fifth first element
        my @l = (split ' ', $_)[0..4];
        # change the first element
        $l[0] = 'Z_NEW';
        # insert 1000 at second position
        splice @l,1,0,1000;
        # push into output array
        push @output, "@l";
    }
}

# proceed second file
open my $fh2, '<', 'in2.txt' or die "unable to open file2: $!";
while(<$fh2>) {
    if (/KW2/ .. eof) {
        next if /KW2/;
        my @l = (split ' ', $_)[0..4];
        $l[0] = 'Z_NEW';
        splice @l,1,0,1000;
        push @output, "@l";
    }
}

# write array to output file
open my $fh3, '>', 'out.txt' or die "unable to open file3: $!";
print $fh3 $_,"\n" for @output;  

你的文件格式是静态的吗?(顺便说一句,这实际上不是csv:P)您可能需要研究一种标准化的文件格式,如JSON或strict csv来存储数据,以便可以使用现有的工具来解析输入文件。python有很好的JSON和CSV库,可以为您完成所有困难的工作。你知道吗

如果你被这种文件格式困住了,我会尝试类似的方法。你知道吗

path = '<input_path>'
kws = ['KW1', 'KW2']
desired_kw = kws[1]

def parse_columns(line):
    array = line.split()
    if array[-1] is '/':
        # get rid of trailing slash
        array = array[:-1]

def is_kw(cols):
    if len(cols) > 0 and cols[0] in kws:
        return cols[0]

# to parse the section denoted by desired keyword
with open(path, 'r') as input_fp:
    matrix = []
    reading_file = False
    for line in input_fp.readlines:
        cols = parse_columns(line)
        line_is_kw = is_kw(line)
        if line_is_kw:
            if not reading_file:
                if line_is_kw is desired_kw:
                    reading_file = True
                else:
                    continue
            else:
                break

        if reading_file:
            matrix = cols

print matrix

在那里,您可以使用诸如切片表示法和基本列表操作之类的方法来获得所需的数组。祝你好运!你知道吗

相关问题 更多 >