我有一个包含大约200行(包括whitelines)的三列CSV文件,如下所示。现在我想在第二列中的条目周围添加括号,基于第三列中的相同条目。
注意:'_'下划线条目不应被视为可能的“相同条目”。 注2:相同的条目(第三列)可以是一个或多个
1 _ _
2 A A1
3 B A1
4 C A4
5 _ _
6 A A5
7 B A5
8 _ _
9 C A4
10 D A4
11 E A4
12 E A2
# pseudocode:
IF there are multiple identical N entries in the third column:
then -> add left parenthesis to first entry second column
add right parenthesis to last entry second column
IF there is ONE entry in the third column:
then -> add parenthesis around entry in second column
我正在寻找的输出:
1 _ _
2 (A A1
3 B) A1
4 (C) A4
5 _ _
6 (A A5
7 B) A5
8 _ _
9 (C A4
10 D A4
11 E) A4
12 (E) A2
编辑,尝试更详细地解释:
我正在将column2和column3读入两个数组。
for i, j in enumerate(column3):
if j == identical consecutive entries column3:
# get indexes of those entries
# add ( to first corresponding index column2
# add ) to last corresponding index column2
# index could be one entry, or a range of entries