[英]TypeError: Must be str not int
我正在使用此代码将带有浮点值的字符串连接起来:
fr = channel.freqhz / 1000000
print ("fr type is", type(fr))
rx_channel = "<RxChannel>\n\
<IsDefault>1</IsDefault>\n\
<UsedForRX2>" + channel.usedforrx2 + "</UsedForRX2>\n\
<ChIndex>" + str(i) + "</ChIndex>\n\
<LC>" + str(channel.index) + "</LC>\n\
<SB>" + channel.subband + "</SB>\n\
<DTC>100</DTC>\n\
<Frequency>" + str(fr) + "</Frequency>\n\
<MinDR>0</MinDR>\n\
<MaxDR>5</MaxDR>\n\
</RxChannel>\n"
但我收到此错误消息:
> fr type is <class 'float'>
> Traceback (most recent call last):
> File "createRFRegion.py", line 260, in <module>
> write_rf_region(rf_region_file, rf_region_filename)
> File "createRFRegion.py", line 233, in write_rf_region
> rf_region_file.write(create_rx_channel(channel, i))
> File "createRFRegion.py", line 164, in create_rx_channel
> <Frequency>" + str(fr) + "</Frequency>\n\
> TypeError: must be str, not int
我不明白此错误,因为我正在使用str()函数将float转换为str。
str()
。 Python无法很好地处理多行操作和错误,并且会指向多行操作的最后一行(我假设它会将最后一行中的所有字符串连接在一起,因此指向str(fr)
)。
字段fr
甚至不是int
,它是一个float
,因此错误来自其他字段之一。 我的猜测是channel.subband
,一个通道的子带将是一个整数。
rx_channel = "<RxChannel>\n<IsDefault>1</IsDefault>\n<UsedForRX2>" +
str(channel.usedforrx2) + "</UsedForRX2>\n<ChIndex>" +
str(i) + "</ChIndex>\n<LC>" +
str(channel.index) + "</LC>\n<SB>" +
str(channel.subband) + "</SB>\n<DTC>100</DTC>\n<Frequency>" +
str(fr) + "</Frequency>\n<MinDR>0</MinDR>\n<MaxDR>5</MaxDR>\n/RxChannel>\n"
channel.usedforrx2
和channel.subband
值是多少? 我怀疑其中之一是整数而不是字符串。 另外,为使代码更具可读性,请考虑将该语句替换为:
rx_channel = """<RxChannel>
<IsDefault>1</IsDefault>
<UsedForRX2>{}</UsedForRX2>
<ChIndex>{}</ChIndex>
<LC>{}</LC>
<SB>{}</SB>
<DTC>100</DTC>
<Frequency>{}</Frequency>
<MinDR>0</MinDR>
<MaxDR>5</MaxDR>
</RxChannel>
""".format(channel.usedforrx2, i, channel.index, channel.subband, fr)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.