Skip to content

Commit 93360ff

Browse files
committed
新增md文件
新增md文件
1 parent bafd0a5 commit 93360ff

15 files changed

Lines changed: 459 additions & 0 deletions

pic/python_basic/tuple2.png

32.4 KB
Loading

python_basic/Excel文件处理.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## Excel文件处理
2+
#### 读取excel,处理不同类型的单元格
3+
```python
4+
import xlrd
5+
6+
filepath = r''
7+
book = xlrd.open_workbook(filepath)
8+
sheet = book.sheet_by_index(0)
9+
rows = sheet.nrows
10+
cols = sheet.ncols
11+
for i in range(0, rows):
12+
for j in range(0, cols):
13+
cell_type = sheet.cell(i, j).ctype
14+
# 0:empty 1:string 2:number 3:date 4:boolean 5:error
15+
cell_value = sheet.cell(i, j).value
16+
if cell_type == 0:
17+
cell_value = ''
18+
elif cell_type == 2:
19+
cell_value = str(int(cell_value))
20+
elif cell_type == 3:
21+
cell_date = xlrd.xldate_as_tuple(cell_value, book.datemode)
22+
print(cell_date)
23+
```
24+
25+
#### 读取Excel文件
26+
```python
27+
import xlrd
28+
# 读excel文件
29+
book = xlrd.open_workbook('test_copy.xlsx')
30+
book.sheets()
31+
sheet = book.sheet_by_index(0)
32+
# 查看行数和列数
33+
sheet.nrows
34+
sheet.ncols
35+
# 获取指定单元格内容
36+
# text:'RID'
37+
sheet.cell(0,0)
38+
# 获取指定行
39+
sheet.row(0) #[text:'RID', text:'income1', text:'income2']
40+
sheet.row(1) #[number:1.0, number:100.0, number:200.0]
41+
# 获取指定行单元格里的值
42+
sheet.row_values(1) #[1.0, 100.0, 200.0]
43+
# 第一个参数是行索引,第二个参数是从第几个单元格开始取值
44+
sheet.row_values(1,1) #[100.0, 200.0]
45+
# 添加单元格,参数依次为行索引,列索引,单元格格式,单元格内容,None
46+
sheet.put_cell(1,3,xlrd.XL_CELL_TEXT,'new cell',None)
47+
# [1.0, 100.0, 200.0, 'new cell']
48+
sheet.row_values(1)
49+
```
50+
51+
#### 生成Excel文件
52+
```python
53+
import xlwt
54+
# 创建excel工作表
55+
workbook = xlwt.Workbook(encoding='utf-8')
56+
worksheet = workbook.add_sheet('sheet1')
57+
# 设置表头 (第一个参数为行数,第二个参数为单元格位置,第三个参数为单元格值)
58+
worksheet.write(0, 0, label='MAC')
59+
worksheet.write(0, 1, label='IMSI')
60+
worksheet.write(0, 2, label='IMEI')
61+
row_num = 1
62+
63+
res = ['a', 'b', 'c']
64+
for i in res:
65+
# 设置值 (第一个参数为行数,第二个参数为单元格位置,第三个参数为单元格值)
66+
worksheet.write(row_num, 0, '1111')
67+
worksheet.write(row_num, 1, i)
68+
worksheet.write(row_num, 2, i)
69+
row_num += 1
70+
workbook.save('OK.xls')
71+
```
72+
73+
#### 生成Excel文件
74+
```python
75+
from openpyxl import Workbook
76+
wb = Workbook() # 创建文件对象
77+
ws = wb.active # 获取第一个sheet
78+
ws.append(['MAC','IMSI','IMEI'])
79+
ws.append([11111,'2','3'])
80+
wb.save("OK.xlsx")
81+
```

