简体   繁体   中英

Read line containing variables from file

I need to read the row like:

"hello %" % name 

from a file and then use it in the code. For eg:

name = "Joe"
row = open("hello.txt", "r").readline().strip()
print row

And desired result of run of this program should be :

hello Joe

and not

hello %" % name 

Are there any means to accomplish it?

Replace the sub-string with the variable.

name = "Joe"
row = open("hello.txt", "r").readline().strip()
row = row.replace('%" % name', name)
print row

Output:

hello Joe

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