Introduction to zero base little white Python

Python a meow 2020-11-09 16:55:38
introduction zero base little white


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]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database