This document describes how to read and write data using the input and output functions of Python.

Source: Internet
Author: User

This document describes how to read and write data using the input and output functions of Python.

Read, write, and Python

The last basic step for programming is to read data from a file and write data into a file. After reading this article, you can add a task to test the learning performance of this skill in your to-do list.
Simple output

Throughout the series, print statements are used to write (output) data. By default, the expression is written to the screen (or Console window) as a string ). Listing 1 demonstrates this. Listing 1 repeats the first Python program "Hello, World !", However, some small adjustments were made.
Listing 1. Simple output

>>> print "Hello World!"Hello World!>>> print "The total value is = $", 40.0*45.50The total value is = $ 1820.0>>> print "The total value = $%6.2f" % (40.0*45.50)The total value = $1820.00>>> myfile = file("testit.txt", 'w')>>> print >> myfile, "Hello World!">>> print >> myfile, "The total value = $%6.2f" % (40.0*45.50)>>> myfile.close()

As demonstrated in this example, it is easy to write data using the print statement. First, the sample outputs a simple string. Then create and output a composite string, which is created using the string formatting technique.

However, things have changed since then, unlike previous versions of the Code. In the next line, create a file object and pass it to the "testit.txt" and 'w' characters (written to the file ). Then, use the modified print statement -- two variables later than the number to hold the file object -- write the same string. But this time, the data is not displayed on the screen. Naturally, the question is: where is the data? What is the file object?

The first question is easy to answer. Find the testit.txt file and display its content as follows.

% more testit.txt Hello World!The total value = $1820.00

As you can see, the data is accurately written to the file, just as it was written to the screen before.

Now, pay attention to the last line in Listing 1, which calls the close method of the file object. This is very important in Python programs, because by default, file input and output are buffered; data is not actually written when the print statement is called; on the contrary, data is written in batches. The simplest way for Python to write data to a file is to explicitly call the close method.
Object

File is the basic mechanism for interacting with files on computers. You can use a file object to read data, write data, add data to a file, and process binary or text data.

The easiest way to learn file objects is to read help, as shown in Listing 2.
Listing 2. getting help from the file object

>>> help(file)Help on class file in module __builtin__:class file(object) | file(name[, mode[, buffering]]) -> file object |  | Open a file. The mode can be 'r', 'w' or 'a' for reading (default), | writing or appending. The file will be created if it doesn't exist | when opened for writing or appending; it will be truncated when | opened for writing. Add a 'b' to the mode for binary files. | Add a '+' to the mode to allow simultaneous reading and writing. | If the buffering argument is given, 0 means unbuffered, 1 means line | buffered, and larger numbers specify the buffer size. | Add a 'U' to mode to open the file for input with universal newline | support. Any line ending in the input file will be seen as a '\n' | in Python. Also, a file so opened gains the attribute 'newlines'; | the value for this attribute is one of None (no newline read yet), | '\r', '\n', '\r\n' or a tuple containing all the newline types seen. |  | 'U' cannot be combined with 'w' or '+' mode. |  | Note: open() is an alias for file(). |  | Methods defined here:...

As pointed out by the help tool, it is very easy to use file objects. Create a file object using the file constructor or open method. open is the alias of the file constructor. The second parameter is optional. It specifies the usage of the file:

  • 'R' (default) indicates reading data from a file.
  • 'W' indicates writing data to the file and truncating the previous content.
  • 'A' indicates the data to be written to the file, but added to the end of the current content.
  • 'R + 'indicates reading and writing files (deleting all previous data ).
  • 'R + a' indicates that the file is read and written (added to the end of the current content ).
  • 'B' indicates the binary data to be read and written.

The first code list in this article writes data to the file. Now, listing 3 shows how to read the data into a Python program and parse the file content.
Listing 3. Reading data from a file

>>> myfile = open("testit.txt")>>> myfile.read()'Hello World!\nThe total value = $1820.00\n'>>> str = myfile.read()>>> print str>>> myfile.seek(0)>>> str = myfile.read()>>> print strHello World!The total value = $1820.00>>> str.split()['Hello', 'World!', 'The', 'total', 'value', '=', '$1820.00']>>> str.split('\n')['Hello World!', 'The total value = $1820.00', '']>>> for line in str.split('\n'):...   print line... Hello World!The total value = $1820.00>>> myfile.close()

To read data, first create a suitable file object-in this example, open the testit.txt file and read the content using the read method. This method reads the entire file into a string and then outputs the string to the console in the program. In the second call to the read method, an attempt is made to assign the value to the str variable, and an empty string is returned. This is because the first read operation reads the entire file. When trying to read the content again, it is at the end of the file, so nothing can be read.

The solution to this problem is also simple: Let the file object return the beginning of the file. To return to the beginning, you must use the seek method. It accepts a parameter to indicate where the file is to be read or written (for example, 0 indicates the beginning of the file ). The seek method supports more complex operations, but it may be risky. For the moment, we still insist on a simple approach.

