Help with code

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with code

Alden Meneses
Hello All,
 
I am playing with python to work read some text files and help me gather some data. I have some further reading to do and was hoping some of you can help point me in the right direction. This is what I have so far.
 
 
f = open('H:\xxxx\xxxx\xxxx\9-7-07')
#File is a report that summarizes each account by account group and service area then has the details for each account and Totals before the next group of accounts.
edit = ["GRP", "AREA", "CHARGES"]
ptype = "NULL"
area = "NULL"
for line in f:
    if line[:12] == 'ACCOUNT GROUP':
        ptype = line[16:]                                   # The account group starts on the 16th character of the line
    elif line[:11] == 'SERVICE AREA':
        area = line[11:]                                    # The service area starts on the 11th character of the line
    elif line(-1) == 'F' and line(1) != ' ':
        edit.append(ptype,area,line[56:66])        # I wanted to append the edit stack with the variables collected above.
f.close()
print edit
 
 
I get this error message -
 
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\menesea\My Documents\scripts\readfile.py", line 11, in <module>
    elif line(-1) == 'F' and line(1) != ' ':
TypeError: 'str' object is not callable
 
 
Thanks in advance,
Alden

_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Help with code

Shiqi Yang

Hi,

  I’m not an expert, but seems changing the line(-1) to line[-1] and line(1) to line[1] in your code would help in some sense.

 

Shiqi

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Alden Meneses
Sent: Friday, November 23, 2007 3:04 PM
To: [hidden email]
Subject: [Baypiggies] Help with code

 

Hello All,

 

I am playing with python to work read some text files and help me gather some data. I have some further reading to do and was hoping some of you can help point me in the right direction. This is what I have so far.

 

 

f = open('H:\xxxx\xxxx\xxxx\9-7-07')
#File is a report that summarizes each account by account group and service area then has the details for each account and Totals before the next group of accounts.
edit = ["GRP", "AREA", "CHARGES"]

ptype = "NULL"

area = "NULL"
for line in f:
    if line[:12] == 'ACCOUNT GROUP':
        ptype = line[16:]                                   # The account group starts on the 16th character of the line
    elif line[:11] == 'SERVICE AREA':
        area = line[11:]                                    # The service area starts on the 11th character of the line
    elif line(-1) == 'F' and line(1) != ' ':
        edit.append(ptype,area,line[56:66])        # I wanted to append the edit stack with the variables collected above.
f.close()
print edit

 

 

I get this error message -

 

Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\menesea\My Documents\scripts\readfile.py", line 11, in <module>
    elif line(-1) == 'F' and line(1) != ' ':
TypeError: 'str' object is not callable

 

 

Thanks in advance,

Alden


_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Help with code

Andy Wiggin
In reply to this post by Alden Meneses
in "elif line(-1) == 'F' and line(1) != ' ':", it looks like you need
[], not () for subscripting the string. -andy

On Nov 23, 2007 3:04 PM, Alden Meneses <[hidden email]> wrote:

> Hello All,
>
> I am playing with python to work read some text files and help me gather
> some data. I have some further reading to do and was hoping some of you can
> help point me in the right direction. This is what I have so far.
>
>
> f = open('H:\xxxx\xxxx\xxxx\9-7-07')
> #File is a report that summarizes each account by account group and service
> area then has the details for each account and Totals before the next group
> of accounts.
>  edit = ["GRP", "AREA", "CHARGES"]
> ptype = "NULL"
> area = "NULL"
> for line in f:
>     if line[:12] == 'ACCOUNT GROUP':
>         ptype = line[16:]                                   # The account
> group starts on the 16th character of the line
>      elif line[:11] == 'SERVICE AREA':
>         area = line[11:]                                    # The service
> area starts on the 11th character of the line
>     elif line(-1) == 'F' and line(1) != ' ':
>         edit.append(ptype,area,line[56:66])        # I wanted to append the
> edit stack with the variables collected above.
> f.close()
> print edit
>
>
> I get this error message -
>
> Traceback (most recent call last):
>   File
> "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
> line 310, in RunScript
>     exec codeObject in __main__.__dict__
>   File "C:\Documents and Settings\menesea\My Documents\scripts\readfile.py",
> line 11, in <module>
>     elif line(-1) == 'F' and line(1) != ' ':
> TypeError: 'str' object is not callable
>
>
> Thanks in advance,
> Alden
> _______________________________________________
> Baypiggies mailing list
> [hidden email]
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Help with code