python_basic/XML文件处理.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
## 创建XML文件
2+
```python
3+
import xml.dom.minidom as Dom
4+
# 创建XML文档
5+
doc = Dom.Document()
6+
# 创建根节点
7+
root = doc.createElement('html')
8+
# 创建h1节点
9+
h1 = doc.createElement('h1')
10+
h1.setAttribute('name','aaaa')
11+
h1.setAttribute('value','hhhhh')
12+
13+
h1.appendChild(doc.createTextNode('hello world'))
14+
15+
doc.appendChild(root)
16+
# 把h1节点作为root的子节点进行添加
17+
root.appendChild(h1)
18+
19+
f = open(r"C:\Users\daacheng\Desktop\test2.xml", "w",encoding='utf-8')
20+
doc.writexml(f, addindent='\t', newl='\n', encoding="utf-8")
21+
f.close()
22+
```
23+
## XML文件解析
24+
25+
```python
26+
from xml.etree.ElementTree import parse
27+
from xml.etree.ElementTree import parse
28+
#解析简单的xml文档
29+
data = open('data.xml')
30+
et = parse(data)
31+
# xml.etree.ElementTree.ElementTree
32+
type(et)
33+
root = et.getroot()
34+
root.tag
35+
root.attrib
36+
# findall()只能寻找当前节点子节点内的标签
37+
for child in root.findall('RecordTime'):
38+
for child_two in child:
39+
print(child_two.tag)
40+
# iter()可以递归查找当前节点下所有子孙节点标签
41+
list(root.iter('Year'))
42+
```
43+
## XML字符串解析
44+
45+
```python
46+
import xml.etree.ElementTree as ET
47+
root = ET.fromstring(body)
48+
devicelist = root.find('DeviceList')
49+
items = devicelist.findall('Item')
50+
for item in items:
51+
camera_sip_id = item.findtext('DeviceID') # 设备sip_id
52+
camera_name = item.findtext('Name') # 设备名称
53+
camera_address = item.findtext('Address') # 设备IP
54+
camera_org = item.findtext('Manufacturer') # 设备厂商
55+
camera_model = item.findtext('Model') # 设备型号
56+
```

python_basic/json文件处理.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## json文件处理
2+
#### json.dumps(): 把Python对象转换成字符串
3+
#### json.loads(): 把字符串转换成Python对象
4+
5+
```python
6+
import json
7+
#dumps() 把python对象转换成json字符串
8+
p_dict={'c':'aaa','b':'ss','a':'dd'}
9+
10+
# 字典转换成json格式字符串
11+
# '{"a": "dd", "b": "ss", "c": "aaa"}'
12+
json.dumps(p_dict,sort_keys=True)
13+
14+
# json格式字符串转换成字典
15+
obj = json.loads('{"a": "dd", "b": "ss", "c": "aaa"}')
16+
# dict
17+
print(type(obj))
18+
19+
obj2 = json.loads('["aaa","bbb",222]')
20+
# list
21+
print(type(obj2))
22+
23+
# 把Python对象写入到.json文件中
24+
l=['a','b','{"c":1,"d":2}']
25+
with open('dump.json','w') as f:
26+
json.dump(l,f)
27+
# 读取.json文件
28+
with open('dump.json','r') as f:
29+
# ['a', 'b', '{"c":1,"d":2}']
30+
print(json.load(f))
31+
```

python_basic/main.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@
55
4. [文件路径处理](文件路径处理.md)
66
5. [创建临时文件](创建临时文件.md)
77
6. [文件目录扫描](文件目录扫描.md)
8+
7. [XML文件处理](XML文件处理.md)
9+
8. [utf8-BOM文件处理](utf8BOM文件处理.md)
10+
9. [解析命令行参数(getopt)](解析命令行参数getopt.md)
11+
10. [等分字符串](等分字符串.md)
12+
11. [读取配置文件(configparser)](读取配置文件configparser.md)
13+
12. [集合删除元素](集合删除元素.md)
14+
13. [网络字节序(大端)与主机字节序(小端)转换](网络字节序与主机字节序转换.md)
15+
14. [文件目录事件监控](文件目录事件监控.md)
16+
15. [车牌格式校验](车牌格式校验.md)
17+
16. [关于tuple](关于tuple.md)
18+
17. [json文件处理](json文件处理.md)
19+
18. [Excel文件处理](Excel文件处理.md)
20+
19. [深拷贝与浅拷贝](深拷贝与浅拷贝.md)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## utf8-BOM文件处理
2+
#### 如果文件是utf8-BOM开头,会对文件转换造成影响,需要处理。
3+
```python
4+
import codecs
5+
# b'\xef\xbb\xbf'
6+
codecs.BOM_UTF8
7+
with open(dev_filepath, 'r+b') as f:
8+
data = f.read()
9+
if data[:3] == codecs.BOM_UTF8:
10+
data = data[3:]
11+
# seek()方法用于移动文件读取指针到指定位置
12+
f.seek(0)
13+
# truncate() 方法用于截断文件,
14+
# 如果指定了可选参数size,则表示截断文件为size个字符。
15+
# 如果没有指定 size,则从当前位置起截断
16+
# 截断之后 size 后面的所有字符被删除
17+
f.truncate()
18+
f.write(data)
19+
```

