Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > convert string number to real number - ValueError: invalid literal

Reply
Thread Tools

convert string number to real number - ValueError: invalid literal

 
 
davidj411
Guest
Posts: n/a
 
      02-28-2008
i am parsing a cell phone bill to get a list of all numbers and the
total talktime spend on each number.

i already have a unique list of the phone numbers.
now i must go through the list of numbers and add up the totals for
each number.
on the bill, each line has a few fields,one field containing the phone
number, another field containing the number of minutes on that call.
the bill is comma delimited.

here is the function i wrote to get one number at a time's total
talktime.

def getsinglenumbertalktime(number,talktime):
for line in file[0:-2]:
if number in line:
li=line.split(',')
if len(li)==6 and li[5]!="Minutes" :
print "talktime type: " + str(type (talktime))
#li[5]=fpformat.fix(li[5],0)

print li[5] + "li[5] type: " + str(type(li[5]))
newvar = int(li[5])
print (type(newvar))
print li[5]
talktime = talktime + li[5]
return talktime

here is the output with error that i get back.

talktime type: <type 'int'>
"2"li[5] type: <type 'str'>
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\path\inprog\all_t_mob_nums.py", line 74, in <module>
getsinglenumbertalktime('"800-218-2644"',talktime)
File "c:\path\inprog\all_t_mob_nums.py", line 66, in
getsinglenumbertalktime
newvar = int(li[5])
ValueError: invalid literal for int() with base 10: '"2"'


here is the question:

How can i convert a string number like "2" to a true number that can
be added.
I have tried using pfformat, float(), and int() - all with no good
results.
this seems like is should be simple, but it just plain isn't.


I actually found a good solution.
basically, take each entry and add it to a list.
then iterate through the list , converting each item to int().
then add them to sum them all up.

FINAL SOLUTION:
def getsinglenumbertalktime(number,talktime):
num_of_calls=0
num_mins=[]
for line in file[0:-2]:
#print "LINE: " + line
#print number in line
if number in line:
num_of_calls += 1
#print number,num_of_calls
li=line.strip("\n")
#print "stripped:" + line
li=li.split(',')
#print "split: " + str(li)
#print "len of li: " + str(len(li)) + str(num_of_calls)
if len(li)==7 and li[5]!="Minutes" :
#print "talktime type: " + str(type (talktime))
#print li[4] + "li[4] type: " + str(type(li[5]))
#newvar = fpformat.fix(li[4],0)
#print (type(newvar))
#print "len 7: " + str(type(li[6]))
num_mins.append(li[6])
#talktime = talktime + int(a)

if len(li)==6 and li[5]!="Minutes" :
#print "talktime type: " + str(type (talktime))
#print li[5] + "li[5] type: " + str(type(li[5]))
#newvar = fpformat.fix(li[4],0)
#print (type(newvar))
#print "len 6: " + str(type(li[5]))
num_mins.append(li[5])
#talktime = talktime + int(a)
#return talktime , num_of_calls
x=0
#print "this" + str(number) + str(num_mins)
for a in num_mins:
b=int(a)
x=x+b
print str(number), str(x), str(type(x))

output should look like this (parts of script are not included)
555-555-5555 replaced the innocent ):
555-555-5555 19 <type 'int'>
555-555-5555 6 <type 'int'>
555-555-5555 3 <type 'int'>
555-555-5555 3 <type 'int'>
555-555-5555 2 <type 'int'>
555-555-5555 52 <type 'int'>
 
Reply With Quote
 
 
 
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      02-28-2008
On Thu, 28 Feb 2008 14:56:10 -0800 (PST)
davidj411 <> wrote:
> ValueError: invalid literal for int() with base 10: '"2"'
>
>
> here is the question:
>
> How can i convert a string number like "2" to a true number that can
> be added.


You have to get rid of the double quotes first.

