Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to do in Python
#1
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
Reply
#2
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.
Reply
#3
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()
Reply
#4
You only do anything with the last line, because your indentation is off. Work each line as you iterate over the file.
Reply
#5
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
Reply
#6
(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 Wink
Mint 18.2 comes with Python 3.5,look at Linux Python 3 environment
Reply
#7
(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.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020