Pankkitilin laskentaa Python ohjelmointikielellä ja csv tiedostolla
Jos haluat hiukan opetella python-ohjelmointikielellä tekemään pankkitilisi seurantaa, niin tämä saattaa auttaa sinua asiaan tutustumaan – varsinkaan jos et ole asiasta paljon perillä.
Hyviä koodaushaluja ja Vappua.
# Code starts here
import csv
”””
If you want to play little bit with python programming language and with real world application,
this might help you on the way. I have downloaded a note of my bank account in csv format like this:
File name (name is changed):
2023-03-30-12-12-12.csv
CSV file contains rows like this:
2023/03/24;-16,31;GGGG **** AAAA BBBB CC;;;S-MARKET KOIVUHAKA;;EUR
2023/03/24;-4,88;GGGG AAAA BBBB CCCC DD;;;LIDL VANTAA – KORSO;;EUR
2023/03/24;-7,04;GGGG AAAA VVVV DDDD WW;;;S-MARKET KOIVUHAKA;;EUR
Background for csv files and python’s string functions:
1 – csv file usages …
https://realpython.com/python-csv/
2 – python string functions …
https://www.w3schools.com/python/ref_string_startswith.asp
3 string to float
https://www.w3schools.com/python/python_casting.asp
4 string replace : string.replace(oldvalue, newvalue, count)
https://www.w3schools.com/python/ref_string_replace.asp
https://earthly.dev/blog/csv-python/
run program in Ubuntu 22.04 : python3 ./csv-count.py
Application sums euros according to column’s string value.
Finally all sums are summed into sums_sum.
row[1] contains euros and row[5] contains the category that sub sums are summed.
If you copy this application from uusisuomi pages, be aware that some characters
may change to new values in your computer, and application won’t work, but shows
error value that you can change to correct one. Happy coding and Vappu !
Sample run:
python3 ./csv-count.py
pyapp starting …
csv file scan ended !
sm_sum is: 257.89
alepa_sum is: 24.46
lidl_sum is: 4.88
sums_sum is: 287.22999999999996
”””
sm_sum = 0.0
alko_sum = 0.0
alepa_sum = 0.0
lidl_sum = 0.0
km_sum = 0.0
sums_sum = 0.0
fnum = 0.0
print(”pyapp starting …”)
with open(”./2023-04-30-14-18-54.csv”, ’r’) as file:
csvreader = csv.reader(file, delimiter=’;’)
for row in csvreader:
# Searhing for S-Market euros
fnum = 0.0
if (row[5].startswith(”S-”)):
fnum = float(row[1].replace(”,”, ”.”))
#print(”S-MARKET FOUND”)
fnum = fnum * (-1.0)
sm_sum = sm_sum + fnum
# Searhing for ALEPA euros
fnum = 0.0
if (row[5].startswith(”ALEPA”)):
fnum = float(row[1].replace(”,”, ”.”))
#print(”ALEPA FOUND”)
fnum = fnum * (-1.0)
alepa_sum = alepa_sum + fnum
# Searhing for LIDL euros
fnum = 0.0
if (row[5].startswith(”LIDL”)):
fnum = float(row[1].replace(”,”, ”.”))
#print(”ALEPA FOUND”)
fnum = fnum * (-1.0)
#print(fnum )
lidl_sum = lidl_sum + fnum
sums_sum = sm_sum + alepa_sum + lidl_sum
print(”csv file scan ended !”)
print(”sm_sum is:”, sm_sum)
print(”alepa_sum is:”, alepa_sum)
print(”lidl_sum is:”, lidl_sum)
print(”sums_sum is:”, sums_sum)
Näyttää siltä, että vaikka laitoin ohjelmakoodin code-tägien väliin, niin tuosta huolimatta sisennykset eivät tulostuneet oikein.
Edit : kokeillaanpa kuvatiedostona esittää ohjelmakoodi oikein sisennettynä:
Joo kuvaa ei ainakaan ihan helposti vääristellä!
Kommentit (0)