简体   繁体   English

如何将字符串转换为数据类型变量?

[英]How to convert string to a datatype variable?

I have a.c file, which contains below line我有一个 .c 文件,其中包含以下行

uint32 num_of_entries = 65;

I am writing an automation script to open and create similar 'c' file and write the line "uint32 num_of_entries = " int(65)我正在编写一个自动化脚本来打开并创建类似的'c'文件并写入行“uint32 num_of_entries =” int(65)

Both files look similar, but the script created file throws undefined reference.这两个文件看起来相似,但脚本创建的文件会引发未定义的引用。 I think it is because of how I am writing in string format.我认为这是因为我如何以字符串格式编写。 If that is the case do I need to change the string to any data type?如果是这种情况,我是否需要将字符串更改为任何数据类型?

I am writing it as file.write("uint32 num_of_entries = " + int(65))我把它写成file.write("uint32 num_of_entries = " + int(65))

You are close, but you need to use f-strings or the .format() function to format the output:你很接近,但你需要使用 f-strings 或.format() function 来格式化 output:

# Using an f-string
file.write(f"uint32 num_of_entries = {65}")

# Using the `.format()` function:
file.write("uint32 num_of_entries = {0}".format(65))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM