This question already has an answer here:
Int conversion not working [duplicate]
(1 answer)
Closed 6 years ago.
Is there some python way to convert a text (from a file for example) into a format string?
I mean for a text file:
this is a {format}
string.
Load it in python and have it become like the triple quotes format string:
var = """this is a {format}
string."""
I know how to just read the file and replace the curly braces, but I was wondering if there is already something that does this.
Thanks
Edit:
This is the code I've tried:
with open('file.txt', 'r') as rs:
lines = rs.readlines()
text = ','.join(lines)
print(text)
text.format(format='something_else')
print(text)
It just prints the text file.
I'm looking to know if there is a more pythonic way then me having to write a class that does this.
Thanks
variable.format() returns the formatted text and does not change the content of variable. Try
print(text.format(format="bla"))
or
text = text.format(format="bla")
Related
This question already has answers here:
How to search for a string in text files?
(13 answers)
Closed 1 year ago.
Hello i am using python combined with txt file and want to let my code search for parts of a word, like my txt file looks like this =
'www.test.com/product/wings' , 'www.test1.com/product/chicken' , 'www.okay.com/product/burger' , 'www.Test23542.com/product/smoothie'
So and i want to let my code open the file and search for 'www.test1.com/product/' and it should print out the complete link, included the product like = 'www.test1.com/product/chicken'
I am stuck and dont know how to do it so my current code wont really help because its just one line.
Here is an example. Use open and then read or readline to read in the date in the file and then use str() methods to find what you need,
This question already has answers here:
Passing meta-characters to Python as arguments from command line
(4 answers)
Closed 4 years ago.
Let's say I have an example file named 'greetings.txt' with this in it
Hello\nThere
and this code
f = open("greetings.txt", "r")
readit = f.read()
print(readit)
But the output is
Hello\nThere
What do I do to make the output detect the "\n" and put Word "There" to the 2nd line?
Thanks for your answers!
Try this:
print(readit.replace(r'\n','\n'))
(When an 'r' or 'R' prefix is present, a character following a backslash is included in the string without change, and all backslashes are left in the string. See here)
This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
Closed 5 years ago.
I'm new to python and I was wondering how to put a variable inside a string to show the contents of such variable in an LCD 20x4 display (2004A).
The code I have looks as follows:
with open("temperature.txt") as myfile:
currenttemp=myfile.readlines()
lcd_string("%(currenttemp)" ,LCD_LINE_1,2)
Unfortunately, this prints "%(currenttemp)" in the LCD display, and not the currenttemp variable value.
An example of string formatting in Python:
Code:
a = "one line file"
print("this file contains: %s" %a)
Output:
this file contains: one line file
For performance comparison of various string formatting technique see here: Python string formatting: % vs. .format
This question already has answers here:
Get plain text from QLineEdit
(2 answers)
Closed 7 years ago.
I am using PyQt and want to get string from text field. I found below solution but it returns a QString. I just want to avoid writing extra code to extract string further. Can anyone suggest a simple solution to retrieve text from text field.
text = self.your_plugin_dlg.ui.yourLineEdit.text()
Thanks.
The only way to get the text is converting the QString into a String
text = str(self.your_plugin_dlg.ui.yourLineEdit.text())
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to join list of strings?
I have a couple of functions in a module to run. First, I have to use read() to read strings in a document and save it as new_string and run a function. Then, I need to read a document using readlines(). After running the first function like match = clean_passage(new_string) then match contains ['/n', 'asdf', 'dfg']. Now, I need them in a line as it is shown in the original document. So, asdf dfg. How can I convet match into a thing that contains strings in a similar fashion that we get when we read a document using readlines().
So far, to do this, I had to save it and then open it using readlines(), which takes time. Is there any way to do that using a simple command? Sorry if the explanation is not clear.
try this:
to_convert = ['/n', 'asdf', 'dfg']
original_line = " ".join(to_convert)