txt/csv file operation
Manipulating txt documents is a very important lesson. Writing, appending, and reading txt documents is very common in python programming.
write to txt
Writing txt is very simple, you can use the print function to write txt.
After running the above program, you will get a file called test.txt in the folder where the python program is located. The following is the content of the file.
You can also write to a file using the file's own features:
You can get the exact same file. (The write method can only write the content of the string type, so you need to convert the result to the string type to write it with write.)
The print function comes with a line break (you can use end='' to cancel the automatic line break), but the write function does not have a line break.
There are few things to note:
Using the print function can print any format, but using write can only write data of type string, so you need to convert the data to a string, so it seems easier to use the print function.
But the print function also has a disadvantage, that is, the efficiency is relatively low. If you need to write a large amount of content, it is best to use the write method.
When we use 'w' to open a file, all the content in the file will be deleted, so we need to pay special attention. If there is important content in your file, this operation will destroy the original content, and the lost content cannot be recovered.
After we write to a file, the computer uses a caching mechanism to first write a portion of the content to a memory or disk buffer instead of writing it directly to disk. So if you don't close a file, part of the content will be lost. So make sure to close the file after we write to it.
If your program may crash, but you don't want to lose data, using flush can make the cache write to disk immediately. The above are two different ways of writing to the cache immediately.
append to txt
The above code is to append to txt file.
Use 'a' to append the file, the original file content will not be deleted, the newly written file content will be appended at the end of the file.
Both print and write can be used for file appending.
The file after appending looks like this, if you run the code once, then the result is 0-19 repeated twice, and if you run the code twice, it is 0-19 repeated three times.
If test.txt does not exist, then using append will create a new file called test.txt, and then append whatever you want to the empty file.
read txt
The 'r' mode is used to read txt. When reading txt, the content in txt will not be deleted, but read-only mode is used.
The above program can print each line in txt.
Reading a txt file does not require closing the txt text, since you are not writing to the file and the file will not be locked by the OS, but it is still recommended to close the file after reading the entire contents of the text to avoid problems.
csv file
Comma-Separated Values are simply referred to as csv files.
For example, the following file contains the iris dataset. If you save it as iris.csv, you can open it with excel.
In the following chapters, it is detailed how to read the iris dataset and how to batch the strings into float format data. See the following chapters:
iris data setWriting to csv files is also very simple: for example, the following code can write some random numbers in csv:
The above code will generate a csv file with headers:
Use notepad to open the csv file.
At this time, the file is not in the form of cells, but in plain text. This format is not easy to observe the data. You can use excel to open the csv file:
It should be noted that csv needs to have the same number of commas in each line (the last comma is not counted), if the number of commas is different, the format may be messed up. If a cell is empty, it should be indicated by two consecutive commas: , ,
In addition, there is no space after the comma in csv, which is different from the English grammar.
csv can not only be separated by commas, but also tab (\t) or spaces or special symbols such as # or $, but excel cannot open these files correctly.
If the data you need to save already has commas in it, then you should not use commas as csv delimiters, because then commas in your data will also be treated as csv delimiters. At this point you should use the tab key.
Statistics
Start time of this page: January 15, 2022
Completion time of this page: January 16, 2022
Last updated