Ceiling-level python reading file method, really fragrant......

Hello everyone! I am a red panda ❤

Let's find out todayfileinput

When it comes to fileinput, maybe 90% of the coders say they have never used it or even heard of it.

This is not surprising, because in the python world, since open can go all over the world, why do you need fileinput?

However, today I still want to introduce the fileinput method, because it is too nice.

More than just incense. It's really fragrant!

Next, just follow me, fileinput together~~

Yes, this is the feel.


If you have any python-related error answers that you can't answer, or source code information/module installation/ women's clothing bosses are proficient in skills, you can come here: ( https://jq.qq.com/?_wv=1027&k=2Q3YTfym ) or +V: python10010 ask me

Please add image description

1. Method introduction

Basic usage

Let's take a look at the basic functions of fileinput:

  • fileinput.filename(): Returns the filename currently being read. —> Return None until the first line is read.
  • fileinput.fileno(): Returns the current file "file descriptor" as an integer. —> When the file is not open (between the first line and the file), return -1.
  • fileinput.lineno(): Returns the cumulative line number that has been read. —> Before the first line is read, return
    0. After the last line of the last file has been read, returns the line number of that line.
  • fileinput.filelineno(): Returns the line number in the current file. —> Before the first line is read, return 0.
    --> After the last line of the last file has been read, returns the line number of that line in this file.

Advanced usage

  • fileinput.isfirstline(): Returns True if the line just read is the first line of the file it is in, otherwise returns False.
  • fileinput.isstdin(): Returns True if the last line read was from sys.stdin, False otherwise.
  • fileinput.nextfile(): Closes the current file so that the next iteration will read the first line from the next file (if it exists); lines not read from this file will not be counted toward the cumulative line count. The filename does not change until the first line of the next file has been read.
    --> This function will not take effect until the first line has been read; it cannot be used to skip the first file. --> After the last line of the last file has been read, this function will no longer take effect.
  • fileinput.close(): Close the sequence.

Please add image description

2. Default read

code example

import fileinput

'当 Python 脚本没有传入任何参数时,fileinput 默认会以 stdin 作为输入源'
for line in fileinput.input():
    print(f'{
      
      line}')

operation result

Please add image description
What you enter, the program will read and output.

Common name: repeater

Please add image description

3. Process a file

code example

import fileinput

'files 输入打开文件的名称即可'
with fileinput.input(files=('output.txt',)) as file:
    for line in file:
        print(f'{
      
      fileinput.filename()}{
      
      fileinput.lineno()}行:{
      
      line}',end='')

operation result

Please add image description
Parse:

  • fileinput has and only these two read modes: 'r', 'rb';
  • fileinput.input() uses mode='r' by default to read files. If your file is binary, you can use mode='rb'.

Please add image description

4. Processing batch files

Serial numbering of multiple files

call method

  • fileinput.lineno() method

code example

import fileinput

'files 输入打开文件的名称即可'
with fileinput.input(files=('output.txt','input.txt')) as file:
    for line in file:
        #fileinput.lineno() 把两个文件的整合陈一个文件对象file,需要排序输出
        print(f'{
      
      fileinput.filename()}{
      
      fileinput.lineno()}行: {
      
      line}', end='')
        
        # fileinput.filelineno()两个文件单独读取,需要单独排序
        print(f'{
      
      fileinput.filename()}{
      
      fileinput.filelineno()}行: {
      
      line}', end='')

operation result

Please add image description
Multiple file serial numbers are sorted separately

call method

  • fileinput.filelineno() method

code example

import fileinput

'files 输入打开文件的名称即可'
with fileinput.input(files=('test1.txt','test2.txt')) as file:
    for line in file:       
        # fileinput.filelineno()两个文件单独读取,需要单独排序
        print(f'{
      
      fileinput.filename()}{
      
      fileinput.filelineno()}行: {
      
      line}', end='')

Use of running results
Please add image description
with glob

In the era of beauty, the above output style can no longer meet our needs, so we thought of glob.

code example

import fileinput
import glob

#glob 匹配te开头的txt文件
for line in fileinput.input(glob.glob("te*.txt")):
    if fileinput.isfirstline():
        #输出读取文件
        print('='*10,f'读取文件{
      
      fileinput.filename()}','='*10)
        #fileinput.filelineno()方法读取
    print(str(fileinput.filelineno())+ ':'+line.upper(),end='')

operation result

Please add image description
With this beauty, which young lady can not like it.

Please add image description

Five, read and backup

call method

The backup parameter of fileinput.input can specify the suffix of the backup, such as .bak

code example

import fileinput

#触发backup的动作,源文件内容被修改,对源文件进行backup
with fileinput.input(files=("test1.txt",), backup=".bak",inplace=1) as file:
    for line in file:
        print(line.rstrip().replace('111111', '222222'))
        print(f'{
      
      fileinput.filename()}{
      
      fileinput.lineno()}行: {
      
      line}', end='')

operation result
Please add image description

Please add image description

6. Redirect replacement

Parse

In the above example, the inplace parameter is used to indicate whether to write the standard output result back to the file, which is not replaced by default.
Code example:

import fileinput

#触发backup的动作,源文件内容被修改,对源文件进行backup
with fileinput.input(files=("test2.txt",), inplace=True) as file:
    print("[INFO] task is started...")
    for line in file:
        print(f'{
      
      fileinput.filename()}{
      
      fileinput.lineno()}行: {
      
      line}', end='')
    print("[INFO] task is closed...")

operation result

Please add image description

Note

By running the results, you can see:

The print content in the body of the for loop will be written back to the original file. The print outside the for loop is unchanged.

Please add image description

7. Advanced

Analysis of the meaning of openhook

  • There is an openhook parameter in fileinput.input(), which supports the user to pass in a custom object reading method;
  • If no hook is passed in, fileinput uses the open function by default;

Method introduction

fileinput has two built-in hooks

1、fileinput.hook_compressed(filename, mode)

  • use the gzip and bz2 modules to transparently open gzip and bzip2 compressed files (identified by the extensions '.gz' and '.bz2');
  • If the file extension is not '.gz' or '.bz2', the file is opened in the normal way (ie with open() and without any decompression); example usage: fi=fileinput.FileInput(openhook=fileinput.hook_compressed)

2、fileinput.hook_encoded(encoding, errors=None)

  • Returns a hook that opens each file via open(), reading the file with the given encoding and errors.

  • 使用示例: fi =
    fileinput.FileInput(openhook=fileinput.hook_encoded(“utf-8”,
    “surrogateescape”))

Example combat

If I want to use fileinput to read files on the network, the idea is:

  • First use requests to download the file to the local
  • Then use open to read it;
def online_open(url, mode):
    import requests
    r = requests.get(url) 
    filename = url.split("/")[-1]
    with open(filename,'w') as f1:
        f1.write(r.content.decode("utf-8"))
    f2 = open(filename,'r')
    return f2

Just pass this function to openhook directly:

# -*- coding:utf-8 -*-
# @Time   : 2022-07-23
# @Author : carl_DJ

import fileinput
file_url = 'https://www.csdn.net/robots.txt'
with fileinput.input(files=(file_url,), openhook=online_open) as file:
    for line in file:
        print(line, end="")

Please add image description

Code integration:

# -*- coding:utf-8 -*-
# @Time   : 2022-07-23
# @Author : carl_DJ
def online_open(url, mode):
    import requests
    r = requests.get(url)
    filename = url.split("/")[-1]
    with open(filename,'w') as f1:
        f1.write(r.content.decode("utf-8"))
    f2 = open(filename,'r')
    return f2

import fileinput
file_url = 'https://www.csdn.net/robots.txt'
with fileinput.input(files=(file_url,), openhook=online_open) as file:
    for line in file:
        print(line, end="")

operation result

Please add image description
The introduction to fileinput is also introduced here.

fileinput itself is a re-encapsulation of the open function, so in the cc part of reading, it is more professional and more professional than open.grace, which is also read-only.
In terms of writing, compared to open, it is not so powerful.

At the end of the day, fileinput is still a good method. worthy of your possession.


Today's article is shared here~

If there is something you don't understand, feel free to ask~

I'm Red Panda, see you in the next article (✿◡‿◡)

Please add image description

Supongo que te gusta

Origin blog.csdn.net/m0_67575344/article/details/126665583
Recomendado
Clasificación