Examples of Python and shell reading a row of a file

Source: Internet
Author: User


Python and the Shell (awk command) enable you to read directly a row of a file and read it by line number. And you can get the exact field of the line, which is similar to the x-axis, y-axis to locate a point.

One, awk fetch a column value of a row

Awk can set the condition to output the specified K field for each row in M to n rows in the file, using the following format:

awk ' nr==m,nr==n {print $k} ' Path/filename
M,n,k represents a real value. If you want to use a variable to represent a M,n value, the variable needs to be quoted in single quotes. Nr,{print} is the specified field of the awk command in this usage; Path/filename represents the path and file name of the read file. Two lines are specified here, and if you specify only one line, you can write this:

awk ' nr==m {print $k} ' Path/filename
Two, Python take a row of a column

The Linecache module provided by the standard library provides a specific way to take a particular line:

Import Linecache
Theline = Linecache.getline (filepath, Line_number)
After taking the relevant line, and then to Theline do split cut into list, and then the list index value on the line. such as Theline.split () [2].

Iii. the use of Linecache modules

That is, referring to the Linecache module, here is a list of other methods of Linecache. The Linecache module allows any row to be obtained from any file and is optimized using caching, often by reading multiple rows from a single file.

Linecache.getlines (filename) gets all of the content from the file named filename, the output as a list format, an element in the list of each behavior of the file, and the location of the linenum-1 element in the list is stored

Linecache.getline (Filename,lineno) Gets the Lineno line from a file named filename. This function never throws an exception-it will return when the error occurs (the line feed will be included in the found row). If the file is not found, this function will be searched in Sys.path.
Linecache.clearcache () clears the cache. If you no longer need a line previously obtained from Getline ()

Linecache.checkcache (filename) checks the validity of the cache. Use this function if the file in the cache has changed on the hard disk and you need to update the version. If filename is omitted, all entries in the cache are checked.

Linecache.updatecache (filename) updates the cache with file name filename. If the filename file is updated, use this function to update the list returned by linecache.getlines (filename).

Example:

# Cat A.txt
1a
2b
3c
4d
5e
6f
7g
1, get the contents of the A.txt file

>>> a=linecache.getlines (' a.txt ')
>>> A
[' 1a\n ', ' 2b\n ', ' 3c\n ', ' 4d\n ', ' 5e\n ', ' 6f\n ', ' 7g\n ']
2, get the contents of the 1–4 line in A.txt file

>>> a=linecache.getlines (' a.txt ') [0:4]
>>> A
[' 1a\n ', ' 2b\n ', ' 3c\n ', ' 4d\n ']
3, get the contents of the 4th line in the A.txt file

>>> a=linecache.getline (' A.txt ', 4)
>>> A
' 4d\n '

Attention:

After you open the contents of a file using Linecache.getlines (' a.txt '), if the A.txt file changes, such as what you obtained again with Linecache.getlines, not the latest content of the file, or the previous content, there are two ways to do this:

1, use Linecache.checkcache (filename) to update the file on the hard disk cache, and then in the implementation of Linecache.getlines (' a.txt ') can get to the latest a.txt content;

2, direct use of Linecache.updatecache (' a.txt '), you can get the latest A.txt the latest content.

After reading the file you do not need to use the file cache, you need to clean the cache at the end, so that Linecache.clearcache () clean cache, release the cache.

Related Article

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.