Drew Perttula
In reply to this post by Alden Meneses
Alden Meneses wrote:

> f = open('H:\xxxx\xxxx\xxxx\9-7-07')
> #File is a report that summarizes each account by account group and
> service area then has the details for each account and Totals before the
> next group of accounts.
> edit = ["GRP", "AREA", "CHARGES"]
> ptype = "NULL"
> area = "NULL"
> for line in f:
>     if line[:12] == 'ACCOUNT GROUP':
>         ptype = line[16:]                                   # The
> account group starts on the 16th character of the line
>     elif line[:11] == 'SERVICE AREA':
>         area = line[11:]                                    #
> The service area starts on the 11th character of the line
>     elif line(-1) == 'F' and line(1) != ' ':
>         edit.append(ptype,area,line[56:66])        # I wanted to append
> the edit stack with the variables collected above.
> f.close()
> print edit

The others are correct about line[-1], but in some of these cases you
could use string methods:

    if line.startswith('ACCOUNT GROUP'):
...
    elif line.startswith('SERVICE AREA'):
...
    elif line.endswith('F') # less important

str.startswith is easier to read and maintain, since it's got an english
name and you don't have to count the length of your test string.



Then, edit.append takes only one argument. I'd guess you wanted to make
a list of 3-tuples:

   edit = [("GRP", "AREA", "CHARGES")]  # len-1 list of one tuple
...
   edit.append((ptype, area, line[56:66]))  # append one item to list




Your last line is probably just for testing, but you should be aware of
the pretty-printing library:

from pprint import pprint
...
pprint(edit)

[('GRP', 'AREA', 'CHARGES'),
  ('piggies', 'bay', '$99.99'),
  ('drew', 'bay', '$10.00')]


_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Help with code

Max Slimmer
In reply to this post by Alden Meneses
elif line(-1) == 'F' and line(1) != ' ':
you need to use [] vs () to subscript into line. as the message says line is not callable.
line[-1] is the last char in line and line[1] is the second char in line.
 
max


From: [hidden email] [mailto:[hidden email]] On Behalf Of Alden Meneses
Sent: Friday, November 23, 2007 3:04 PM
To: [hidden email]
Subject: [Baypiggies] Help with code

Hello All,
 
I am playing with python to work read some text files and help me gather some data. I have some further reading to do and was hoping some of you can help point me in the right direction. This is what I have so far.
 
 
f = open('H:\xxxx\xxxx\xxxx\9-7-07')
#File is a report that summarizes each account by account group and service area then has the details for each account and Totals before the next group of accounts.
edit = ["GRP", "AREA", "CHARGES"]
ptype = "NULL"
area = "NULL"
for line in f:
    if line[:12] == 'ACCOUNT GROUP':
        ptype = line[16:]                                   # The account group starts on the 16th character of the line
    elif line[:11] == 'SERVICE AREA':
        area = line[11:]                                    # The service area starts on the 11th character of the line
    elif line(-1) == 'F' and line(1) != ' ':
        edit.append(ptype,area,line[56:66])        # I wanted to append the edit stack with the variables collected above.
f.close()
print edit
 
 
I get this error message -
 
Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\menesea\My Documents\scripts\readfile.py", line 11, in <module>
    elif line(-1) == 'F' and line(1) != ' ':
TypeError: 'str' object is not callable
 
 
Thanks in advance,
Alden

_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Help with code

Alden Meneses
In reply to this post by Drew Perttula
Thank you to everyone for your comments and for the extra eyeballs on this line of code.....
 
elif line(-1) == 'F' and line(1) != ' ':
 
I blame that on stuffing myself with turkey. :)
 
