I'm working in 'wb' mode with array of bytes
for i in range(len(mas)):
message.write(mas[i])
after I have to write data to a file on new line, for example '\n' in 'w' mode
for i in range(3):
message.write(str(i))
message.write("\n")
>>>0
>>>1
>>>2
>>>3
How can I do this?
To write a string to a binary file you, like "\n" into wb mode, you must first encode it by calling string.encode('utf-8') or any other encoding you need.
For example, to write a message to a binary file:
with open("a.txt", "wb") as f:
line = str(0) + "\n\n"
f.write(line.encode('utf-8'))
This would write 0\n\n. To write the numbers from 0 to 3, followed by blank lines:
with open("a.txt", "wb") as f:
for i in range(4):
line = str(i) + "\n\n"
f.write(line.encode('utf-8'))
Encoded newlines are printed as newlines correctly, so the following lines are equivalent:
open('a.txt', 'w').write('\n')
open('a.txt', 'wb').write('\n'.encode('utf-8'))
open('a.txt', 'wb').write(b'\n')
To print a literal \n, and not a newline, escape the backslash with another backslash \\n or write r'\n' to create a "raw" string.
I think you need to write a newline bytestring to your file after each of your bytearrays:
for i in range(len(mas)):
message.write(mas[i])
message.write(b"\n")
Note that a more natural (Pythonic) way of writing your loop is to iterate directly over mas, rather than iterating on a range and then indexing. You can also combine the two write calls into one:
for line in mas:
message.write(line + b"\n")
Related
I have a text file which contains \n (back-slash N) in the file's contents.
I want Python to interpret \n as a new line, rather than a literal \n string.
samplefile.txt contains
multi\nline
My Python code
file_path = "./samplefile.txt"
with open(file_path, "r") as f:
contents = f.read()
print(contents)
The print statement currently outputs this:
multi\nline
But I want it to output this:
multi
line
After I read the file's contents, how can I get Python to interpret \n in the file's contents as a new line?
What I've tried:
contents = repr(contents).replace("\\n", "\n")
print(contents)
But that outputs:
'multi\
line
'
Expected output:
multi
line
Any suggestions?
In the code you tried, just drop the call to repr(). It escapes the backslash, which is not what you want. So it's just this:
>>> content = r'multi\nline'
>>> print(content.replace("\\n", "\n"))
multi
line
I want to save some mathjax code to a .txt file in python.
x = "$\infty$"
with open("sampletext.txt", "a+") as f:
f.write(x)
Works exactly as expected
sampletext.txt
$\infty$
However when i try to save the escape sequence in a list
x = ["$\infty$"]
with open("sampletext.txt", "a+") as f :
f.write(str(x))
sampletext.txt
['$\\infty$']
How do i remove the double backslash in the latter and save it as ['$\infty$'] ?
Try this:
x = [r"$\infty$"]
with open("sampletext.txt", "a+") as f:
f.write(str(x))
The r means that the string is to be treated as a raw string, which means all escape codes will be ignored.
Maybe this can help you:
x = [r"$\infty$"]
with open("sampletext.txt", "a+") as f:
f.write(''.join(x))
Flag "r" (raw) can be use to save string with special symbols like "\"
Or if you don't know how many items in the list:
x = ["$\infty$"]
with open("sampletext.txt", "a+") as f:
f.write(f"{''.join(x)}")
I am trying to write a python script to convert rows in a file to json output, where each line contains a json blob.
My code so far is:
with open( "/Users/me/tmp/events.txt" ) as f:
content = f.readlines()
# strip to remove newlines
lines = [x.strip() for x in content]
i = 1
for line in lines:
filename = "input" + str(i) + ".json"
i += 1
f = open(filename, "w")
f.write(line)
f.close()
However, I am running into an issue where if I have an entry in the file that is quoted, for example:
client:"mac"
This will be output as:
"client:""mac"""
Using a second strip on writing to file will give:
client:""mac
But I want to see:
client:"mac"
Is there any way to force Python to read text in the format ' "something" ' without appending extra quotes around it?
Instead of creating an auxiliary list to strip the newline from content, just open the input and output files at the same time. Write to the output file as you iterate through the lines of the input and stripping whatever you deem necessary. Try something like this:
with open('events.txt', 'rb') as infile, open('input1.json', 'wb') as outfile:
for line in infile:
line = line.strip('"')
outfile.write(line)
i have the following output, which i want to write into a file:
l = ["Bücher", "Hefte, "Mappen"]
i do it like:
f = codecs.open("testfile.txt", "a", stdout_encoding)
f.write(l)
f.close()
in my Textfile i want to see: ["Bücher", "Hefte, "Mappen"] instead of B\xc3\xbccher
Is there any way to do so without looping over the list and decode each item ? Like to give the write() function any parameter?
Many thanks
First, make sure you use unicode strings: add the "u" prefix to strings:
l = [u"Bücher", u"Hefte", u"Mappen"]
Then you can write or append to a file:
I recommend you to use the io module which is Python 2/3 compatible.
with io.open("testfile.txt", mode="a", encoding="UTF8") as fd:
for line in l:
fd.write(line + "\n")
To read your text file in one piece:
with io.open("testfile.txt", mode="r", encoding="UTF8") as fd:
content = fd.read()
The result content is an Unicode string.
If you decode this string using UTF8 encoding, you'll get bytes string like this:
b"B\xc3\xbccher"
Edit using writelines.
The method writelines() writes a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value.
# add new lines
lines = [line + "\n" for line in l]
with io.open("testfile.txt", mode="a", encoding="UTF8") as fd:
fd.writelines(lines)
I tried to trim whitespace in python using s.strip() like this, but it's only working on the first line:
Input:
a
b
Output:
a
b
How do I get it to trim whitespace from multiple lines? Here's my code:
Code:
import sys
if __name__ == "__main__":
text_file = open("input.txt", "r")
s = text_file.read()
s = s.strip()
text_file.close()
with open("Output.txt", "w") as text_file:
text_file.write(s)
Split the lines, strip each, then re-join:
s = text_file.read()
s = '\n'.join([line.strip() for line in s.splitlines()])
This uses the str.splitlines() method, together with the str.join() method to put the lines together again with newlines in between.
Better still, read the file line by line, process and write out in one go; that way you need far less memory for the whole process:
with open("input.txt", "r") as infile, open("Output.txt", "w") as outfile:
for line in infile:
outfile.write(line.strip() + '\n')
The issue occurs because string.strip() only strips the trailing and leading whitespaces, it does not strip the whitespaces in the middle.
For the input -
a
b
And doing text_file.read() .
The actual string representation would be -
' a\n b'
s.strip() would strip the trailing and leading whitespaces , but not the \n and spaces in the middle, hence you are getting the multiple lines and the spaces in the middle are not getting removed.
For your case to work, you should read the input line by line and then strip each line and write it back.
Example -
import sys
if __name__ == "__main__":
with open("input.txt", "r") as text_file, open("Output.txt", "w") as out_file:
for line in text_file:
out_file.write(line.strip() + '\n')
Use
for line in s.splitlines()
to iterate over each line and use strip() for them.
Just for completeness, there is also textwrap.dedent(),
which e.g. allows to write multi-line strings indented in code (for readability), while the resulting strings do not have left-hand side whitespaces.
For example as given in https://docs.python.org/3/library/textwrap.html#textwrap.dedent
import textwrap
def test():
# end first line with \ to avoid the empty line!
s = '''\
hello
world
'''
print(repr(s)) # prints ' hello\n world\n '
print(repr(dedent(s))) # prints 'hello\n world\n'