python - How to write on new string uses byte 'wb' mode? -


i'm working in 'wb' mode array of bytes

for in range(len(mas)):     message.write(mas[i]) 

after have write data file on new line, example '\n' in 'w' mode

for in range(3):     message.write(str(i))     message.write("\n")  >>>0  >>>1  >>>2  >>>3 

how can this?

to write string binary file you, "\n" wb mode, must first encode calling string.encode('utf-8') or other encoding need.

for example, write message binary file:

with open("a.txt", "wb") f:     line = str(0) + "\n\n"     f.write(line.encode('utf-8')) 

this write 0\n\n. write numbers 0 3, followed blank lines:

with open("a.txt", "wb") f:     in range(4):         line = str(i) + "\n\n"         f.write(line.encode('utf-8')) 

encoded newlines printed newlines correctly, following lines 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 literal \n, , not newline, escape backslash backslash \\n or write r'\n' create "raw" string.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -