当前位置:网站首页>Introduction to zero base little white Python

Introduction to zero base little white Python

2020-11-09 16:55:00 Python a meow

Any language is inseparable from the operation of files , that Python How languages operate and manage files .

Encoding mode

The history of coding is roughly ASCII ->gb2312->unicode->utf-8, During the specific details can baidu

Let's take a little example of encoding and decoding , Remember that Chinese can be done GBK and utf-8 code , stay GBk One Chinese character corresponds to two bytes , stay utf-8 A Chinese character corresponds to three bytes , Chinese can't be done ASCII code .

>>> ' Meow '.encode('GBK')
b'\xc1\xf5\xc8\xf3\xc9\xad'
>>> ' Meow '.encode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
>>> ' Meow '.encode('ascii')
b' Meow '
>>> " Meow ".encode('utf-8')
b'\xe5\x88\x98\xe6\xb6\xa6\xe6\xa3\xae'
>>> ' Meow '.encode('GBK').decode('GBK')
' Meow '
>>> ' Meow '.encode('GBK').decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 0: invalid start byte
 Copy code 

If you have difficulties in learning , Looking for one python Learning communication environment , Can join us python circle , Skirt number 947618024, Can claim python Learning materials , It will save a lot of time , Reduce a lot of problems .

If the encoding and decoding format is inconsistent, there may be garbled code ,encode It means code ,decode It means decoding .

File operated API

Here is Python File operation specific API.

Method meaning open open read Read write write in close close readline Single line read readlines Multi line read seek File pointer operation tell Read the current pointer position

Open file

Python Of open() Function to open a file , There are several parameters available . However , The most common parameters are the first two .

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Be careful , The first is mandatory , The rest is optional . If you do not add mode Parameters , The file will be Python Open in read-only mode .

encoding: Don't write . Don't write parameters , The default codebook is the default codebook of the operating system .windows Default gbk,linux Default utf-8,mac Default utf-8.

f=open('test.txt',encoding='utf-8')   # Open file 
data=f.read()  # Read the file 
print(data)
f.close() # Close file          
 Copy code 

mode

mode meaning r Text mode , Read rb Binary mode , Read w Text mode , write in wb Binary mode , write in a Text mode , Additional ab Binary mode , Additional + Can read but write

Read the file

Code used in the file file operation of 1.txt The contents of the document are as follows :

 Focus on programming a meow 
 author : Meow 
 Copy code 

readline(), When this method is used , You need to specify the mode to open the file as r perhaps r+;

readlines(), Read all lines . Return a list , Each element in the list is each line of the original file . If the file is large , Occupy memory , It's easy to collapse .

#  Open file for reading 
f = open("1.txt","r",encoding='utf-8')
#  Read the contents of the file according to its size 
print(' The output comes from  read()  Method \n',f.read(2048))
#  Close file 
f.close()
#  Open file for reading and writing 
f = open("1.txt","r+",encoding='utf-8')
#  Read the first 2 A word and a paragraph 2 Line file content 
print(' The output comes from  readline()  Method \n',f.readline(2))
print(' The output comes from  readlines()  Method \n',f.readlines(2))
#  Close file 
f.close()
#  Open the file for reading and attaching 
f = open("1.txt","r",encoding='utf-8')
#  Open the file for reading and attaching 
print(' The output comes from  readlines()  Method \n',f.readlines())
#  Close file 
f.close()

#  Output is as follows 
 The output comes from  read()  Method 
  Focus on programming a meow 
 author : Meow 
 The output comes from  readline()  Method 
  Focus on 
 The output comes from  readlines()  Method 
 [' Programming a meow \n']
 The output comes from  readlines()  Method 
 [' Focus on programming a meow \n', ' author : Meow ']

 Copy code 

write file

The following is only about clear write w And add in a

Case study : Write attention programming a meow into test.txt In file

 

# mode=w  Create without files , Clear the contents when you have them , Careful use 
with open('test.txt', 'w', encoding='utf-8') as fb:
      fb.write(' Focus on programming a meow \n')  
 Copy code 

Next, the author : Programming a meow to write test.txt In file

with open('test.txt', 'w', encoding='utf-8') as fb:
      fb.write(' author : Meow \n')  
 Copy code 

After running, you will find that w Mode will clear the contents of the original file , So use it with care . Just use w, It's going to be done all at once .

Additional writing a