Got a new question - How do you convert text to integer? I want to sum charges by group and area?
 
Thanks in advance,
 
Alden
 


 
On 11/23/07, Drew Perttula <[hidden email]> wrote:
Alden Meneses wrote:
> f = open('H:\xxxx\xxxx\xxxx\9-7-07')
> #File is a report that summarizes each account by account group and
> service area then has the details for each account and Totals before the
> next group of accounts.
> edit = ["GRP", "AREA", "CHARGES"]
> ptype = "NULL"
> area = "NULL"

> for line in f:
>     if line[:12] == 'ACCOUNT GROUP':
>         ptype = line[16:]                                   # The
> account group starts on the 16th character of the line
>     elif line[:11] == 'SERVICE AREA':
>         area = line[11:]                                    #
> The service area starts on the 11th character of the line
>     elif line(-1) == 'F' and line(1) != ' ':
>         edit.append(ptype,area,line[56:66])        # I wanted to append
> the edit stack with the variables collected above.
> f.close()
> print edit

The others are correct about line[-1], but in some of these cases you
could use string methods:

   if line.startswith('ACCOUNT GROUP'):
...
   elif line.startswith('SERVICE AREA'):
...
   elif line.endswith('F') # less important

str.startswith is easier to read and maintain, since it's got an english
name and you don't have to count the length of your test string.



Then, edit.append takes only one argument. I'd guess you wanted to make
a list of 3-tuples:

  edit = [("GRP", "AREA", "CHARGES")]  # len-1 list of one tuple
...
  edit.append((ptype, area, line[56:66]))  # append one item to list




Your last line is probably just for testing, but you should be aware of
the pretty-printing library:

from pprint import pprint
...
pprint(edit)

[('GRP', 'AREA', 'CHARGES'),
('piggies', 'bay', '$99.99'),
('drew', 'bay', '$10.00')]




_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Help with code

Ben Gutierrez
Hey Alden,

Use the int function.  E.g., int('4').

If you're not confident that all the  strings are actual numbers,
you'll need to catch exceptions:

try:
    value = int(some_string)
except ValueError:
    value = 0

Later!

Ben

On Nov 26, 2007 11:34 AM, Alden Meneses <[hidden email]> wrote:

> Thank you to everyone for your comments and for the extra eyeballs on this
> line of code.....
>
>
> elif line(-1) == 'F' and line(1) != ' ':
>
> I blame that on stuffing myself with turkey. :)
>
> Got a new question - How do you convert text to integer? I want to sum
> charges by group and area?
>
> Thanks in advance,
>
> Alden
>
>
>
>
>
>
> On 11/23/07, Drew Perttula <[hidden email]> wrote:
> > Alden Meneses wrote:
> > > f = open('H:\xxxx\xxxx\xxxx\9-7-07')
> > > #File is a report that summarizes each account by account group and
> > > service area then has the details for each account and Totals before the
> > > next group of accounts.
> > > edit = ["GRP", "AREA", "CHARGES"]
> > > ptype = "NULL"
> > > area = "NULL"
> > > for line in f:
> > >     if line[:12] == 'ACCOUNT GROUP':
> > >         ptype = line[16:]                                   # The
> > > account group starts on the 16th character of the line
> > >     elif line[:11] == 'SERVICE AREA':
> > >         area = line[11:]                                    #
> > > The service area starts on the 11th character of the line
> > >     elif line(-1) == 'F' and line(1) != ' ':
> > >         edit.append(ptype,area,line[56:66])        # I wanted to append
> > > the edit stack with the variables collected above.
> > > f.close()
> > > print edit
> >
> > The others are correct about line[-1], but in some of these cases you
> > could use string methods:
> >
> >    if line.startswith('ACCOUNT GROUP'):
> > ...
> >    elif line.startswith('SERVICE AREA'):
> > ...
> >    elif line.endswith('F') # less important
> >
> > str.startswith is easier to read and maintain, since it's got an english
> > name and you don't have to count the length of your test string.
> >
> >
> >
> > Then, edit.append takes only one argument. I'd guess you wanted to make
> > a list of 3-tuples:
> >
> >   edit = [("GRP", "AREA", "CHARGES")]  # len-1 list of one tuple
> > ...
> >   edit.append((ptype, area, line[56:66]))  # append one item to list
> >
> >
> >
> >
> > Your last line is probably just for testing, but you should be aware of
> > the pretty-printing library:
> >
> > from pprint import pprint
> > ...
> > pprint(edit)
> >
> > [('GRP', 'AREA', 'CHARGES'),
> > ('piggies', 'bay', '$99.99'),
> > ('drew', 'bay', '$10.00')]
> >
> >
> >
>
>
> _______________________________________________
> Baypiggies mailing list
> [hidden email]
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies
Reply | Threaded
Open this post in threaded view
|

