Last active
August 29, 2018 22:34
-
-
Save Disassembler0/b8dcd120b392c484ae65f32418f04560 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import csv | |
colgroup = 5 # Kolik sloupcu na zacatku musi byt stejnych | |
with open('input.csv', 'r') as ifd, open('output.csv', 'w') as ofd: | |
reader = csv.reader(ifd, delimiter=';') # Vstupni CSV | |
writer = csv.writer(ofd, delimiter=';') # Vystupni CSV | |
buffer = [next(reader)] # Buffer, ktery drzi radky se stejnymi prvnimi sloupci | |
for row in reader: # Pruchod radku ve vstupnim CSV | |
if row[:colgroup] == buffer[0][:colgroup]: # Pokud ma novy radek stejnou skupinu jako prvni radek v bufferu... | |
buffer.append(row) # Pridej do bufferu | |
else: # Pokud ma jinou skupinu... | |
outrow = buffer[0][:colgroup] # Zkopiruj prvnich colgroup stejnych sloupcu z predchozi skupiny | |
for col in range(colgroup, len(buffer[0])): # Vsechny ostatni sloupce z radku v bufferu... | |
outrow.append(sum(int(bufrow[col]) for bufrow in buffer)) # Postupne secti | |
writer.writerow(outrow) # A zapis poscitany radek do vystupniho CSV | |
buffer = [row] # Vycisti buffer a nastav novy prvni radek pro novou skupinu | |
outrow = buffer[0][:colgroup] # Zopakuj to same pro zbyvajici radky v bufferu po skonceni cyklu | |
for col in range(colgroup, len(buffer[0])): | |
outrow.append(sum(int(bufrow[col]) for bufrow in buffer)) | |
writer.writerow(outrow) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import csv | |
indata = [] | |
outdata = [] | |
with open('input.csv', 'r') as fd: # Nacti komplet cele vstupni CSV do indata | |
reader = csv.reader(fd, delimiter=';') | |
indata = list(reader) | |
indata.append('') # Pridej prazdny radek na konec vstupnich dat, bude slouzit jako terminator pri poslednim pruchodu cyklem | |
colgroup = 5 # Kolik sloupcu na zacatku musi byt stejnych | |
ngrow = 0 # Cislo radku s prvnim vyskytem nove skupiny | |
for row in range(len(indata)): # Pruchod radku v indata | |
if indata[row][:colgroup] != indata[ngrow][:colgroup]: # Pri nalezeni noveho radku s jinou skupinou nez doposud... | |
outrow = indata[ngrow][:colgroup] # Zkopiruj prvnich colgroup stejnych sloupcu z predchozi skupiny | |
for col in range(colgroup, len(indata[ngrow])): # Vsechny ostatni sloupce se stejnou skupinou... | |
outrow.append(sum(int(grow[col]) for grow in indata[ngrow:row])) # Postupne secti | |
outdata.append(outrow) # A pridej poscitany radek do outdata | |
ngrow = row # Nastav novy prvni radek pro novou skupinu | |
with open('output.csv', 'w') as fd: # Vysyp outdata do vystupniho CSV | |
writer = csv.writer(fd, delimiter=';') | |
writer.writerows(outdata) |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pol1;pol2;pol3;pol4;pol5;10;20;30 | |
pol1;pol2;pol3;pol4;pol5;10;20;30 | |
pol1;pol2;pol3;pol4;pol5;10;20;30 | |
pol1;pol2;pol3;pol4;pol5;10;20;30 | |
pol1;pol2;pol3;pol4;pol6;10;20;30 | |
pol1;pol2;pol3;pol4;pol6;10;20;30 |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pol1;pol2;pol3;pol4;pol5;40;80;120 | |
pol1;pol2;pol3;pol4;pol6;20;40;60 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment