持续更新
1、list类型转换为str
抓取的时候经常使用正则抓一些网址,即使抓取到的只有一条数据,或者只有数据和空白,或者是加上网址域名前缀作为二级的原始网页,但列表不太好做进一步的处理。比较有效的是使用 ‘’.join 来链接
res2 = re.findall(re.compile('正则表达式'),res1.text)
baseurl2 = ''.join(res2)
2、取出使用input输入的数据,使用eval,这里直接贴一段代码
#温度转换函数
TemStr = input("请输入带有符号的温度值:")
if TemStr[-1] in ['F','f']:
C = (eval(TemStr[0:-1]) - 32)/1.8
print("转换后的温度是:{:.2f}C".format(C))
elif TemStr[-1] in ['C','c']:
F = 1.8*eval(TemStr[0:-1]) + 32
print("转换后的温度是:{:.2f}F".format(F))
else:
print("输入格式错误!")
3、str转换为int,使用map函数
a,b = map(int,input().split(' '))
c = a+b
sum_c = c
t = []
while c>=1:
num_pre,num_aft = divmod(c,10)
t.append(num_aft)
c = num_pre
if sum_c< 100:
print(sum_c)
elif sum_c >= 100 and t[-2] == 0:
print(t[0])
else:
print(t[1],t[0],sep = '')
4、int转换为str,可以直接转换
a,b = map(int,input().split(' '))
c = a+b
sum_c = c
t = str(c)
if sum_c< 100:
print(sum_c)
elif sum_c >= 100 and t[-2] == '0':
print(t[-1])
else:
print(t[-2],t[-1],sep = '')