我是编程新手,但我已经开始研究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' or
Z2with
Z撸u NEW`
添加一个新的第二列,其值为常量,例如1000
按原样复制下三列
在打印末尾的斜杠/
之前,请删除所有剩余的列
有没有人能给我一些大概的提示/技巧来解决这个问题?你知道吗
您的文件不是“部分csv”(看不到逗号);它们是(部分)空格分隔的。您可以逐行读取文件,使用Python的
.split()
方法将相关字符串转换为子字符串列表,然后根据需要重新排列这些片段。拆分和重新组装可能如下所示:最后的
print()
语句显示结果字符串是下面是使用Perl的一种方法:
你的文件格式是静态的吗?(顺便说一句,这实际上不是csv:P)您可能需要研究一种标准化的文件格式,如JSON或strict csv来存储数据,以便可以使用现有的工具来解析输入文件。python有很好的JSON和CSV库,可以为您完成所有困难的工作。你知道吗
如果你被这种文件格式困住了,我会尝试类似的方法。你知道吗
在那里,您可以使用诸如切片表示法和基本列表操作之类的方法来获得所需的数组。祝你好运!你知道吗
相关问题 更多 >
编程相关推荐