python_basic/关于tuple.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#### 不要把可变对象放在元组中
2+
#### 增量赋值不是一个原子操作,虽然程序抛出异常,但还是会添加成功
3+
答案:D
4+
![](../pic/python_basic/tuple2.png)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## 文件目录事件监控
2+
#### 监控指定目录下的文件或文件夹的变动:
3+
4+
```python
5+
from watchdog.observers import Observer
6+
from watchdog.events import *
7+
import time
8+
9+
class FileEventHandler(FileSystemEventHandler):
10+
def __init__(self):
11+
FileSystemEventHandler.__init__(self)
12+
13+
def on_moved(self, event):
14+
if event.is_directory:
15+
print("directory moved from {0} to {1}".format(event.src_path,event.dest_path))
16+
else:
17+
print("file moved from {0} to {1}".format(event.src_path,event.dest_path))
18+
19+
def on_created(self, event):
20+
if event.is_directory:
21+
print("directory created:{0}".format(event.src_path))
22+
else:
23+
print("file created:{0}".format(event.src_path))
24+
25+
def on_deleted(self, event):
26+
if event.is_directory:
27+
print("directory deleted:{0}".format(event.src_path))
28+
else:
29+
print("file deleted:{0}".format(event.src_path))
30+
31+
def on_modified(self, event):
32+
if event.is_directory:
33+
print("directory modified:{0}".format(event.src_path))
34+
else:
35+
print("file modified:{0}".format(event.src_path))
36+
37+
if __name__ == "__main__":
38+
observer = Observer()
39+
event_handler = FileEventHandler()
40+
observer.schedule(event_handler, r"C:\Users\Liye\Desktop\ttt", True)
41+
observer.start()
42+
observer.join()
43+
print('end...')
44+
```
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## 深拷贝与浅拷贝
2+
#### 不可变变量
3+
```python
4+
# 对于Python中的不可变变量(数值,字符串,元组),赋值,浅拷贝与深拷贝都是一样的,都是引用地址。
5+
# a = 123 ,变量a表示的是123在内存中的地址
6+
# b = a,就是把b也指向123在内存中的地址
7+
# copy.copy(a),浅拷贝复制的也是123在内存中的地址
8+
# copy.deepcopy(a),深拷贝复制的也是123在内存中的地址
9+
# 所以 a,b,c,d都指向内存中同一个内存地址,他们的值都是123,他们的id(可以看做内存地址)相等。
10+
a = 123
11+
b = a
12+
c = copy.copy(a)
13+
d = copy.deepcopy(a)
14+
print('a: {}, id={}'.format(a, id(a))) # a: 123, id=1625849120
15+
print('b: {}, id={}'.format(b, id(b))) # b: 123, id=1625849120
16+
print('c: {}, id={}'.format(c, id(c))) # c: 123, id=1625849120
17+
print('d: {}, id={}'.format(d, id(d))) # d: 123, id=1625849120
18+
19+
# a = 100,变量a由原本指向内存中123所在的内存地址,变为指向内存中100所在的内存地址
20+
# 所以变量a表示的内存地址变化,b,c,d还是指向123所在的内存地址,并没有发生变化
21+
a = 100
22+
print('a: {}, id={}'.format(a, id(a))) # a: 100, id=1625848384
23+
print('b: {}, id={}'.format(b, id(b))) # b: 123, id=1625849120
24+
print('c: {}, id={}'.format(c, id(c))) # c: 123, id=1625849120
25+
print('d: {}, id={}'.format(d, id(d))) # d: 123, id=1625849120
26+
```
27+
#### 可变变量
28+
```python
29+
# 无论是不可变变量还是可变变量
30+
# = 赋值都是表示对象的引用传递
31+
a = {'k1': 123, 'k2': [4, 5, 6]}
32+
b = a
33+
# a: {'k1': 123, 'k2': [4, 5, 6]}, id=2036596592712
34+
# b: {'k1': 123, 'k2': [4, 5, 6]}, id=2036596592712
35+
print('a: {}, id={}'.format(a, id(a)))
36+
print('b: {}, id={}'.format(b, id(b)))
37+
38+
a['k1'] = 100
39+
# a: {'k1': 100, 'k2': [4, 5, 6]}, id=2036596592712
40+
# b: {'k1': 100, 'k2': [4, 5, 6]}, id=2036596592712
41+
print('a: {}, id={}'.format(a, id(a)))
42+
print('b: {}, id={}'.format(b, id(b)))
43+
print('b: {}
44+
```
45+
#### copy.copy 浅拷贝
46+
47+
```python
48+
# 浅拷贝,只拷贝对象里面的不可变变量,对于对象里面的可变变量,仍然是引用传递
49+
a = {'k1': 123, 'k2': [4, 5, 6]}
50+
c = copy.copy(a)
51+
# a: {'k1': 123, 'k2': [4, 5, 6]}, id=2067138400400
52+
# c: {'k1': 123, 'k2': [4, 5, 6]}, id=2067138400328
53+
print('a: {}, id={}'.format(a, id(a)))
54+
print('c: {}, id={}'.format(c, id(c)))
55+
56+
a['k1'] = 100
57+
# a: {'k1': 100, 'k2': [4, 5, 6]}, id=2067138400400
58+
# c: {'k1': 123, 'k2': [4, 5, 6]}, id=2067138400328
59+
print('a: {}, id={}'.format(a, id(a)))
60+
print('c: {}, id={}'.format(c, id(c)))
61+
62+
# 浅拷贝对于对象里面的可变变量,仍然是引用传递,所以k2的列表会一起跟着变化
63+
a['k2'][0] = 0
64+
# a: {'k1': 100, 'k2': [0, 5, 6]}, id=2067138400400
65+
# c: {'k1': 123, 'k2': [0, 5, 6]}, id=2067138400328
66+
print('a: {}, id={}'.format(a, id(a)))
67+
print('c: {}, id={}'.format(c, id(c)))
68+
```
69+
70+
#### copy.deepcopy() 深拷贝
71+
```python
72+
# 深拷贝,完全拷贝了对象及其子对象,没有任何引用传递
73+
# 深拷贝后的对象与原对象是完全独立的,其中一方的修改不会影响另一方
74+
a = {'k1': 123, 'k2': [4, 5, 6]}
75+
d = copy.deepcopy(a)
76+
# a: {'k1': 123, 'k2': [4, 5, 6]}, id=3069791555728
77+
# d: {'k1': 123, 'k2': [4, 5, 6]}, id=3069794267424
78+
print('a: {}, id={}'.format(a, id(a)))
79+
print('d: {}, id={}'.format(d, id(d)))
80+
81+
a['k2'][0] = 0
82+
# a: {'k1': 123, 'k2': [0, 5, 6]}, id=3069791555728
83+
# d: {'k1': 123, 'k2': [4, 5, 6]}, id=3069794267424
84+
print('a: {}, id={}'.format(a, id(a)))
85+
print('d: {}, id={}'.format(d, id(d)))
86+
```

python_basic/等分字符串.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## 等分字符串
2+
```python
3+
import re
4+
'-'.join(re.findall(r'.{2}', 'F0B42998CE34')) # 'F0-B4-29-98-CE-34'
5+
```

0 commit comments

Comments
 (0)