Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open file as formatted string?

I have a series of files with lots of variables defined in the form {myvar} inside. e.g.

file.txt

This is {myvar}.

I want to open them, and have the variables being replaced normally:

with open('path/to/file.txt', 'r') as file:
    myfile = file.read().replace('\n', '')


myvar='myself.'
print(f"{myfile}")

Should output:

This is myself.

How can I open the file as a formatted string? Or convert the string to formatted string?

like image 832
Sos Avatar asked May 13 '19 16:05

Sos


People also ask

What's a formatted string?

String formatting uses a process of string interpolation (variable substitution) to evaluate a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.

Which format is used for string?

In java, String format() method returns a formatted string using the given locale, specified format string, and arguments. We can concatenate the strings using this method and at the same time, we can format the output concatenated string. Parameter: The locale value to be applied on the format() method.

How do I open a file in a string format?

Files in the .strings format can be opened and viewed using the Apple Xcode program, among others like the MacroMates TextMate software and Apple TextEdit.

How do I format a string in Python?

The format () method formats the specified value (s) and insert them inside the string's placeholder. The placeholder is defined using curly brackets: {}. Read more about the placeholders in the Placeholder section below. The format () method returns the formatted string. string .format ( value1, value2... ) value1, value2... Required.

How to read file to string in Java?

Learn to read file to string in Java. Given examples use Files.readAllBytes (), Files.lines () (to read line by line) and FileReader & BufferedReader to read text file to String. 1. Files.readString () – Java 11 With the new method readString () introduced in Java 11, it takes only a single line to read a file’s content in to String.

What is string formatting in programming?

String formatting is the process of inserting variables into a string. In modern languages generally, it is done by using the {} brackets. It is also called string interpolation. Many languages have a built-in string method to format strings, but not all languages have it. Another very famous approach to string formatting is concatenation.


2 Answers

This seems to work if the variable is local to the call, if it's a global variable, use **globals(). You can also put the value in a dictionary that has the variable name as the key.

myvar = 'myself'
newline = '\n'  # Avoids SyntaxError: f-string expr cannot include a backslash

with open('unformatted.txt', 'r') as file:
    myfile = f"{file.read().replace(newline, '')}".format(**locals())

print(myfile)
like image 192
martineau Avatar answered Oct 18 '22 21:10

martineau


You are almost there.

Assume there is "This is {myvar}." in file.txt.

Coding:

with open('path/to/file.txt', 'r') as file:
    myfile = file.read().replace('\n', '')

myvar='myself'

print(myfile.format(myvar=myvar))

Any strings in plain text format, can be imported as a variable from a text file. Then the strings variable can be manipulated as normal.

In you case, the key is "{var}". So you can easily use ".format(var=varx) as long as you defined variable "varx".

Output:

This is myself.

And if you want to import html template with css style and replace some contents, you can just use "{{" and "}}" to escape "{" and "}".

Example 2 In file:

This is {myvar} for a {{var}}.

Output:

This is myself for a {var}.

like image 3
AC at CA Avatar answered Oct 18 '22 20:10

AC at CA