在 python 2.7 中划分字符串
Dividing strings in python 2.7
我从事自动百叶窗/窗帘项目已有一段时间了,一切正常,但我想添加百叶窗在早上逐渐打开的功能。我正在从配置文件中读取我的值,并将这些值转换为工作正常的字符串,只是由于某种原因无法划分该值。我已经尝试了很多方法来做到这一点,但我似乎遇到了错误,例如 - SyntaxError:无法分配给运算符和 TypeError:无法连接 'str' 和 'int' 对象。有没有人知道如何划分 python 字符串然后将该字符串用作睡眠值?
我对编程还很陌生(我 13 岁)
from ConfigParser import SafeConfigParser
config = SafeConfigParser()
config.read('/home/pi/config.conf') #read config file
openTime = config.get('blinds', 'open time(secs)') # -> "openTime"
print 'Overall open time ' + openTime + ' seconds'
## code to divide openTime by 5 and print the value
print 'gradual open time' + DividedOpenTime + ' seconds'
## code to repeat in a loop 5 times with a sleep of the value of
## DividedOpenTime
编辑 1
您好,我已经完成了我被告知要添加到代码中的操作,它已经解决了除法和睡眠时间的问题,但由于某种原因我无法打印这些值。第11行和第15行都有如下错误:
Traceback (most recent call last):
File "test.py", line 11, in <module>
print 'gradual open time' + divOpenTime + ' this will repeat 5 times'
TypeError: cannot concatenate 'str' and 'float' objects
有谁知道我该如何解决这个问题?
from ConfigParser import SafeConfigParser
config = SafeConfigParser()
import time
config.read('/home/pi/config.conf') #read config file
openTime = config.get('blinds', 'open time(secs)') # -> "openTime"
print 'Overall open time ' + openTime + ' seconds'
divOpenTime = float(openTime)/5 #working (:
loop = 0
print 'gradual open time' + divOpenTime + ' this will repeat 5 times'
for x in range(0, 5):
loop += 1
print 'gradual opening stage ' + loop + '/5'
#GPIO true- I know how to do this
time.sleep(divOpenTime) #working (:
#GPIO False- I know how to do this
time.sleep(15) #time between each interval fixed value
谢谢埃德
您需要将字符串转换为数字才能用它进行计算,
DividedOpenTime = float(openTime)/5
我解决了
print 'Gradual open time ' + str(divOpenTime) + ' seconds this will repeat 5
times'
您必须 add/convert 将您的浮点值转换为字符串以便打印。你做
这与:
str(Your_float_value_here)
再次感谢您的帮助 Ed
我从事自动百叶窗/窗帘项目已有一段时间了,一切正常,但我想添加百叶窗在早上逐渐打开的功能。我正在从配置文件中读取我的值,并将这些值转换为工作正常的字符串,只是由于某种原因无法划分该值。我已经尝试了很多方法来做到这一点,但我似乎遇到了错误,例如 - SyntaxError:无法分配给运算符和 TypeError:无法连接 'str' 和 'int' 对象。有没有人知道如何划分 python 字符串然后将该字符串用作睡眠值?
我对编程还很陌生(我 13 岁)
from ConfigParser import SafeConfigParser
config = SafeConfigParser()
config.read('/home/pi/config.conf') #read config file
openTime = config.get('blinds', 'open time(secs)') # -> "openTime"
print 'Overall open time ' + openTime + ' seconds'
## code to divide openTime by 5 and print the value
print 'gradual open time' + DividedOpenTime + ' seconds'
## code to repeat in a loop 5 times with a sleep of the value of
## DividedOpenTime
编辑 1
您好,我已经完成了我被告知要添加到代码中的操作,它已经解决了除法和睡眠时间的问题,但由于某种原因我无法打印这些值。第11行和第15行都有如下错误:
Traceback (most recent call last):
File "test.py", line 11, in <module>
print 'gradual open time' + divOpenTime + ' this will repeat 5 times'
TypeError: cannot concatenate 'str' and 'float' objects
有谁知道我该如何解决这个问题?
from ConfigParser import SafeConfigParser
config = SafeConfigParser()
import time
config.read('/home/pi/config.conf') #read config file
openTime = config.get('blinds', 'open time(secs)') # -> "openTime"
print 'Overall open time ' + openTime + ' seconds'
divOpenTime = float(openTime)/5 #working (:
loop = 0
print 'gradual open time' + divOpenTime + ' this will repeat 5 times'
for x in range(0, 5):
loop += 1
print 'gradual opening stage ' + loop + '/5'
#GPIO true- I know how to do this
time.sleep(divOpenTime) #working (:
#GPIO False- I know how to do this
time.sleep(15) #time between each interval fixed value
谢谢埃德
您需要将字符串转换为数字才能用它进行计算,
DividedOpenTime = float(openTime)/5
我解决了
print 'Gradual open time ' + str(divOpenTime) + ' seconds this will repeat 5
times'
您必须 add/convert 将您的浮点值转换为字符串以便打印。你做 这与:
str(Your_float_value_here)
再次感谢您的帮助 Ed