Posts: 18
Threads: 8
Joined: Apr 2018
Using Python version 2.7.12 on Linux Mint 18.2. Will be upgrading os next week.
My background is that of various forms of basic and some perl. I have researched
and am unable to figureout how do the following in python.
Can someone help please.
Thanks in advance
oldcity
$datafile = "expenses18";
expenses18 file
041118,1,11.11,1
042618,2,22.22.2
perl code
open(FILE, '<', $datafile) or die("Could not open* file! datafile\n");
@lines = <FILE>;
close(FILE);
foreach (@lines) {
($pdate,$catg,$amt,$howp) = (split(/,/));
# $mm pulled from $pdate
col1[$mm] = $pdate ;
col2[$mm] = $catg ;
col3]$mm] = $amt ;
col4[$mm] = $howp ;
python code
expenses18 file
041118,1,11.11,1
042618,2,22.22.2
with open('expenses18', 'r') as file:
lines = file.readlines:
## from here on I'm stuck
Posts: 3,458
Threads: 101
Joined: Sep 2016
for line in lines:
pdate, catg, amt, howp = line.split(",") What's col1 etc supposed to be? A dict/hash map? A list?
Also, don't use file.readlines , just iterate over the file directly.
Posts: 18
Threads: 8
Joined: Apr 2018
Apr-27-2018, 03:07 PM
(This post was last modified: Apr-27-2018, 03:21 PM by nilamo.)
Would only do the last item in list of 'expenses-18'. Need to loop thru file.
expenses-18
010118,1,1.00,1
020218,2,2.22,2
030318,3,33.33,3
040418,4,44.44,4
#!/usr/bin/python
num_lines = sum(1 for line in open('expenses-18'))
print ' \n'
print "file line count :", num_lines
print ' \n'
# file = open('expenses-18', 'r')
with open('expenses-18', 'r') as file:
for line in file:
pdate, catg, amt, howp = line.split(",")
print line
print 'pdate :', pdate
print 'catg :', catg
print ' amt :', amt
print 'howp :', howp
print ' ##\n'
file.close()
Posts: 3,458
Threads: 101
Joined: Sep 2016
You only do anything with the last line, because your indentation is off. Work each line as you iterate over the file.
Posts: 18
Threads: 8
Joined: Apr 2018
Please help. Show me how to do this. Have indented code and get no complaint.
As it is still only reports last item. What is correct?
The plan is to work with 11 categories.
tia
Example of expenses-18.
012118,1,1.00,1
022218,2,2.22,2
032318,3,33.33,3
042418,4,44.44,3
#!/usr/bin/python
x = 0
tamt = 0
with open('expenses-18', 'r') as file:
for line in file:
pdate,catg,amt,howp = line.split(",")
catg = int(catg)
tamt = tamt + float(amt)
x = x + 1
print 'cnt > ',x ,'catg :', catg
if catg == 1:
print '* line > ', line
elif catg == 2:
print '** line > ', line
elif catg == 3:
print '*** line > ', line
elif catg == 4:
print '**** line > ', line
print ' ##\n'
print 'total amt > ',tamt
Posts: 7,319
Threads: 123
Joined: Sep 2016
May-24-2018, 06:22 PM
(This post was last modified: May-24-2018, 06:22 PM by snippsat.)
(May-24-2018, 03:53 PM)oldcity Wrote: The plan is to work with 11 categories. If you want do some work with data Pandas give a lot for free,take a look later.
Can first look at csv module,i guss you want to add header so it look like this.
Output: pdate,catg,atm,howp
010118,1,1.00,1
020218,2,2.22,2
030318,3,33.33,3
040418,4,44.44,4
This code gives output over.
import csv
with open("expenses-18.csv") as f, open("output.csv","w", newline='') as f_out:
cr = csv.reader(f)
cw = csv.writer(f_out)
header = ['pdate', 'catg', 'atm', 'howp']
cw.writerow(header)
cw.writerows(cr) Pandas as mention you get a lot for free ,if need to work with data.
Here add header,and all data get convert to integer64 or float64.
Now it's easy to calculate with data or add plot ect.
>>> import pandas
>>> df = pandas.read_csv('expenses-18.csv', header=None)
>>> df.columns = ['pdate', 'catg', 'atm', 'howp']
# It's now a Dataframe
>>> df
pdate catg atm howp
0 10118 1 1.00 1
1 20218 2 2.22 2
2 30318 3 33.33 3
3 40418 4 44.44 4
>>> # All is correct type
>>> df.catg
0 1
1 2
2 3
3 4
Name: catg, dtype: int64
>>> df.atm
0 1.00
1 2.22
2 33.33
3 44.44
Name: atm, dtype: float64 Quote:Using Python version 2.7.12 on Linux Mint 18.2.
Drop Python 2 there is no excuses for use it anymore 
Mint 18.2 comes with Python 3.5,look at Linux Python 3 environment
Posts: 3,458
Threads: 101
Joined: Sep 2016
(May-24-2018, 03:53 PM)oldcity Wrote: for line in file:
pdate,catg,amt,howp = line.split(",")
catg = int(catg)
tamt = tamt + float(amt)
x = x + 1
print 'cnt > ',x ,'catg :', catg
That's your entire file processing. If you want to do more with each line, then whatever processing you're trying to do to each line, should be within the for block.
|