--
D'Arcy J.M. Cain <> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      02-28-2008
davidj411 wrote:
> i am parsing a cell phone bill to get a list of all numbers and the
> total talktime spend on each number.
>
> i already have a unique list of the phone numbers.
> now i must go through the list of numbers and add up the totals for
> each number.
> on the bill, each line has a few fields,one field containing the phone
> number, another field containing the number of minutes on that call.
> the bill is comma delimited.
>
> here is the function i wrote to get one number at a time's total
> talktime.
>
> def getsinglenumbertalktime(number,talktime):
> for line in file[0:-2]:
> if number in line:
> li=line.split(',')
> if len(li)==6 and li[5]!="Minutes" :
> print "talktime type: " + str(type (talktime))
> #li[5]=fpformat.fix(li[5],0)
>
> print li[5] + "li[5] type: " + str(type(li[5]))
> newvar = int(li[5])
> print (type(newvar))
> print li[5]
> talktime = talktime + li[5]
> return talktime
>
> here is the output with error that i get back.
>
> talktime type: <type 'int'>
> "2"li[5] type: <type 'str'>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "c:\path\inprog\all_t_mob_nums.py", line 74, in <module>
> getsinglenumbertalktime('"800-218-2644"',talktime)
> File "c:\path\inprog\all_t_mob_nums.py", line 66, in
> getsinglenumbertalktime
> newvar = int(li[5])
> ValueError: invalid literal for int() with base 10: '"2"'
>
>
> here is the question:
>
> How can i convert a string number like "2" to a true number that can
> be added.
> I have tried using pfformat, float(), and int() - all with no good
> results.
> this seems like is should be simple, but it just plain isn't.
>
>
> I actually found a good solution.
> basically, take each entry and add it to a list.
> then iterate through the list , converting each item to int().
> then add them to sum them all up.
>
> FINAL SOLUTION:
> def getsinglenumbertalktime(number,talktime):
> num_of_calls=0
> num_mins=[]
> for line in file[0:-2]:
> #print "LINE: " + line
> #print number in line
> if number in line:
> num_of_calls += 1
> #print number,num_of_calls
> li=line.strip("\n")
> #print "stripped:" + line
> li=li.split(',')
> #print "split: " + str(li)
> #print "len of li: " + str(len(li)) + str(num_of_calls)
> if len(li)==7 and li[5]!="Minutes" :
> #print "talktime type: " + str(type (talktime))
> #print li[4] + "li[4] type: " + str(type(li[5]))
> #newvar = fpformat.fix(li[4],0)
> #print (type(newvar))
> #print "len 7: " + str(type(li[6]))
> num_mins.append(li[6])
> #talktime = talktime + int(a)
>
> if len(li)==6 and li[5]!="Minutes" :
> #print "talktime type: " + str(type (talktime))
> #print li[5] + "li[5] type: " + str(type(li[5]))
> #newvar = fpformat.fix(li[4],0)
> #print (type(newvar))
> #print "len 6: " + str(type(li[5]))
> num_mins.append(li[5])
> #talktime = talktime + int(a)
> #return talktime , num_of_calls
> x=0
> #print "this" + str(number) + str(num_mins)
> for a in num_mins:
> b=int(a)
> x=x+b
> print str(number), str(x), str(type(x))
>
> output should look like this (parts of script are not included)
> 555-555-5555 replaced the innocent ):
> 555-555-5555 19 <type 'int'>
> 555-555-5555 6 <type 'int'>
> 555-555-5555 3 <type 'int'>
> 555-555-5555 3 <type 'int'>
> 555-555-5555 2 <type 'int'>
> 555-555-5555 52 <type 'int'>


If the file is quote and comma delimited, you should be using the csv module to
do your reading and stripping of the quotes. Should make things MUCH easier.

-Larry
 
Reply With Quote
 
SPGIMail@gmail.com
Guest
Posts: n/a
 
      02-29-2008

> You have to get rid of the double quotes first.


you mean replace them with nothing?

li[4].replace('"','')

once i do that, i should be able to use them as numbers.
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      02-29-2008

"davidj411" <> wrote in message
news:3ca86e1d-a32f-48f2-bc9a-...
|i am parsing a cell phone bill to get a list of all numbers and the
| total talktime spend on each number.
|
| i already have a unique list of the phone numbers.
| now i must go through the list of numbers and add up the totals for
| each number.

| here is the function i wrote to get one number at a time's total
| talktime.
|
| def getsinglenumbertalktime(number,talktime):
| for line in file[0:-2]:
....
| | talktime = talktime + li[5]
| return talktime

It would appear that you are calling this function and rescanning scanning
file for each number. It would be more efficient to scan the file once,
for each line parsing out the number and minutes and incrementing the
minutes for that number.

