1
0
Fork 0
mirror of https://github.com/Findus23/column-transformation.git synced 2024-09-19 14:43:46 +02:00

initial version

This commit is contained in:
Lukas Winkler 2021-04-27 19:04:08 +02:00
commit ecc793da07
Signed by: lukas
GPG key ID: 54DE4D798D244853
3 changed files with 35 additions and 0 deletions

4
input.csv Normal file
View file

@ -0,0 +1,4 @@
label nb_visits
somelabel|anotherlabel 1423
somelabel|anotherlabel|test 1187
test|word 1131
1 label nb_visits
2 somelabel|anotherlabel 1423
3 somelabel|anotherlabel|test 1187
4 test|word 1131

4
out.csv Normal file
View file

@ -0,0 +1,4 @@
somelabel;2610
anotherlabel;2610
test;2318
word;1131
1 somelabel 2610
2 anotherlabel 2610
3 test 2318
4 word 1131

27
script.py Normal file
View file

@ -0,0 +1,27 @@
from pathlib import Path
from sys import argv
inputfile = Path(argv[1])
outputfile = Path(argv[2])
counter = {}
with inputfile.open() as ip:
next(ip) # skip the first line
for line in ip: # go through all lines
columns = line.split("\t")
value = int(columns[1])
names = columns[0].split("|")
for name in names:
if name not in counter:
# if this is the first time the name showed up,
# set the counter to the value
counter[name] = value
else:
# otherwise increase it by the value
counter[name] += value
print(counter)
with outputfile.open("w") as wf:
for name, count in counter.items():
wf.write(f"{name};{count}\n") # Cinema;4