Case study : Add the poem of silent night thinking to test.txt In file

# mode=a  Append to the end of the file 
with open('test.txt', 'a', encoding='utf-8') as fb:
      fb.write(' Focus on programming a meow \n')  
with open('test.txt', 'a'encoding='utf-8') as fb:
      fb.write(' author : Meow \n')      
 Copy code 

Pointer operation

Things or resources exist in the form of documents , Like news 、 Shared memory 、 Connections etc. , Handles can be understood as pointers to these files .

Handle (handle) It's a term from compiler principles , Refers to the part of a sentence that is first regulated , So with a 「 sentence 」 word .

The function of a handle is to locate , Two APi still tell and seek.

tell Returns the current location of the file object in the file ,seek Move the file object to the specified location , The parameter passed in is offset , Represents the offset of the movement .

The following is an example to further understand the above functions , As shown below :

with open('test.txt', 'rb+') as f:
    f.write(b' Meow ')
    #  File object location 
    print(f.tell())
    #  Move to the fourth byte of the file 
    f.seek(3)
    #  Read a byte , The file object moves back one bit 
    print(f.read(1))
    print(f.tell())
    # whence  Is an optional parameter , The value is  0  From the beginning of the file ( The default value is )、 The value is  1  Indicates that the current file location is used 、 The value is  2  Use the end of the file as a reference point 
    #  Move to the penultimate byte 
    f.seek(-2, 2)
    print(f.tell())
    print(f.read(1))
    
# Output is as follows 
6
b's'
4
50
b'\r' 
 Copy code 

Context management

We're going to do this : Open file , Reading and writing , Close file . Programmers often forget to close files . The context manager can be used when files are not needed , Close files automatically , Use with open that will do .

# with context manager
with open("new.txt", "w") as f:
    print(f.closed)
    f.write("Hello World!")
print(f.closed)

# Output is as follows 
False
True
 Copy code 

How to read multiple files in batch

below , Batch read a folder under txt file

 

file_list = ['1.txt', '2.txt', '3.txt','4.txt']
for path in file_list:
    with open(path, encoding='utf-8') as f:
        for line in f:
            print(line)
 Copy code 

Next, we will read the folder in batches txt The content of the document , Merge content into a new file 5.txt in , The specific implementation code is as follows .

import os
# Gets the path to the destination folder 
filedir = os.getcwd()+'\\'+'\\txt'
# Get the list of file names in the current folder 
filenames = []
for i in os.listdir(filedir):
    if i.split(".")[-1] == 'txt':
        filenames.append(i)
# Open the 5.txt file , If not, create 
f = open('5.txt','w')
# First, traverse the filename 
for filename in filenames:
    filepath = filedir+'\\'+filename
    # Traverse a single file , Number of rows read 
    for line in open(filepath,encoding='utf-8'):
        f.writelines(line)
        f.write('\n')
# Close file 
f.close()
 Copy code 

Actually in Window We only need cd To the destination folder , That is, you need to merge all the txt Add the file to the destination folder , The implementation is as follows DOS command type *.txt > C:\ The target path \ The merged file name .txt

practice

subject : create a file data.txt, Documents in total 100000 That's ok , One for each line 1~100 Integer between , Title source : Cattle guest

import random

f = open(‘data.txt’,‘w+’)
for i in range(100000):
  f.write(str(random.randint(1,100)) + ‘\n’)
  f.seek(0)
  print(f.read())
  f.close()
 Copy code 

subject : Generate 100 individual MAC Address and write it to the file ,MAC Before the address 6 position (16 Base number ) by 01-AF-3B, Title source : Cattle guest

import random
import string

def create_mac():
  MAC='01-AF-3B'
  hex_num =string.hexdigits #0123456789abcdefABCDEF
  for i in range(3):
    n = random.sample(hex_num,2)
    sn = '-' + ''.join(n).upper()
    MAC += sn
  return MAC

def main():
  with open('mac.txt','w') as f:
    for i in range(100):
      mac = create_mac()
      print(mac)
      f.write(mac+'\n')

main()
 Copy code 

 

One more sentence at the end , Want to learn Python Please contact Xiaobian , Here's my own set python Learning materials and routes , Anyone who wants this information can enter q skirt 947618024 receive .

The material of this article comes from the Internet , If there is infringement, please contact to delete .

版权声明
本文为[Python a meow]所创,转载请带上原文链接,感谢