简体   繁体   中英

How to convert string to a datatype variable?

I have a.c file, which contains below line

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)

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))

You are close, but you need to use f-strings or the .format() function to format the 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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