Forum » Programiranje » CSV file
CSV file
zero22 ::
Pozdravljeni!
Dobil sem CSV file za katerega moram spisat skripto, ki kot input vzame podatke CSV datoteke,
CSV prebere in obdela tako, da dobimo kot rezultat listo dictionerjev - ki bo zgledal nekako tako:
[
{
"Username" : "booker12",
"Identifier" : "9012",
"One-time password" : "12se74"
"Recovery code" :"rb9012" ,
"First name" : "Rachel",
"Last name" : "Booker",
"Department" : "Sales",
"Location" : "Manchester"
}
# ...
]
https://files.fm/u/4u98vzgmt#/ (csv file sem uploadal)
Tole je največ kar mi uspe naredit, več mi ne rata, zelo bi prosil za pomoč.
with open('username-password-recovery-code.csv', 'r') as my_file:
data = my_file.read()
rows = data.split("\n")
for row in rows:
info = row.split(",")
print(type(info),info)
Dobil sem CSV file za katerega moram spisat skripto, ki kot input vzame podatke CSV datoteke,
CSV prebere in obdela tako, da dobimo kot rezultat listo dictionerjev - ki bo zgledal nekako tako:
[
{
"Username" : "booker12",
"Identifier" : "9012",
"One-time password" : "12se74"
"Recovery code" :"rb9012" ,
"First name" : "Rachel",
"Last name" : "Booker",
"Department" : "Sales",
"Location" : "Manchester"
}
# ...
]
https://files.fm/u/4u98vzgmt#/ (csv file sem uploadal)
Tole je največ kar mi uspe naredit, več mi ne rata, zelo bi prosil za pomoč.
with open('username-password-recovery-code.csv', 'r') as my_file:
data = my_file.read()
rows = data.split("\n")
for row in rows:
info = row.split(",")
print(type(info),info)
HotBurek ::
CSV file ni dostopen, vsaj meni ne. Neki login na slack sprašuje...
Ali lahko objaviš sample 10 vrstic iz CSV-ja kar tule gor (lahko ga daš znotraj "Vstavi kodo", da bo izgledalo kul)?
Ali lahko objaviš sample 10 vrstic iz CSV-ja kar tule gor (lahko ga daš znotraj "Vstavi kodo", da bo izgledalo kul)?
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
zero22 ::
Username; Identifier;One-time password;Recovery code;First name;Last name;Department;Location
booker12;9012;12se74;rb9012;Rachel;Booker;Sales;Manchester
grey07;2070;04ap67;lg2070;Laura;Grey;Depot;London
johnson81;4081;30no86;cj4081;Craig;Johnson;Depot;London
jenkins46;9346;14ju73;mj9346;Mary;Jenkins;Engineering;Manchester
smith79;5079;09ja61;js5079;Jamie;Smith;Engineering;Manchester
Al pa če copy-paste naredm v .txt file pa uploadam?
booker12;9012;12se74;rb9012;Rachel;Booker;Sales;Manchester
grey07;2070;04ap67;lg2070;Laura;Grey;Depot;London
johnson81;4081;30no86;cj4081;Craig;Johnson;Depot;London
jenkins46;9346;14ju73;mj9346;Mary;Jenkins;Engineering;Manchester
smith79;5079;09ja61;js5079;Jamie;Smith;Engineering;Manchester
Al pa če copy-paste naredm v .txt file pa uploadam?
Zgodovina sprememb…
- spremenilo: zero22 ()
HotBurek ::
#!/usr/bin/python3.9 # init empty list list = []; # source csv file csv = ""; csv = csv + "booker12;9012;12se74;rb9012;Rachel;Booker;Sales;Manchester" + "\n"; csv = csv + "grey07;2070;04ap67;lg2070;Laura;Grey;Depot;London" + "\n"; csv = csv + "johnson81;4081;30no86;cj4081;Craig;Johnson;Depot;London" + "\n"; csv = csv + "jenkins46;9346;14ju73;mj9346;Mary;Jenkins;Engineering;Manchester" + "\n"; csv = csv + "smith79;5079;09ja61;js5079;Jamie;Smith;Engineering;Manchester" + "\n"; # split file into rows rows = csv.split("\n"); # go for each row for row in rows: # split row into columns columns = row.split(";"); # init new empty dictionary dictionary = {}; # go for each column for i in range(0, len(columns)): # write each column into dictionary if i == 0: dictionary["Username"] = columns[i]; elif i == 1: dictionary["Identifier"] = columns[i]; elif i == 2: dictionary["One-time password"] = columns[i]; elif i == 3: dictionary["Recovery code"] = columns[i]; elif i == 4: dictionary["First name"] = columns[i]; elif i == 5: dictionary["Last name"] = columns[i]; elif i == 6: dictionary["Department"] = columns[i]; elif i == 7: dictionary["Location"] = columns[i]; # append dictionary to list list.append(dictionary) # print print(list);
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
Zgodovina sprememb…
- spremenilo: HotBurek ()
HotBurek ::
Zgornjega posta ne morem več popravit. Tole je nekak finalna verzija:
#!/usr/bin/python3.9 # import import os; # init empty list list = []; # init empty csv file csv = ""; # read csv from file if os.path.exists("username-password-recovery-code.csv"): file = open("username-password-recovery-code.csv", "r"); csv = file.read(); file.close(); # split file into rows rows = csv.split("\n"); # go for each row for i in range(0, len(rows)): # skip first line, it is header if i > 0: # split row into columns columns = rows[i].split(";"); # init new empty dictionary dictionary = {}; # go for each column for j in range(0, len(columns)): # write each column into dictionary if j == 0: dictionary["Username"] = columns[j]; elif j == 1: dictionary["Identifier"] = columns[j]; elif j == 2: dictionary["One-time password"] = columns[j]; elif j == 3: dictionary["Recovery code"] = columns[j]; elif j == 4: dictionary["First name"] = columns[j]; elif j == 5: dictionary["Last name"] = columns[j]; elif j == 6: dictionary["Department"] = columns[j]; elif j == 7: dictionary["Location"] = columns[j]; # append dictionary to list list.append(dictionary) # print print(list);
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
HotBurek ::
Dokumentacija:
https://stackoverflow.com/questions/322...
Rešitev:
https://stackoverflow.com/questions/322...
Rešitev:
#!/usr/bin/python3.9 # import import os; import json; # init empty list list = []; # init empty csv csv = ""; # init file path filepath = "username-password-recovery-code.csv"; # read csv from file if os.path.exists(filepath): file = open(filepath, "r"); csv = file.read(); file.close(); else: print("Warning: file does not exits."); print("Path: " + filepath); # split file into rows rows = csv.split("\n"); # go for each row for i in range(0, len(rows)): # skip first line, it is header if i > 0: # split row into columns columns = rows[i].split(";"); # init new empty dictionary dictionary = {}; # go for each column for j in range(0, len(columns)): # write each column into dictionary if j == 0: dictionary["Username"] = columns[j]; elif j == 1: dictionary["Identifier"] = columns[j]; elif j == 2: dictionary["One-time password"] = columns[j]; elif j == 3: dictionary["Recovery code"] = columns[j]; elif j == 4: dictionary["First name"] = columns[j]; elif j == 5: dictionary["Last name"] = columns[j]; elif j == 6: dictionary["Department"] = columns[j]; elif j == 7: dictionary["Location"] = columns[j]; # append dictionary to list list.append(dictionary) # print print(json.dumps(list, indent=4));
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
zero22 ::
Super. Deluje. Dolžn sm ti za pivo.
Hodim na tečaje smartninja če si že slišal. Pa nevem slabo uči. Ful hitr gremo čez snov. In sem čist
zgubljen, maš mogoče kak nasvet al napotek kje bi se še lahka kar se da naučil vsaj teh osnov tolko da razumem
ta koncept programiranja. Imam zakupljen tudi course na udemy. Ženska res fajn razlaga nimam kej rečt. Sam me
zanima če se še kje v Sloveniji izvaja kak tečaj. Ker kolkr so hvalil smartninja žal niso nevem kaj.
Hodim na tečaje smartninja če si že slišal. Pa nevem slabo uči. Ful hitr gremo čez snov. In sem čist
zgubljen, maš mogoče kak nasvet al napotek kje bi se še lahka kar se da naučil vsaj teh osnov tolko da razumem
ta koncept programiranja. Imam zakupljen tudi course na udemy. Ženska res fajn razlaga nimam kej rečt. Sam me
zanima če se še kje v Sloveniji izvaja kak tečaj. Ker kolkr so hvalil smartninja žal niso nevem kaj.
kunigunda ::
Tole zgoraj verjetno deluje samo, ce ni ; v samem stringu.
a;b;"something;and this";last
a;b;"something;and this";last
tony1 ::
Priporočam Courserin tečaj Google Crash course on Python, ki pa je, če se ne motim, prav tako plačljiv.
Če te zanimajo knjige pa priporočam karkoli za Python začetnike avtorja Ala Swigarta iz založbe No startch press. Nekaj njegovih knjig se da dobiti zastonj na njegovi spletni strani https://inventwithpython.com/hacking/
Če te zanimajo knjige pa priporočam karkoli za Python začetnike avtorja Ala Swigarta iz založbe No startch press. Nekaj njegovih knjig se da dobiti zastonj na njegovi spletni strani https://inventwithpython.com/hacking/
Zgodovina sprememb…
- spremenil: tony1 ()
HotBurek ::
Tole zgoraj verjetno deluje samo, ce ni ; v samem stringu.
a;b;"something;and this";last
Zagotovo je tako. Če ne drugega, bi prišlo vsaj do napak oz. zamika. In kako bi to (elegantno) rešil?
Python je sel naprej od verzije 3.9 in podpira tudi switch statement ...
Imam Debian in to je najnovejša verzija, ker je tu gor. Kaj "switch" omogoča oz. izboljša napram uporabi "if/elif/elif"?
Tisti del bi se dalo sicer rešit tudi bolj "na trdo", ob predpostavki, da je v vsaki vrstici vsaj 8 podatkov.
Primer:
# init new empty dictionary dictionary = {}; # fill up dictionary dictionary["Username"] = columns[0]; dictionary["Identifier"] = columns[1]; dictionary["One-time password"] = columns[2]; dictionary["Recovery code"] = columns[3]; dictionary["First name"] = columns[4]; dictionary["Last name"] = columns[5]; dictionary["Department"] = columns[6]; dictionary["Location"] = columns[7]; # append dictionary to list list.append(dictionary)
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
kuall ::
Tole zgoraj verjetno deluje samo, ce ni ; v samem stringu.
a;b;"something;and this";last
Zagotovo je tako. Če ne drugega, bi prišlo vsaj do napak oz. zamika. In kako bi to (elegantno) rešil?
kako le? ko najdeš " rečeš weAreInString = !weAreInString
pol moraš pa še na to računat, da je lahko " v stringu:
if weAreInString and curChar = '"' and prevChar != '"'
weAreInString = false
kunigunda ::
Zagotovo je tako. Če ne drugega, bi prišlo vsaj do napak oz. zamika. In kako bi to (elegantno) rešil?
Nardis si nek simpl tokenizer, ce ze ne obstaja v jeziku (al pa regex).
Ce ta python podpira regex v split lohk das npr
csv.split(";(?=(?:(?:[^\"]*+\"){2})*+[^\"]*+$)")
Zgodovina sprememb…
- spremenilo: kunigunda ()
zero22 ::
Ko smo imeli html pa css in bootstrap je bilo zanimivo oziroma končno sem začel dojemat kako in kaj in potem
smo že šli na pyhton. Evo tu se je pa men ustavilo, nikakor nevem kako bi se učil da bi razumel, ali je res
programiranje zgolj ta tiste ko jim je vse samoumnevno.
smo že šli na pyhton. Evo tu se je pa men ustavilo, nikakor nevem kako bi se učil da bi razumel, ali je res
programiranje zgolj ta tiste ko jim je vse samoumnevno.
HotBurek ::
Sem poizkušal z regex, pa ni šlo po načrtih. Najde celoten line, ampak ga ne split-ta.
Če ima kdo rešitev, kar pogumno.
Spodaj sample:
Output:
Če ima kdo rešitev, kar pogumno.
Spodaj sample:
#!/usr/bin/python3.9 import re; input = "a;b;\"something;and this\";last "; # sample 1 print("sample 1 **************************************************"); matches = re.split(";(?=(?:(?:[^\"]*\+\"){2})*\+[^\"]*\+$)", input); print("len=" + str(len(matches))); for i in range(0 ,len(matches)): print(matches[i]); # sample 2 print("sample 2 **************************************************"); reiter = re.finditer(";(?=(?:(?:[^\"]*\+\"){2})*\+[^\"]*\+$)", input); for iter in reiter: print("aaa"); item = input[iter.start(), iter.end()]; print(item); # sample 3 print("sample 3 **************************************************"); reall = re.findall(";(?=(?:(?:[^\"]*\+\"){2})*\+[^\"]*\+$)", input); print("len=" + str(len(reall))); for i in range(0 ,len(reall)): print(reall[i]);
Output:
sample 1 ************************************************** len=1 a;b;"something;and this";last sample 2 ************************************************** sample 3 ************************************************** len=0
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
Ahim ::
Kako naj bi pa to sploh delovalo? Patterm mora ustrezati enemu stolpcu.
Namesto tega zongliranja eksplodiraj input string s podpicjem kot delimiterjem, nato pa zlepi skupaj elemente od (vkljucno) prvega, ki se zacne z ", do (vkljucno) tistega naslednjega, ki se konca z ", za vsako pojavo " na zacetku elementa.
https://www.w3schools.com/python/ref_st...
Namesto tega zongliranja eksplodiraj input string s podpicjem kot delimiterjem, nato pa zlepi skupaj elemente od (vkljucno) prvega, ki se zacne z ", do (vkljucno) tistega naslednjega, ki se konca z ", za vsako pojavo " na zacetku elementa.
https://www.w3schools.com/python/ref_st...
kunigunda ::
Sem poizkušal z regex, pa ni šlo po načrtih. Najde celoten line, ampak ga ne split-ta.
Če ima kdo rešitev, kar pogumno.
Spodaj sample:
#!/usr/bin/python3.9
import re;
input = "a;b;\"something;and this\";last ";
# sample 1
print("sample 1 **************************************************");
matches = re.split(";(?=(?:(?:[^\"]*\+\"){2})*\+[^\"]*\+$)", input);
print("len=" + str(len(matches)));
for i in range(0 ,len(matches)):
print(matches[i]);
# sample 2
print("sample 2 **************************************************");
reiter = re.finditer(";(?=(?:(?:[^\"]*\+\"){2})*\+[^\"]*\+$)", input);
for iter in reiter:
print("aaa");
item = input[iter.start(), iter.end()];
print(item);
# sample 3
print("sample 3 **************************************************");
reall = re.findall(";(?=(?:(?:[^\"]*\+\"){2})*\+[^\"]*\+$)", input);
print("len=" + str(len(reall)));
for i in range(0 ,len(reall)):
print(reall[i]);
Output:
sample 1 **************************************************
len=1
a;b;"something;and this";last
sample 2 **************************************************
sample 3 **************************************************
len=0
Zgleda da mal drugac regex interpetira kot drugi jeziki.
Probi tkole:
matches = re.findall(r'"[^"]*"|\'[^\']*\'|[^"\';]+',input)
HotBurek ::
Ja, to pa deluje.
Sample:
Output:
Sample:
#!/usr/bin/python3.9 import re; input = "a;b;\"something;and this\";last"; regex = '"[^"]*"|\'[^\']*\'|[^"\';]+'; reall = re.findall(regex, input); print("len=" + str(len(reall))); for i in range(0 ,len(reall)): print("item(" + str(i) + ")=" + reall[i]);
Output:
len=4 item(0)=a item(1)=b item(2)="something;and this" item(3)=last
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
bemfa ::
Predno diplomirate:
import csv import json csvfile = open('file.csv', 'r') jsonfile = open('file.json', 'w') fieldnames = ("Username","Identifier","One-time password","Recovery code","First name","Last name","Department","Location") reader = csv.DictReader( csvfile, fieldnames) for row in reader: json.dump(row, jsonfile) jsonfile.write('\n')
kunigunda ::
Hvala za pomoc. Jaz pythona ne poznam niti ne vem kaj vse ze ima built-in, zato so bli tud nasveti temu primerni :)
HotBurek ::
V katerem programskem jeziku (ali več njih) pa programiraš, da ti je uspelo zaobit Python?
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
kunigunda ::
Sej jeziki so si dost podobni vids pa dobis filing ostalo se pa sprot naucis k zacnes delat v posameznem. Pythona sicer ne maram ker mi je osebno grd tko k mi ni kitajscina lepa pa jo miljardo plus govori :)
HotBurek ::
Sej Python omogoča uporabo podpičja na koncu vsakega ukaza, tako da izgleda vsaj half decent.
Je pa res, da se ne da uporabljat curly brackets na tak način, kot to gre pri nrp. JavaScript.
Je pa res, da se ne da uporabljat curly brackets na tak način, kot to gre pri nrp. JavaScript.
root@debian:/# iptraf-ng
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
fatal: This program requires a screen size of at least 80 columns by 24 lines
Please resize your window
kunigunda ::
Sej Python omogoča uporabo podpičja na koncu vsakega ukaza, tako da izgleda vsaj half decent.
Je pa res, da se ne da uporabljat curly brackets na tak način, kot to gre pri nrp. JavaScript.
Jp, kle so sli mal nazaj. Tale indent blokov me zlo spominja se na cajte Cobola cist na zacetku. Pac stvar navade.
Ga pa zlo po solah forsirajo vidm.
Vredno ogleda ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | python regex splitOddelek: Programiranje | 551 (456) | HotBurek |
» | (Java) Iskanje manjkajoči podatkov v tabeliOddelek: Programiranje | 1052 (817) | noraguta |
» | Python - pomoč (strani: 1 2 3 )Oddelek: Programiranje | 18047 (8795) | black ice |
» | Naloga iz Putka - UPMOddelek: Programiranje | 2217 (1553) | NejcSSD |
⊘ | python pomočOddelek: Programiranje | 3407 (2328) | Mavrik |