Re: Help with code

Alden Meneses
Thanks Ben.
 
Yeah. I tried that and thought I was doing something wrong. My problem was I was iterating the float or the integer which python didn't like.

 
On 11/26/07, Ben Gutierrez <[hidden email]> wrote:
Hey Alden,

Use the int function.  E.g., int('4').

If you're not confident that all the  strings are actual numbers,
you'll need to catch exceptions:

try:
   value = int(some_string)
except ValueError:
   value = 0

Later!

Ben

On Nov 26, 2007 11:34 AM, Alden Meneses <[hidden email]> wrote:

> Thank you to everyone for your comments and for the extra eyeballs on this
> line of code.....
>
>
> elif line(-1) == 'F' and line(1) != ' ':
>
> I blame that on stuffing myself with turkey. :)
>
> Got a new question - How do you convert text to integer? I want to sum
> charges by group and area?
>
> Thanks in advance,
>
> Alden
>
>
>
>
>
>
> On 11/23/07, Drew Perttula <[hidden email]> wrote:
> > Alden Meneses wrote:
> > > f = open('H:\xxxx\xxxx\xxxx\9-7-07')

> > > #File is a report that summarizes each account by account group and
> > > service area then has the details for each account and Totals before the
> > > next group of accounts.
> > > edit = ["GRP", "AREA", "CHARGES"]
> > > ptype = "NULL"
> > > area = "NULL"
> > > for line in f:
> > >     if line[:12] == 'ACCOUNT GROUP':
> > >         ptype = line[16:]                                   # The
> > > account group starts on the 16th character of the line
> > >     elif line[:11] == 'SERVICE AREA':

> > >         area = line[11:]                                    #
> > > The service area starts on the 11th character of the line
> > >     elif line(-1) == 'F' and line(1) != ' ':
> > >         edit.append(ptype,area,line[56:66])        # I wanted to append
> > > the edit stack with the variables collected above.
> > > f.close()
> > > print edit
> >
> > The others are correct about line[-1], but in some of these cases you
> > could use string methods:
> >
> >    if line.startswith('ACCOUNT GROUP'):
> > ...
> >    elif line.startswith('SERVICE AREA'):
> > ...
> >    elif line.endswith('F') # less important
> >
> > str.startswith is easier to read and maintain, since it's got an english
> > name and you don't have to count the length of your test string.
> >
> >
> >
> > Then, edit.append takes only one argument. I'd guess you wanted to make
> > a list of 3-tuples:
> >
> >   edit = [("GRP", "AREA", "CHARGES")]  # len-1 list of one tuple
> > ...
> >   edit.append((ptype, area, line[56:66]))  # append one item to list
> >

> >
> >
> >
> > Your last line is probably just for testing, but you should be aware of
> > the pretty-printing library:
> >
> > from pprint import pprint
> > ...
> > pprint(edit)
> >
> > [('GRP', 'AREA', 'CHARGES'),
> > ('piggies', 'bay', '$99.99'),
> > ('drew', 'bay', '$10.00')]
> >
> >
> >
>
>
> _______________________________________________
> Baypiggies mailing list
> [hidden email]
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
>
_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies


_______________________________________________
Baypiggies mailing list
[hidden email]
To change your subscription options or unsubscribe:
http://mail.python.org/mailman/listinfo/baypiggies