Python Read Text File and Python Write to Text file, CSV files etc
Table of Contents
Python read and write text files:
Python Read and Write- Before you can read or write a text file in Python, you must open the file with open. You pass the file name in the first parameter and in the second the desired access mode (see table1).
open delivers a file object that can now be edited with various methods (see table 2). file is one of the elementary Python data types. You need i.e. not importing a module to perform simple file operations.
Table 1: Access modes of the open method
mode | Importance |
‘r’ | Read file (applies by default) |
‘w’ | Write file; existing files will be overwritten! |
‘a’ | write to the end of an existing file (append) |
‘r+’ | Read and write file |
Amazon Purchase Links:
*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
Table 2: Important methods for file objects
method | Importance |
s=f.read() | Reads the entire file. |
s=f.read(n) | Reads n bytes and delivers them as a character string. |
s=f.readline() | Reads a line from the file. |
f.write(s) | Writes the string s to the file. |
n=f.tell() | Indicates the current read / write position. |
f.seek(n, offset) | Changes the read / write position. |
close() | Closes the file. |
close() terminates access to the file and gives it back to other programs. You can do without close() in short scripts – at the end of the program all open files are closed in any case. Unless it’s the structure your code allows you to use all of the code to manipulate a file as in the Formulate the next example with open() as f. You won’t save yourself with that only the explicit close() call, you also make sure that the file is also closed when an error occurs.
With readline() you can read a file line by line. readline() always returns including the end of line character of each line, i.e. \ n on the Raspberry Pi. When the end of the file is reached, readline() returns an empty string. Because empty Lines within the file consist of at least \n, there are no ambiguities here. To process a text file line by line, you can also just use a for loop and save yourself the hassle with readline():
1 2 3 4 |
#! / usr / bin / python3 with open ('test .py') as f: for line in f: print (line, end = '') |
When writing text files don’t forget to include \n in the strings, you want to output. write() doesn’t care about line endings! Also note that, unlike print, write takes only one parameter and that this parameter really has to be a string. If you pay or other data in the text file, you must first save it in Convert strings.
The following program produces a text file with ten lines:
1 2 3 4 |
#! / usr / bin / python3 with open ('test.txt', 'w') as out: for i in range (1, 11): out. write ('line% d \ n'% i) |
CSV files in Python
CSV files (Comma Separated Values) are text files with tabular data. The elements or columns are marked with a comma “,” depending on the origin of the data separated by a semicolon. Strings can also be split into double Quotation marks. Help with reading and writing such files the methods of the csv module.
With reader() or writer() you create a CSV read or a CSV write object; pass the constructor a previously created file object and some parameters, which describe the syntax of your CSV file (see table 3).
Table 3: CSV format parameters
Parameter | Importance |
delimiter | Specifies the column separator (default ‘,’). |
lineterminator | Specifies the line separator (s) (‘\ r \ n’). |
quotechar | Specifies how character strings are marked (‘”‘). |
skipinitialspace | Specifies whether blank lines should be skipped (False). |
strict | Indicates whether an error is triggered in the event of syntax errors (False). |
escapechar | Specifies which character the writer prefixes special characters. |
doublequote | Indicates whether apostrophes are doubled (True). |
lineterminator only applies to writer objects. When reading, the reader comes automatically with any combination of ‘\r’ and \n. doublequote indicates how the CSV module should deal with the special case that a character string itself contains the quotechar character. By default, the character is then doubled doublequote = false, on the other hand, the character becomes the character to be set with escapechar in front, usually a \ -sign. Note that it is for escapechar there is no default setting.
To read a line with the CSV reader, simply form a for loop. The Loop variable contains the individual elements as a list:
1 2 3 4 5 6 7 |
#! / usr / bin / python3 import csv with open ('/ var / log / temp .csv') as f: c = csv. reader (f, delimiter = ';') for line in c: print (line) # Output ['Column 1', 'Column 2', ...] |
With the CSV writer, the writerow method inserts a line in the CSV file. The elements must be transferred as a list:
1 2 3 4 5 6 7 8 9 10 |
#! / usr / bin / python3 import csv with open ('out. csv', 'w') as out: cw = csv. writer (out, delimiter = ';', lineterminator = '\ n', quotechar = '"') cw. writerow (['Column 1', 2, 'Column 3', 1/4]) cw. writerow (['Text with "and; and \\', 'äöü']) # Content of out. csv # Column 1; 2; Column 3; 0.25 # "Text with" "and; and \"; äöü |
Note that the CSV writer only has to put strings in quotation marks if otherwise a correct reading would not be possible. If every element should be enclosed in quotes, pass it to the constructor from writer the additional parameter quoting = csc.QUOTE_ALL.
Process measurement data
Let’s say you are monitoring a temperature with a 1-wire temperature sensor and save them regularly in a CSV file that has the following format:
2020 -01 -27 T10: 35, t = 23437
2020 -01 -27 T10: 40, t = 23500
…
From this file you want the average temperature, the minimum, for each day and extract the maximum. The result should be saved in two files: an easy-to-read text file and a CSV file. The text file should look like this:
2020 -01 -27 Minimum: 18.7 ° C Maximum: 24.3 ° C Average: 21.1 ° C
The CSV file, however, should use the standard formatting of the CSV writer:
2020 -01 -27, 18.7, 24.3, 21.1
The required Python code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
#! / usr / bin / python3 # Templog .py file import csv with open ('/ var / log / temp .csv') as csvfile, \ open ('temperatures. txt', 'w') as txtout, \ open ('temperatures. csv', 'w') as csvout: cr = csv. reader (csvfile, delimiter = ',') cw = csv. writer (csvout) date before = '' temperatures = [] # Loop over all lines of the CSV file for line in cr: try: date = line [0] [: 10] # 1st column, the # first 10 characters temp = int (line [1] [2:]) / 1000 # 2nd column, from # the 2nd character if date == date before: # Collect temperature values in a list temperatures. append (temp) else: if len (temperatures)> 0: # Output temperatures txt = '% s minimum:% .1f ° C maximum:% .1f ° C Average:% .1 f ° C \ n ' tmin = min (temperatures) tmax = max (temperatures) trough = sum (temperatures) / len (temperatures) txtout. write (txt% (date, tmin, tmax, tby)) cw. writerow ([date before, '% .1 f'% tmin, '% .1f'% tmax, '% .1f'% tby]) # new list for the next day temperatures = [temp] date before = date except: print ('Syntax error in CSV file, the erroneous line is: ') print (line) |
Here are a few notes: with open three files for reading and writing. In the Line date = … the first element is read from the list in line first. Must do it is a character string from which the first 10 characters are read will. This is the date.
temp = … acts similarly and extracts all characters except for the second column the first two (i.e. ‘t =’). The string is interpreted as a number with int and divided by 1000. If the CSV file does not comply with the prescribed format, a lot can go wrong – hence the error protection with try.
All temperatures for a day are saved in the temperatures list. If the date changes, so the condition date == date before is no longer met, the program determines the minimum, the maximum and the average value of temperatures. write writes the result to the text file Temperaturen.txt. The List temperatures is now re-initialized with the temperature of the new date. Note that the measurement data of the last day are not included in the result.