在python里的实现对字符串和数字变量之间的转化:
data = open('C:\Documents and Settings\Administrator\桌面\test.txt')
>>> str2=[]
>>> for read_line in data:
fields=read_line
tool_num =(fields[:(len(fields)-1)])
abc.append(tool_num)
>>> abc
['1 0.000000 0.000000 1.000000 0.000000', '2 0.000000 0.000000 2.000000 0.000000', '3 33.000000 0.000000 0.000000 0.000000', '4 0.000000 0.000000 40.000000 0.000000', '5 0.000000 0.000000 0.000000 0.000000', '6 0.000000 0.000000 8.000000 0.000000', '7 0.000000 0.000000 0.000000 0.000000', '8 0.000000 0.000000 0.000000 0.000000', '9 0.000000 0.000000 0.000000 0.000000', '10 0.000000 0.000000 0.000000 0.000000', '11 0.000000 0.000000 0.000000 0.000000', '12 0.000000 0.000000 0.000000 0.000000', '13 0.000000 0.000000 0.000000 0.000000']
>>> str1=abc[0].split()
>>> str1
['1', '0.000000', '0.000000', '1.000000', '0.000000']
>>> str1[2]
'0.000000'
>>> str1[4]
'0.000000'
>>> str1[3]
'1.000000'
>>> f2=string.atof(str1[3])
>>> f2
1.0
>>> f1=string.atoi(str1[3])
1
Traceback (most recent call last):
File "
", line 1, in
f1=string.atoi(str1[3])
File "D:\Python26\lib\string.py", line 401, in atoi
return _int(s, base)
ValueError: invalid literal for int() with base 10: '1.000000'
>>> f1=string.atoi(str1[0])
>>> f1
这里用到的是就是python的一些内部函数,atof是将字符串转化为浮点数 atoi是将字符串转化为整型数。
将数字转化为字符串就更加的直接简单了
数字转换成字符串(接上面):
>>> d = "%i"%c # 10进制表示
>>> print
d
18
>>> type(d)
>>> e = "0x%X"%c
#16进制表示
>>> print e
0x12
阅读(656) | 评论(0) | 转发(0) |