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)
Related
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I have a text file that looks like this:
../../../../foo/bar
../../this/that
../barfoo
and I want:
foo/bar
this/that
barfoo
with open('file_list.txt', 'r') as file_list:
for file_list_lines in file_list:
file_list_lines.lstrip('../')
print(file_list_lines)
I tried .lstrip('../') but nothing was stripped from the beginning of the line.
The string.lstrip() does not do the string manipulation in place. In other words, you would need to store it into a variable like so:
stripped_line = file_list_lines.lstrip('../')
print( stripped_line )
In your version, you did the lstrip, but did not store the result of that operation anywhere.
This question already has answers here:
In Python, is it possible to escape newline characters when printing a string?
(3 answers)
Closed 5 years ago.
I'm trying to write code to a python file from a python program
Example:
f.write("output.write(str(fib(int(line))) + '\n')")
I want the newline symbol to actually be written to the file and not place an actual newline in the file. How would I go about doing this?
You just need to escape the backslash with another backslash:
... + '\\n'
Or use raw strings:
... + r'\n'
try adding another backslash \ before the newline symbol like so:
f.write("output.write(str(fib(int(line))) + '\\n')")
This question already has answers here:
How can I remove a trailing newline?
(27 answers)
Closed 6 years ago.
i have some scripts that take as input one or multiple paths as on argument.
The script is run like that: myScript.py D:\Folder1,E:\OtherData\Files
In the script, i split the path arguments in the comma and i read the paths.
The problem is that Python adds a \r in the end of each path for no reason.
So the script tries to read D:\Folder1\r and E:\OtherData\Files\r.
Why is this and how can i solve it?
There are many ways, here is one for example.
Replacing the relevant problematic string with an empty string:
paths= [x.rstrip() for x in paths_list]
The method rstrip() returns a copy of the string in which all chars have been stripped from the end of the string (default whitespace characters).
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 7 months ago.
I have a .txt file form which I read multiple lines and append an array with every line.
Unfortunately I also have the line breaks in the array.
When I try to replace them with line.replace("\n", ""), nothing will happen.
Are you just doing line.replace("\n", "")? If so, that's the problem. You are doing the replacement, then throwing away the result. You need:
line = line.replace("\n", "")
I had the same problem, but even saving the result in another variable. I started breaking it in code units to find the problem and I found that my input had carriage returns, which is '\r'. They made visually the same result as the '\n' in the output file. So I fixed it by doing the following:
result = input.replace("\n", "").replace("\r", "");
This question already has answers here:
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
(7 answers)
Closed 7 years ago.
Very simple question for Python 2:
I am calling specific library/function passing filename with readonly flag:
myfunction(r'/tmp/file.txt')
I wanted to replace it with variable:
filename = '/tmp/file.txt'
myfunction(r????)
How can I call that function?
That is not readonly flag. That means raw string. You use it when you don't want escape sequences inside string to be interpreted (like \n, \t etc.) See https://docs.python.org/2.0/ref/strings.html
For your string, you don't need it since it does not contain any escape sequence. Just omit the leading r.
filename = r'/tmp/file.txt'
myfunction(filename)
in most cases you can use
myfunction(r''+filename)
if you don't want to define the r prefix in your variable. This works with b'' and u'' too.