Now back to the beginning of the file, you can read the file content into the string variable and properly parse the string. Note that in a file, lines are distinguished by new lines (or line ends. If you try to call the split method on the string, it will split the string with blank characters (such as spaces. To split a row by a new line character, you must explicitly specify the new line character. Then, you can split the string and iterate the object in the for loop.

It seems that there is a lot of work to read and process a line of content from a file. Python needs to make simple things easier, so you may want to know if this task has a shortcut available. As shown in Listing 4, the answer is yes.
Listing 4. Reading and parsing rows

>>> myfile = open("testit.txt")>>> for line in myfile.readlines():...   print line... Hello World!The total value = $1820.00>>> myfile.close()>>> for line in open("testit.txt").readlines():...   print line... Hello World!The total value = $1820.00>>> for line in open("testit.txt"):...   print line... Hello World!The total value = $1820.00

Listing 4 demonstrates three techniques for reading and parsing text file lines. First, open the file and assign it to the variable. Call the readlines method to read the entire file into the memory and split the content into a string list. The for loop iterates on the string list and outputs one row at a time.

The second for loop simplifies the process by using the implicit variable of the file object (that is, the variable is not explicitly created. Once the file is opened and the file content is read, The generated results are the same as those in the first explicit example. The last example is further simplified and demonstrates the ability to iterate directly on file objects (note that this is a new feature of Python, so it may not work on your computer ). In this example, create an implicit file object, and then perform the rest of the work in Python to allow all the ongoing iterations in the file.

However, in some cases, when reading data from a file, you may want a better level of control. In this case, the readline method should be used, as shown in listing 5.
Listing 5. Reading data

>>> myfile = open("testit.txt")>>> myfile.readline()'Hello World!\n'>>> myfile.readline()'The total value = $1820.00\n'>>> myfile.readline()''>>> myfile.seek(0)>>> myfile.readline()'Hello World!\n'>>> myfile.tell()13L>>> myfile.readline()'The total value = $1820.00\n'>>> myfile.tell()40L>>> myfile.readline()''>>> myfile.tell()40L>>> myfile.seek(0)>>> myfile.read(17)'Hello World!\nThe '>>> myfile.seek(0)>>> myfile.readlines(23)['Hello World!\n', 'The total value = $1820.00\n']>>> myfile.close()

This example demonstrates how to move a file, read a row at a time, or explicitly move the file location indicator using the seek method. First, use the readline method to move in the file line. When the end of the file is reached, the readline method returns an empty string. After the end of the file, if this method is used to continue reading, it will not cause errors and only return NULL strings.

Then return the start point of the file and read another row. The tell method displays the current position in the file (after the first line of text)-In this example, the position is 13th characters. By using this knowledge, you can pass a parameter to the read or readline method to control the number of characters read. For the read method, this parameter (17 in this example) is the number of characters to be read from the file. However, after reading a specified number of characters, the readline method continues reading until the end of the row. In this example, it reads the first and second lines of text.
Write Data

The examples so far focus on reading data rather than writing data. As shown in Listing 6, once you understand the basic knowledge of using file objects, writing is also easy.
Listing 6. Writing data

>>> mydata = ['Hello World!', 'The total value = $1820.00']>>> myfile = open('testit.txt', 'w')>>> for line in mydata:...   myfile.write(line + '\n')... >>> myfile.close()>>> myfile = open("testit.txt")>>> myfile.read()'Hello World!\nThe total value = $1820.00\n'>>> myfile.close()>>> myfile = open("testit.txt", "r+")>>> for line in mydata:...   myfile.write(line + '\n')... >>> myfile.seek(0)>>> myfile.read()'Hello World!\nThe total value = $1820.00\n'>>> myfile.close()>>> myfile = open("testit.txt", "r+a")>>> myfile.read()'Hello World!\nThe total value = $1820.00\n'>>> for line in mydata:...   myfile.write(line + '\n')... >>> myfile.seek(0)>>> myfile.read()'Hello World!\nThe total value = $1820.00\nHello World!\nThe total value = $1820.00\n'>>> myfile.close()

To write data to a file, you must first create a file object. However, in this case, the 'W' mode must be used to mark the file to be written. In this example, write the content of mydata list to the file, close the file, and re-open the file to read the content.

However, you usually want to read and write files at the same time, so the next part of this example re-opens the file in 'r + 'mode. Because the file can be written, rather than added, the file will be truncated. First, write the content of mydata list to the file, then locate the file pointer to the beginning of the file, and read the content. In this example, close the file and re-open the file in read and add mode "r +. As shown in the sample code, the file content is now the result of two write operations (the text is duplicated ).
Processing binary data

All the preceding examples process text or character data: write and read character strings. However, in some cases, for example, when processing integers or compressed files, you must be able to read and write binary data. When creating a file object, you can easily use Python to process binary data by adding 'B' to the file mode, as shown in listing 7.
Listing 7. Processing binary data

>>> myfile = open("testit.txt", "wb")>>> for c in range(50, 70):...   myfile.write(chr(c))... >>> myfile.close()>>> myfile = open("testit.txt")>>> myfile.read()'23456789:;<=>?@ABCDE'>>> myfile.close()

In this example, create a suitable file object and write binary characters with ASCII values ranging from 50 to 69. Use the chr method to convert the integer created by calling the range method into a character. After writing all the data, close the file and re-open the file for reading, or use the binary mode flag. Reading a file proves that the integer is not written into the file. On the contrary, the character value is written.

Be careful when reading and writing binary data, because different platforms store binary data in different ways. If binary data must be processed, it is best to use an appropriate object (or an object from a third-party Developer) from the Python library ).

Read and Write: the most interesting part

This article discusses how to read data from and write data to a file in a Python program. In general, the process is simple: create a suitable file object, and then read and write as needed. However, when using the write mode to create a file, you must note the file truncation when writing data to the file. To add data to a file, use the Add mode when creating a file object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.