Sunday, June 26, 2022

[FIXED] How to make a print("{}") with another File?

Issue

I want to make the print({}) display something from another File. What I have been trying:

main.py:

with open("Filename.txt", "tr") as f:
  data = f.readlines()
 if "print({})" in data:
  print("{}".format(data))

Filename.txt:

print("Hello world")

result:

This means I have made an error. But there were no error messages. What did I do wrong?


Solution

It will search for the exact string print({}). It seems like you need the text within that print statement, its better to use regex and find text between print( and ). See https://stackoverflow.com/a/3369000/7334699

I don't know what your goal exactly is, but you can also take a look at the eval() function which just runs python code within a string.



Answered By - ToTheMax
Answer Checked By - Marilyn (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.