473,320 Members | 2,158 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

convert string number to real number - ValueError: invalid literal

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 :P):
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'>
Feb 28 '08 #1
11 12289
On Thu, 28 Feb 2008 14:56:10 -0800 (PST)
davidj411 <da*******@gmail.comwrote:
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 <da***@druid.net | 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.
Feb 28 '08 #2
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 :P):
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
Feb 28 '08 #3
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.
Feb 29 '08 #4

"davidj411" <da*******@gmail.comwrote in message
news:3c**********************************@z17g2000 hsg.googlegroups.com...
|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

Feb 29 '08 #5
On Fri, 29 Feb 2008 13:32:15 -0800 (PST)
"SP******@gmail.com" <SP******@gmail.comwrote:
>
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 <da***@druid.net | 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.
Feb 29 '08 #6
On Feb 28, 5:56 pm, davidj411 <davidj...@gmail.comwrote:
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
Mar 1 '08 #7
En Tue, 27 May 2008 13:00:05 -0300, David Jackson <da*******@gmail.com>
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

Jun 27 '08 #8
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>

Jun 27 '08 #9
On May 28, 2:22*am, Kam-Hung Soh <kamhung....@gmail.comwrote:
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.
Jun 27 '08 #10
Am Wed, 28 May 2008 10:41:51 -0700 schrieb davidj411:
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.
Welcome to the wonderful world of floating point numbers. For your usage
you want 10-based numbers. Have a look at the decimal module:
>>from decimal import Decimal
a = Decimal("11.45")
a
Decimal("11.45")
>>str(a)
'11.45'
>>a + 1
Decimal("12.45")
>>a + Decimal("1.55")
Decimal("13.00")

HTH

Matthias
Jun 27 '08 #11
Matthias Bläsing wrote:
Am Wed, 28 May 2008 10:41:51 -0700 schrieb davidj411:
>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.

Welcome to the wonderful world of floating point numbers. For your usage
you want 10-based numbers. Have a look at the decimal module:
>>>from decimal import Decimal
a = Decimal("11.45")
a
Decimal("11.45")
>>>str(a)
'11.45'
>>>a + 1
Decimal("12.45")
>>>a + Decimal("1.55")
Decimal("13.00")

HTH

Matthias
--
http://mail.python.org/mailman/listinfo/python-list
Thanks for the tip, Matthias. I knew that there had to be a way to
handle arbitrary precision numbers.

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

Jun 27 '08 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ken | last post by:
I've been looking for a solution to a string to long conversion problem that I've run into >>> x = 'e10ea210' >>> print x e10ea210 >>> y=long(x) Traceback (most recent call last): File...
2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
7
by: Michael Foord | last post by:
How do you safely turn an arbitrarily long signed hex string into a long integer ? e.g. -0x55aff8080000 When treating them as hex literals I'm getting future warnings that from 2.4 hex values...
19
by: Mike Moum | last post by:
I think there may be a bug in string.atoi and string.atol. Here's some output from idle. > Python 2.3.4 (#2, Jan 5 2005, 08:24:51) > on linux2 > Type "copyright", "credits" or "license()"...
13
by: Hako | last post by:
I try this command: >>> import string >>> string.atoi('78',16) 120 this is 120 not 4E. Someone can tell me how to convert a decimal number to hex number? Can print A, B, C,DEF. Thank you.
16
by: ondekoza | last post by:
Hello, I need to convert the string "FFFFFFFF" to a long. To convert this string I tried the following: >>> 0xffffffff -1 >>> 0xffffffffL 4294967295L OK, this is what I want, so I tried
6
by: google0 | last post by:
I know this is a trivial function, and I've now spent more time searching for a surely-already-reinvented wheel than it would take to reinvent it again, but just in case... is there a published,...
2
by: kath | last post by:
Hi, 38938.0 <type 'unicode'> Traceback (most recent call last): File "D:\Python23\Testing area\Python and Excel\xlrdRead.py", line 30, in ?...
7
by: Andreas Hofmann | last post by:
Hello Folks! I've got a little problem here, which which really creeps me out at the moment. I've got some strings, which only contain numbers plus eventually one character as si-postfix (k for...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.