python学习笔记11 字符串分割split

版权声明: https://blog.csdn.net/qq_40025335/article/details/81607608
#!/usr/bin/python
# -*-coding: utf-8 -*-
#Time:20180806
#Function:数据分割(以空格为界限)、数据类型转换(把字符串数据转换为浮点型数据)
#Runing OK
#author@New
# 纯在个问题每次写文件会把cc2530.txt清空在写,方便读数据,但历史数据无法保存
# 实现每次串口读的数据,先保存到cc2530.txt,然后读cc2530.txt数据,http发送出去
#设计数据类型转换:字符串 转换为浮点型,618 +28.0Z30.3P1.92 ->123 28.0 30.3 1.92


import os

str = "123 28.0 30.3 1.92"
# 直接从字符串中提取字符
str3 = str[0:3]
print(type(str3))
str3 = float(str3)
print(type(str3))
print("str", str[0:4])
# str = "618 +28.0Z30.3P1.92"
print("str 数据类型:")
print(type(str))
# str1 = "asd 28.0 asdf 1.92"
str1 = "3w.gorly.test.com.cn"
str1 = "3w gorly test com cn"
# print(str.split())
str2 =str.split()
print("str2 数据类型:")
print(type(str2))
# str3 = float(str)
print(str2[0])
asd = str2[0]
print("asd 数据类型:")
print(type(asd))
asd = float(asd)
print("float(asd) 数据类型:")
print(type(asd))
temp=str2[1]
ph = str2[2]
kg = str2[3]
print("asd:", asd)
print("temp:", temp)
print("ph:", ph)
print("kg:", kg)
# print("qq;%.3f" %str3)
# print(str1.split('.',3))

---------------------------------------------------------------------

G:\Python\testproject1\venv1\Scripts\python.exe G:/Python/testproject1/venv1/pyMutilProcess/cc2530数据分割.py
<class 'str'>
<class 'float'>
str 123 
str 数据类型:
<class 'str'>
str2 数据类型:
<class 'list'>
123
asd 数据类型:
<class 'str'>
float(asd) 数据类型:
<class 'float'>
asd: 123.0
temp: 28.0
ph: 30.3
kg: 1.92

Process finished with exit code 0
 

猜你喜欢

转载自blog.csdn.net/qq_40025335/article/details/81607608