๐ Before you dive into this article...
๐ Check out our vibrant new community at CoderLegion.com!
๐ก Share your knowledge, connect with like-minded developers, and grow together.
๐ Click here to join now!
As serialized data structures, Python programmers make extensive use of arrays, lists, and dictionaries. Persistent storage of these data structures requires a file or a database to work with.
For writing data to a file or reading it from a file, the Python programming language offers the standard methods for and for dealing with a single line, as well as for and for dealing with multiple lines. Additionally, both the and the module also allow for smart ways of dealing with serialized data sets.write()read()writelines()readlines()picklejson
Use read and write methods
For dealing with characters (strings), the basic methods work excellently. Saving such a list line by line in the file can be done as follows:listfile.txt
places = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open('listfile.txt', 'w') as filehandle:
for listitem in places:
filehandle.write('%sn' % listitem)
In line 6 it listitemis extended with a newline "n", first, and secondly it is stored in the output file. To read an entire list from the list file into memory, this Python code shows you how it works:
open file to read a content in a list
with open('listfile.txt', 'r') as filehandle:
for line in filehandle:
remove line break which is a last character of the string
currentPlace = line[:-1]
add item to the list
places.append(currentPlace)
Note that you will need to remove the line break from the end of the String. In this case, it helps that Python also allows list operations on strings. In line 8 of the code above, this removal is done simply as a list operation on the string itself, keeping everything but the last item. This element contains the character "n" that represents the line break on UNIX / Linux systems.
Using the write lines and read lines methods
As mentioned at the beginning of this article, Python also contains the two methods and for writing and reading multiple lines in one step, respectively. To write the entire list to a file on disk, the Python code is as follows:
writelines()readlines()
places_list = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open('listfile.txt', 'w') as filehandle:
file handle.write lines("sn" % place for place in places_list)
To read the complete list from a file on disk, the Python code is as follows:
open file and read the content in a list
with open('listfile.txt', 'r') as filehandle:
filecontents = filehandle.readlines()
for line in filecontents:
remove line break which is a last character of the string
current_place = line[:-1]
add item to the list
places.append(current_place)
The above list follows a more traditional approach taken from other programming languages. To write it in a more pythonic way, take a look at the code below:
open file to read the content in a list
with open('listfile.txt', 'r') as filehandle:
places [current_place.rstrip() for current_place in filehandle.readlines()]
Having opened the file on line 5, the list reset is done completely on line 6. First, the content of the file is read through . Second, in a loop of each line, the line break character is removed using the method. Third, the string is added to the list of places as a new item in the list. Compared to the list above, the code is much more compact, but it can be more difficult for beginning Python programmers to read.listfile.txtreadlines()forrstrip()
Using the pickle module
The different methods explained so far store the list so that humans can still read it. In case this is not necessary, Pickle Module can be quite useful for you. Its method efficiently stores the list as a binary data stream. First, on line 7 (in the following code) the output file is opened for binary writing ("wb"). Second, on line 9 the list is stored in the file opened using the method.dump()listfile.data
dump()
load additional module
import pickle
define a list of places
placesList = ['Berlin', 'Cape Town', 'Sydney', 'Moscow']
with open('listfile.data', 'wb') as filehandle:
store alldata as binary data stream
pickle.dump(placesList, filehandle)
As a next step, we read the list from the file as follows. First, the output file is opened binary for reading ("rb") on line 4. Second, the list of places is loaded from the file using the method.listfile.dataload()
load additional module
import pickle
with open('listfile.data', 'rb') as filehandle:
read the data as a binary data stream
placesList = pickle.load(filehandle)
The two examples here demonstrate the use of strings.It pickle works with all kinds of Python objects, such as strings, numbers, self-defining structures, and any other built-in data structures that Python provides.
Hope it helps you.
Top comments (0)