Also, if you had printed repr(li[5]) and len(li[5]) instead of or in
addition to just li[5] (which prints str(li[5]),

>>> a='"2"'
>>> print a, repr(a), len(a)

"2" '"2"' 3

you might have seen for yourself the problem that the string contains quote
marks and not just digits. Repr and len are useful debugging aids.

tjr



 
Reply With Quote
 
D'Arcy J.M. Cain
Guest
Posts: n/a
 
      02-29-2008
On Fri, 29 Feb 2008 13:32:15 -0800 (PST)
"" <> wrote:
>
> > You have to get rid of the double quotes first.

>
> you mean replace them with nothing?
>
> li[4].replace('"','')


Sure, that will do. However, look at the csv module for another way of
handling this.

> once i do that, i should be able to use them as numbers.


You still need to apply int() or float() to the result.

--
D'Arcy J.M. Cain <> | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
 
Reply With Quote
 
George Sakkis
Guest
Posts: n/a
 
      03-01-2008
On Feb 28, 5:56 pm, davidj411 <davidj...@gmail.com> wrote:

> i am parsing a cell phone bill to get a list of all numbers and the
> total talktime spend on each number.
>
> (snipped)
>
> I actually found a good solution.
> (snipped)


If you post 1-2 samples of the cell phone bill input, I am sure you'll
get much better solutions in terms of clarity, simplicity and
elegance, commonly known as "pythonicity"

George
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      05-28-2008
En Tue, 27 May 2008 13:00:05 -0300, David Jackson <>
escribió:

> i used the csv module and saved its contents to a list.
>
> ['Date', 'No.', 'Description', 'Debit', 'Credit']
> ['3/17/2006', '5678', 'ELECTRONIC PAYMENT', '', '11.45']
> ['3/04/2007', '5678', 'THE HOME DEPOT 263 SomeCity FL', '', '25.40']
>
>
> the credit/debit fields are strings.
> what should i have done within the CSV module to make numbers appear as
> numbers?
> how can i remove the quotes to make them numbers? i realize i posted a
> solution to this once before (same posting thread) but i am thinking
> there
> is a better method.


What do you want to do with those empty strings? Convert them to zeros?
Suppose your list is named `rows`:

for i,row in enumerate(rows):
if not i: continue # skip titles
row[3] = float(row[3] or 0)
row[4] = float(row[4] or 0)

--
Gabriel Genellina

 
Reply With Quote
 
Kam-Hung Soh
Guest
Posts: n/a
 
      05-28-2008
David Jackson wrote:
> i used the csv module and saved its contents to a list.
>
> ['Date', 'No.', 'Description', 'Debit', 'Credit']
> ['3/17/2006', '5678', 'ELECTRONIC PAYMENT', '', '11.45']
> ['3/04/2007', '5678', 'THE HOME DEPOT 263 SomeCity FL', '', '25.40']
>
>
> the credit/debit fields are strings.
> what should i have done within the CSV module to make numbers appear as
> numbers?
> how can i remove the quotes to make them numbers? i realize i posted a
> solution to this once before (same posting thread) but i am thinking
> there is a better method.


There doesn't seem to be a way to describe how specific columns should
be processed in the csv module. You could define a conversion function
that "guesses" the best conversion, for example:

def str2num(datum):
try:
return int(datum)
except:
try:
return float(datum)
except:
return datum

for row in csv.reader(file(r'Transaction.csv')):
[str2num(cell) for cell in row]

['Date', 'No.', 'Description', 'Debit', 'Credit']
['3/17/2006', 5678, 'ELECTRONIC PAYMENT', '', 11.449999999999999]
['3/04/2007', 5678, 'THE HOME DEPOT 263 SomeCity FL', '',
25.399999999999999]

--
Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>

 
Reply With Quote
 
davidj411
Guest
Posts: n/a
 
      05-28-2008
On May 28, 2:22*am, Kam-Hung Soh <kamhung....@gmail.com> wrote:
> David Jackson wrote:
> > i used the csv module and saved its contents to a list.

>
> > ['Date', 'No.', 'Description', 'Debit', 'Credit']
> > ['3/17/2006', '5678', 'ELECTRONIC PAYMENT', '', '11.45']
> > ['3/04/2007', '5678', 'THE HOME DEPOT 263 SomeCity FL', '', '25.40']

>
> > the credit/debit fields are strings.
> > what should i have done within the CSV module to make numbers appear as
> > numbers?
> > how can i remove the quotes to make them numbers? i realize i posted a
> > solution to this once before (same posting thread) but i am thinking
> > there is a better method.

>
> There doesn't seem to be a way to describe how specific columns should
> be processed in the csv module. *You could define a conversion function
> that "guesses" the best conversion, for example:
>
> def str2num(datum):
> * * * * try:
> * * * * * * * * return int(datum)
> * * * * except:
> * * * * * * * * try:
> * * * * * * * * * * * * return float(datum)
> * * * * * * * * except:
> * * * * * * * * * * * * return datum
>
> for row in csv.reader(file(r'Transaction.csv')):
> * * * * [str2num(cell) for cell in row]
>
> ['Date', 'No.', 'Description', 'Debit', 'Credit']
> ['3/17/2006', 5678, 'ELECTRONIC PAYMENT', '', 11.449999999999999]
> ['3/04/2007', 5678, 'THE HOME DEPOT 263 SomeCity FL', '',
> 25.399999999999999]
>
> --
> Kam-Hung Soh <a href="http://kamhungsoh.com/blog">Software Salariman</a>


I like the str2num function approach, but then i get left with a float
that has more than 2 decimal spaces , i.e. 11.50 becomes
11.449999999999999 and round will not fix that.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert a literal string to regex Peng Yu Perl Misc 1 06-13-2010 03:55 PM
convert string literal to object attribute BiraRai Python 2 10-31-2008 07:18 PM
How do fix: "Wrong number of arguments" / "invalid propert assignment"/ "Invalid argument" Java Guy Javascript 1 10-15-2006 08:41 PM
How do fix: "Wrong number of arguments" / "invalid propert assignment"/ "Invalid argument" Java Guy Java 1 10-15-2006 08:31 PM
What's wrong with rpc-literal? Why use doc-literal? Anonieko Ramos ASP .Net Web Services 0 09-27-2004 09:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57