Reputation: 394
I am using this code to concatenate strings with a float value :
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"
But I get this error message :
> 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
I don't understand this error because I am using the str() function to convert the float to str.
Upvotes: 2
Views: 6794
Reputation: 12927
What are values of channel.usedforrx2
and channel.subband
? I suspect one of them is int rather than string. Also, to make your code more readable, consider replacing that statement with:
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)
Upvotes: 2
Reputation: 2976
str()
in all fields.Python doesn't deal well with multi-line operations and errors, and will point at the last line of the multi-line operation (I'm assuming it is joining together all the strings in the last line, and thus pointing at str(fr)
).
The field fr
isn't even an int
, it's a float
, so the error is coming from one of the other fields. My guess is channel.subband
, a sub-band of a channel will be an integer.
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"
Upvotes: 4