本文介绍了将字符串数转换为实数 - ValueError:无效的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在解析一个手机账单,以获取所有号码的列表以及每个号码上的总通话时间花费。


i已经有一个独特的电话号码列表。

现在我必须浏览号码列表并将每个号码的总额加起来为



on the账单,每一行有几个字段,一个字段包含电话

号码,另一个字段包含该电话的分钟数。

该帐单以逗号分隔。


这里是我写的函数,一次只能获得一个数字

通话时间。


def getsinglenumbertalktime(number,talktime):

for line in file [0:-2]:

如果行号:

li = line.split('','')

if len(li)== 6 and li [5]!=" Minutes" :

print" talktime type:" + str(类型(通话时间))

#li [5] = fpformat.fix(li [5],0)


print li [5] +li [5]类型:" + str(type(li [5]))

newvar = int(li [5])

print(type(newvar))

打印li [5]

talktime = talktime + li [5]

返回通话时间


这里输出有错误我回来了。


通话时间类型:<类型''int''>

" 2" li [5]类型:<输入''str''>

Traceback(最近一次调用最后一次):

文件"< stdin>",第1行,< module>

文件" c:\ path \ inprog \ all_t_mob_nums.py",第74行,< module>

gotinglenumbertalktime(''" 800- 218-2644"'',talktime)

文件" c:\ path \ inprog \ all_t_mob_nums.py",第66行,

gotinglenumbertalktime

newvar = int(li [5])

ValueError:基数为10的int()的无效文字:''" 2"''

这里是一个问题:


我如何转换像&q这样的字符串数字UOT; 2英寸一个真正的数字,可以添加


我尝试过使用pfformat,float()和int() - 一切都没有好

结果。

这似乎应该很简单,但它只是简单的不是。

我实际上找到了一个很好的解决方案。

基本上,取每个条目并将其添加到列表中。

然后遍历列表,将每个项目转换为int()。

然后添加它们以求它们全部。


最终解决方案:

def gotinglenumbertalktime(号码,通话时间):

num_of_calls = 0

num_mins = []

for line in file [0:-2]:

#print" LINE:" + line

#print 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)

如果len(li)== 7和li [5]!=" Minutes" :

#print" talktime type:" + str(类型(通话时间))

#print li [4] +" li [4]类型:" + 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)


如果len(li)== 6和li [5]!=" Minutes" :

#print" talktime type:" + str(类型(通话时间))

#print li [5] +" li [5] type:" + str(type(li [5]))

#newvar = fpformat.fix(li [4],0)

#print(type(newvar))

#print" len 6:" + str(类型(li [5]))

num_mins.append(li [5])

#talktime = talktime + int(a)

#return talktime,num_of_calls

x = 0

#print" this" + str(数字)+ str(num_mins)

表示在num_mins中:

b = int(a)

x = x + b

print str(number),str(x),str(type(x))


输出应该如下所示(不包括部分脚本)

555-555-5555取代了无辜者: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''>

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''>

推荐答案



你必须先摆脱双引号。


-

D ''Arcy JM Cain< da *** @ druid.net |民主是三只狼
|和一只羊投票

+1 416 425 1212(国防部#0082)(eNTP)|什么是晚餐。

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.




如果文件是引号和逗号分隔,你应该使用csv模块

读取和删除引号。应该让事情变得更容易。


-Larry

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




你的意思是什么都没换?


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


一旦我这样做,我应该可以将它们用作数字。

you mean replace them with nothing?

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

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


这篇关于将字符串数转换为实数 - ValueError:无效的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 00:33