Python: 为指定Python版本安装pip

python Logo
image-3442

如果还没有安装pip,先尝试从标准库中对其进行引导:

1
$ python3.6 -m ensurepip --default-pip

如果仍然不允许,可以继续:

下载: get-pip.py

运行

1
 sudo python3.6 get-pip.py

现在,就可以使用pip3为python3.6安装软件包。例如:

1
$ sudo pip3 install requests  # install requests for python3.6

或者,只要已安装相应的pip,就可以将pip用于特定的Python版本,如下所示:

1
$ python3.6 -m pip install requests

Python: 更新ElasticSearch中的数据

python Logo
image-3320

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from elasticsearch import Elasticsearch

'''

pip install elasticsearch==7.9.1


'''





elasticsearch_client = Elasticsearch("http://192.168.0.1:9200")

es_index = "es_index"


def main():
    update_es_index_data()


# 批量更新数据.
def update_es_index_data():
    all_es_id = ["iWEfdsa6z359R9xI2", "mGEqiY89656zaRrxJC", "mWEqiYEBCO2z6zRrxJC"]
    for es_id in all_es_id:
        update_body = {
            "doc": {
                "mileage": 1000.95,
                "speed": 0.0
            }
        }
        elasticsearch_client.update(index=es_index, id=es_id, body=update_body)
    print("更新位置数据,更新成功!")

if __name__ == '__main__':
    main()

Python: 根据HTML标签内容,生成测试json数据.

python Logo
image-3288

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'''
根据HTML标签内容,生成测试json数据.

'''


import re
import json

html='''<el-table-column prop="NAME" label="姓名" width="120"></el-table-column>
        <el-table-column prop="AGE" label="年龄"></el-table-column>'''


if __name__ == '__main__':
    result=re.sub(r'(.*prop=")|(" label.*)','',html)
    lists=[]
    for index in range(1,4):
        dicts={}
        for item in result.split("\n"):
            if item:
                dicts[item]=f'{item}{index}'
        lists.append(dicts)
    print(json.dumps(lists,indent=4))

Python: 递归遍历当前目录下面的所有文件

python Logo
image-3282

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os

'''
递归遍历当前目录下面的所有文件.
可以设置过滤的文件.
'''



path="."

# 过滤的文件
entryNameFilter=["app - 副本.json","app.json","App.vue","main.js"]


def scanDir(path=""):
    with os.scandir(path) as it:
        for entry in it:
            if not entry.name.startswith('.') and entry.name not in entryNameFilter and entry.is_file():
                print(entry.path)
                #with open(entry.path,"r",encoding="utf-8") as rrs:
                #   print(rrs.read())
            elif entry.is_dir():
                scanDir(entry.path)



if __name__ == '__main__':
    scanDir(".")