- Как записать текст на русском в файл UTF-8?
- Python Write Text File
- TL;DR
- Steps for writing to text files
- Writing text file examples
- Appending text files
- Writing to a UTF-8 text file
- Summary
- How to Use Python to Write a Text File (.txt)
- How to Use Python to Write to a Text File
- Writing Multiple Lines to a Text File Using Python
- How to Append to a Text File in Python
- How to Write UTF-8 Encoded Text to a File in Python
- Conclusion
- Additional Resources
Как записать текст на русском в файл UTF-8?
1. Почему файлы ‘str_ru_text_1.txt’, ‘str_ru_text_2.txt’ в кодировке Windows 1251, а файлы ‘str_en_text_1.txt’, ‘str_en_text_2.txt’ — в UTF-8?
2. Есть ли способ записать utf-8 без str.encode(‘utf-8’)?
3. Где описаны правила записи строк в файл? Где и что почитать по этой проблеме?
with open('str_ru_text_1.txt', 'rb') as f: print(f.read().decode('utf-8')) with open('str_en_text_1.txt', 'rb') as f: print(f.read().decode('utf-8'))
Оценить 2 комментария
>а файлы ‘str_en_text_1.txt’, ‘str_en_text_2.txt’ — в UTF-8?
Как ты определил по файлу с ascii символами в какой они кодировке?
lololololo: см. ветку комментариев к первому комментарию (началось с прикладного софта, закончилось подбором кодировки через decode): https://toster.ru/answer?answer_id=344879#comments_list
Товарищи, это писец какой-то. Хотели как лучше, а получилось еще более через жопу.
mode is an optional string that specifies the mode in which the file is opened. <. >In text mode, if encoding is not specified the encoding used is platform dependent: locale.getpreferredencoding(False) is called to get the current locale encoding. (For reading and writing raw bytes use binary mode and leave encoding unspecified.)
1. Если не указан режим ‘b’, то по умолчанию файл считается текстовым. В двоичный файл можно писать только байты, в текстовый — только юникод.
(В текстовом режиме файл читается только до EOF (‘\x1a’). Как совместить чтение до конца файла и запись юникода в файл? А никак.)
2. Если кодировка не указана, по умолчанию берется locale.getpreferredencoding(False), т.е. результат выполнения будет зависеть от настроек оси! (для винды — от текущей локали). Нахера. От одних граблей избавились, другие приобрели.
В общем, всегда явно указывай явно кодировку файла.
with open('str_ru_text_1.txt', 'w', encoding='utf-8') as f:
Из вопроса:
>> Python 3.4, Windows 8.1
С codecs не встречался, а с двойкой не работаю. Чем codecs.open от open отличается (в Python 3)?
Если только для двойки, то не стоит тратить время на объяснение, спасибо. А с параметром encoding еще тогда разобрались, но без источников. Спасибо за ссылку.
Python Write Text File
Summary: in this tutorial, you’ll learn various ways to write text files in Python.
TL;DR
The following illustrates how to write a string to a text file:
with open('readme.txt', 'w') as f: f.write('readme')
Code language: JavaScript (javascript)
Steps for writing to text files
To write to a text file in Python, you follow these steps:
- First, open the text file for writing (or append) using the open() function.
- Second, write to the text file using the write() or writelines() method.
- Third, close the file using the close() method.
The following shows the basic syntax of the open() function:
The open() function accepts many parameters. But you’ll focus on the first two:
- The file parameter specifies the path to the text file that you want to open for writing.
- The mode parameter specifies the mode for which you want to open the text file.
For writing to a text file, you use one of the following modes:
Mode | Description |
---|---|
‘w’ | Open a text file for writing. If the file exists, the function will truncate all the contents as soon as you open it. If the file doesn’t exist, the function creates a new file. |
‘a’ | Open a text file for appending text. If the file exists, the function append contents at the end of the file. |
‘+’ | Open a text file for updating (both reading & writing). |
The open() function returns a file object that has two useful methods for writing text to the file: write() and writelines() .
- The write() method writes a string to a text file.
- The writelines() method write a list of strings to a file at once.
The writelines() method accepts an iterable object, not just a list, so you can pass a tuple of strings, a set of strings, etc., to the writelines() method.
To write a line to a text file, you need to manually add a new line character:
f.write('\n') f.writelines('\n')
Code language: JavaScript (javascript)
Writing text file examples
The following example shows how to use the write() function to write a list of texts to a text file:
lines = ['Readme', 'How to write text files in Python'] with open('readme.txt', 'w') as f: for line in lines: f.write(line) f.write('\n')
Code language: JavaScript (javascript)
If the readme.txt file doesn’t exist, the open() function will create a new file.
The following shows how to write a list of text strings to a text file:
lines = ['Readme', 'How to write text files in Python'] with open('readme.txt', 'w') as f: f.writelines(lines)
Code language: JavaScript (javascript)
If you treat each element of the list as a line, you need to concatenate it with the newline character like this:
lines = ['Readme', 'How to write text files in Python'] with open('readme.txt', 'w') as f: f.write('\n'.join(lines))
Code language: JavaScript (javascript)
Appending text files
To append to a text file, you need to open the text file for appending mode. The following example appends new lines to the readme.txt file:
more_lines = ['', 'Append text files', 'The End'] with open('readme.txt', 'a') as f: f.write('\n'.join(more_lines))
Code language: JavaScript (javascript)
Writing to a UTF-8 text file
If you write UTF-8 characters to a text file using the code from the previous examples, you’ll get an error like this:
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-44: character maps to undefined>
Code language: HTML, XML (xml)
To open a file and write UTF-8 characters to a file, you need to pass the encoding=’utf-8′ parameter to the open() function.
The following example shows how to write UTF-8 characters to a text file:
quote = '成功を収める人とは人が投げてきたレンガでしっかりした基盤を築くことができる人のことである。' with open('quotes.txt', 'w', encoding='utf-8') as f: f.write(quote)
Code language: JavaScript (javascript)
Summary
- Use the open() function with the w or a mode to open a text file for appending.
- Always close the file after completing writing using the close() method or use the with statement when opening the file.
- Use write() and writelines() methods to write to a text file.
- Pass the encoding=’utf-8′ to the open() function to write UTF-8 characters into a file.
How to Use Python to Write a Text File (.txt)
In this tutorial, you’ll learn how to use Python to write (or save) to a text file. Python provides incredible opportunity to read and work with text files – being able to save the output to a text file is an important skill. Python can handle both regular text files and binary files – in this tutorial, you’ll learn how to work with text files.
By the end of this tutorial, you’ll have learned:
- How to use Python to write to a .txt file
- How to use a context manager to safely write to a text file
- How to append text to a file in Python
- How to write UTF-8 text to a File in Python
How to Use Python to Write to a Text File
Python provides a number of ways to write text to a file, depending on how many lines you’re writing:
- .write() will write a single line to a file
- .writelines() will write multiple lines to a file
These methods allow you to write either a single line at a time or write multiple lines to an opened file. While Python allows you to open a file using the open() , it’s best to use a context manager to more efficiently and safely handle closing the file.
Let’s see what this looks like:
# Writing a Single Line to a Text File text = 'Welcome to datagy.io!' with open('/Users/nikpi/Desktop/textfile.txt', 'w') as f: f.write(text)
Let’s break down what the code above is doing:
- We load a string that holds our text in a variable text
- We then use a context manager to open a file in ‘w’ mode, which allows us to overwrite an existing text
- The file doesn’t need to exist – if it doesn’t, it’ll automatically be created
- When then use the .write() method to write our string to the file
Writing Multiple Lines to a Text File Using Python
In many cases, you may not want to write a single line of text to a file. Let’s take a look at how we can write multiple lines of text to a file using the .write() method:
# Writing Multiple Lines to a Text File text = ['Welcome to datagy.io!', "Let's learn some Python!"] with open('/Users/nikpi/Desktop/textfile.txt', 'w') as f: for line in text: f.write(line) f.write('\n')
Let’s take a look at what we’re doing in the code above:
- We load a list of strings that contains the lines of text we want to save
- Similar to the previous example, we open the file using a context manager
- We then loop over each item in the list to write each string to the file
- For each item, we also write a newline character so that each line is split across a new line
The approach above feels a bit clunky. We can simplify this process by using the .writelines() method, which allows us to write multiple lines at once. Let’s see how we can modify our code above to use the .writelines() method:
# Writing Multiple Lines to a Text File (with .writelines) text = ['Welcome to datagy.io', "Let's learn some Python!"] with open('/Users/nikpi/Desktop/textfile.txt', 'w') as f: f.writelines('\n'.join(text))
In the code above, we avoid using a for loop to write multiple lines of text to a file. Because our text isn’t separated by newline characters, we use the .join() string method to place each item onto a new line.
How to Append to a Text File in Python
In the previous sections, you learned how to write a new file with text in Python. In this section, you’ll learn how to append to a given text file using Python. We previously used the write mode, ‘w’ when opening the file – in order to append, we use the append mode, ‘a’ .
Let’s see how we can append to a text file in Python:
# Appending to a Text File in Python text = 'Welcome to datagy.io!\n' with open('/Users/nikpi/Desktop/textfile.txt', 'a') as f: f.write(text)
Running this will append to the end of the text file. Note that we applied the newline character into the string. This could also be done in the context manager, depending on how you prefer your code to run.
Similarly, we can append multiple lines to a file by using the .writelines() method, as shown below:
# Appending Multiple Lines to a Text File text = ['Welcome to datagy.io!\n', 'Python is fun!\n'] with open('/Users/nikpi/Desktop/textfile.txt', 'a') as f: f.writelines(text)
How to Write UTF-8 Encoded Text to a File in Python
Python will open a file using the system’s default encoding. While UTF-8 is the de-facto standard, your system may not open the file using that encoding format. Because of this, you may need to specify the encoding format when opening the file.
Let’s see how we can do this:
# Specifying Encoding to UTF-8 text = 'é' with open('/Users/nikpi/Desktop/textfile.txt', 'w', encoding="utf-8") as f: f.write(text)
Running this code on, for example, macOS, doesn’t require specifying the encoding. However, if you want your code to run platform independently, it is a good idea to specify the encoding.
Conclusion
In this tutorial, you learned how to use Python to write a text file. You first learned about the different ways of overwriting a file using the .write() and .writelines() methods. You learned about the nuances of inserting newline characters. Then, you learned how to append to an existing file, both single lines as well as multiple lines. Finally, you learned how to specify the encoding when writing a file.
Additional Resources
To learn more about related topics, check out the tutorials below: