PYTHON学习笔记

1、python并发编程之多进程

import time
import random
from multiprocessing import Process
def run(name):
    print('%s runing' %name)
    time.sleep(random.randrange(1,5))
    print('%s running end' %name)
p1=Process(target=run,args=('anne',)) #必须加,号 
p2=Process(target=run,args=('alice',))
p3=Process(target=run,args=('biantai',))
p4=Process(target=run,args=('haha',))
p1.start()
p2.start()
p3.start()
p4.start()
print('主线程')

2、DataFrame index\columns 参数必须是列表形式出现

3、For循环次数的获取

enumerate还可以接收第二个参数,用于指定索引起始值,如:
list1 = ["这", "是", "一个", "测试"]
for index, item in enumerate(list1, 1):
    print index, item
>>>
1 这
2 是
3 一个
4 测试

4、检查文件是否存在

import os
os.path.exists(test_file.txt)
#True

os.path.exists(no_exist_file.txt)
#False

5、CSV的最后一行和第一行抓取方法

希望仅从非常大的csv的第一行和最后一行创建一个pandas DataFrame。本练习的目的是能够轻松地从这些csv文件中的第一个和最后一个条目中获取一些属性。我使用以下方法抓住csv的第一行没有问题:
pd.read_csv(filename, nrows=1)
我也可以通过各种方式抓取文本文件的最后一行,例如:
with open(filename) as f:
    last_line = f.readlines()[-1]

6、Python图形化编程界面PY QT 的学习

显示动态数据,展示界面!不学习了,现在改用dash,在网页端展示更加合!

7、Error when importing Dash: "ImportError: DLL load failed while importing _brotli: The specified module could not be found."

Traceback (most recent call last):
  File "app_example.py", line 7, in <module>
    import dash
  File "C:\Users\lizsc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\dash\__init__.py", line 1, in <module>
    from .dash import Dash, no_update  # noqa: F401
  File "C:\Users\lizsc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\dash\dash.py", line 18, in <module>
    from flask_compress import Compress
  File "C:\Users\lizsc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\flask_compress.py", line 10, in <module>
    import brotli
  File "C:\Users\lizsc\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\brotli.py", line 8, in <module>
    import _brotli
ImportError: DLL load failed while importing _brotli: The specified module could not be found.


In my case helped installing both vc_redist:

x86: vc_redist.x86.exe
x64: vc_redist.x64.exe

from here https://support.microsoft.com/en-gb/help/2977003/the-latest-supported-visual-c-downloads

Additionally make sure that you updated Python/Windows and other software to newest versions and restarted machine.

Tested on Windows 10 Pro, Python 3.8.5, PyCharm 2020.2 .
Share
Follow 

这个坑太大了,好久才找到方法。

8.Python datetime去除毫秒 返回当前时间

import datetime

if __name__ == '__main__':
    a = datetime.datetime.now().replace(microsecond=0)
    print(a, type(a))

运算结果:
2020-05-22 12:13:42 <class 'datetime.datetime'>
Process finished with exit code 0

9.lambda写法

queryDays = 2
rule = lambda x : '60S' if x <= 10 else ( '2T' if x <=30 else ( '5T' if x <=60 else ( '20T' if x <= 120 else '30T')))
print(rule(queryDays))

10.时间戳转换写法

timesp = 160000000
timeobj = time.localtime(timesp)
t1 = datetime.datetime.fromtimestamp(timesp)
t = datetime.datetime.strftime(t1,'%Y-%m-%d %H:%M:%S')
print(t)

11.VSCode 删除空行用正则表达式

^\s*(?=\r?$)\n

12.VSCode 远程联接配置文件

Host OfficeDBServer01
  HostName office.comcj.club
  Port 2256
  User root
  IdentityFile "C:\Users\Administrator\.ssh\id_rsa"

Host AliYunThreeYear
  HostName 101.200.91.223
  User root
  IdentityFile "C:\Users\Administrator\.ssh\id_rsa"

Host OfficeWebServer01
  HostName office.comcj.club
  Port 2253
  User root
  IdentityFile "C:\Users\Administrator\.ssh\id_rsa"

13.RuntimeError: The session is unavailable because no secret key was set.

在flask中遇到这种错,很容易解决,就是没有设置secret_key,django在配置文件中自动生成secret_key

# 两种方式设置secret_key
# app.secret_key = 'TPmi4aLWRbyVq8zu9v82dWYW1'
app.config["SECRET_KEY"] = 'TPmi4aLWRbyVq8zu9v82dWYW1'

本文章使用limfx的vscode插件快速发布