Python随笔
文章平均质量分 54
weixin_888988
自动化运维
展开
-
Python和Powershell的相互调用
使用Python内建的subprocess模块,能够实现外部程序的调用。如果你的工作环境是Windows系统,那么Python+Powershell的组合会为你的工作带来极大的便利。本篇介绍一个使用Python做数据处理,Powershell做系统调用的例子。在上面这个例子中,使用Powershell做数据收集,Python做数据处理,最后使用Powershell的内建方法Send-MailMessage来发送数据处理报告。实现的过程非常简便。转载 2022-10-26 21:59:14 · 1504 阅读 · 0 评论 -
Auto-created primary key used when not defining a primary key
When you define a model in Django without specifying a primary key, Django will automatically create a primary key for you. The primary key is set to be an integer. If you want to override the field type, you can do so on a per model basis.Starting in Djan转载 2022-06-28 16:12:52 · 752 阅读 · 1 评论 -
Django 两种访问图片的方式
访问静态资源中的图片(就是放在static文件夹中的图片)静态资源存放一些页面需要频繁加载且一般不是由用户所定义的,所以一般存放在哪里就不会变化,文件夹名为定义为 static,位置在 你的 app下访问方式:在settings.py文件中最底部配置STATIC_URL = '/static/'STATIC_ROOT = os.path.join(BASE_DIR, 'collected_static')在浏览器输入 https://host:port/static/img.png ..转载 2022-05-31 10:23:10 · 1501 阅读 · 0 评论 -
python中 _、__、__xx__() 区别
1、访问权限(private、public)与继承方式(只有public继承) 在面向对象编程语言中,类的属性与方法都会设置访问控制权限,从而满足我们的设计需求。一般而言,我们通常会将对象的属性设置为私有的(private)或受保护的(protected),简单的说就是不允许外界访问,而对象的方法通常都是公开的(public),因为公开的方法就是对象向外界提供的接口。在Python中,属性和方法的访问权限只有两种,也就是公开的和私有的,如果希望属性是私有的,在给属性命名时可以用两个下划线作为开头,下.转载 2022-05-16 00:05:38 · 244 阅读 · 0 评论 -
python 模块化__init__.py和__all__
模块化编程优势模块化编程将一个任务分解成多个模块。每个模块就像一个积木一样,便于后期的反复使用、反复搭建。便于将一个任务分解成多个模块,实现团队开发,完成大规模程序。实现代码复用。一个模块实现后,可以被反复调用。可维护性增强init.py1.init.py的在文件夹中,可以使文件夹变为一个python模块,python的每个模块对应的包中都有一个__init__.py文件的存在2.通常__init__.py文件为空,但是我们还可以为它增加其他的功能,我们在导入一个模块时候(也叫包),实际转载 2022-05-06 21:21:57 · 364 阅读 · 0 评论 -
What‘s the exact usage of __reduce__ in Pickler
When you try to pickle an object, there might be some properties that don't serialize well. One example of this is an open file handle. Pickle won't know how to handle the object and will throw an error.You can tell the pickle module how to handle these转载 2022-01-06 19:08:45 · 59 阅读 · 0 评论 -
Max retries exceeded with url: /v2/info (Caused by SSLError(SSLError(“bad handshake: SysCallError(10
This can be figured out....there is a conflict between urllib3 and pyopenssl libraries.Once we uninstalled the pyopenssl it started working..原创 2021-12-07 13:15:21 · 230 阅读 · 0 评论 -
TypeError: ‘module‘ object is not callable
pip 命令 出现错误:[root@krsddpromet2 ~]# pip listTraceback (most recent call last): File "/usr/local/bin/pip", line 10, in <module> sys.exit(main())TypeError: 'module' object is not callable[root@krsddpromet2 ~]# cat /usr/local/bin/pi...原创 2021-11-29 17:44:50 · 525 阅读 · 0 评论 -
Python requests.exceptions.SSL: EOF occurred in violation of protocol
[Solved] Python requests.exceptions.SSL: EOF occurred in violation of protocolOctober 19, 2021byTeam FlutterqHello Guys, How are you all? Hope You all Are Fine. Today I get the following errorPython requests.exceptions.SSL: EOF occurred in violation...转载 2021-11-23 10:21:22 · 1145 阅读 · 1 评论 -
python3使用SMTP发送HTML格式邮件
一、设置开启SMTP服务并获取授权码可以参考第一篇文章,这里不再赘述:【一】Python3使用SMTP发送简单文本邮件二、使用Python3 发送HTML格式的邮件0.使用的环境为:Python3.6.3 (64bit)PyCharm 2017.3 (64bit)1.实现代码:import smtplib from email.header import Header from email.mime.text import MIMEText # 发件人和收.转载 2021-07-22 16:46:43 · 632 阅读 · 0 评论 -
print对齐方式
对齐方式的取值:<:左对齐 >:右对齐 ^:居中 =:在正负号(如果有的话)和数字之间填充,该对齐选项仅对数字类型有效。它可以输出类似+0000120这样的字符串。>>> print("|",format("RUNOOB","*>30"),"|") #左对齐| ************************RUNOOB |>>> print("|",format("RUNOOB","*^30"),"|") #居中对齐..原创 2021-05-16 18:32:40 · 708 阅读 · 0 评论 -
selenium加载cookie报错问题:selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cooki
selenium加载cookie报错问题:selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain在使用selenium进行自动化登录的过程中已经获取到cookie后,依旧报错:selenium.common.exceptions.InvalidCookieDomainException: Message: invalid cookie domain获取cookie和添加cookie转载 2021-04-26 19:41:21 · 3324 阅读 · 0 评论 -
Python语句的缩进
Python 语句只需要在同一个语句块才要相同的缩进n=int(input('请输入一个整数:'))def fab(n): if n<1: print('输入有误!') #缩3空格 return -1 #缩3空格 if n==1 or n==2: return 1 #缩3空格 else: return fab(n-1)+fab(n-2) .原创 2021-04-24 18:43:04 · 184 阅读 · 0 评论 -
python创建空文件
The easiest way to simply create a file without truncating it in case it exists is −open('my_file.txt', 'a').close()In Python 3.4+, you can directly use the pathlib module to touch files. Forexample,from pathlib import PathPath('my_file.txt').tou.原创 2021-03-31 17:59:39 · 1150 阅读 · 0 评论 -
获取打印异常(Exception)信息
异常信息的获取对于程序的调试非常重要,可以有助于快速定位有错误程序语句的位置。下面介绍几种python中获取异常信息的方法,这里获取异常(Exception)信息采用try...except...程序结构。如下所示try: ...except Exception, e: ...1、str(e)返回字符串类型,只给出异常信息,不包括异常信息的类型,如1/0的异常信息'integer division or modulo by zero'2、repr(e)给..转载 2020-09-22 10:41:44 · 3272 阅读 · 0 评论 -
selenium之 下拉选择框Select
今天总结下selenium的下拉选择框。我们通常会遇到两种下拉框,一种使用的是html的标签select,另一种是使用input标签做的假下拉框。后者我们通常的处理方式与其他的元素类似,点击或使用JS等。而对于前者,selenium给了有力的支持,就是Select类。我们要进行试验的网站:http://sahitest.com/demo/selectTest.htm网页与源码截图如下:1.导入(import)你可以用以下方式导入:from selenium.webdriver.转载 2020-09-14 20:41:34 · 528 阅读 · 0 评论 -
python lxml中etree的简单应用3
本次主要介绍,无论使用的xpath表达式中是否包含text()方法,最后都可以获取目标标签下的文本。使用的依然是etree.HTML和etree.tostring方法。1.思路首先将字符串源码转换成_Element对象,然后使用_Element对象的xpath()方法解析xpath表达式。如果通过xpath表达式解析得到的是文本对象,那么先将文本对象(也是字符串)转换成_Element对象,最后通过etree.tostring方法获取_Element对象中的文本内容(可以参考这里)。2.代码实现转载 2020-09-12 13:43:06 · 559 阅读 · 0 评论 -
python lxml中etree的简单应用2
通过python lxml中etree的简单应用1的介绍可以知道,有了etree.HTML()和etree.tostrint(),我们就可以很方便的在字符串对象和_Element对象进行转换了,接着介绍一下如何用etree对html源码进行过滤。假设现有如下html代码:“<h1>12345</h1><script>js</script><h2>67890</h2>”在上面的html代码中,如果想要去除所有的<script转载 2020-09-12 13:38:38 · 317 阅读 · 0 评论 -
python lxml中etree的简单应用1
我一般都是通过xpath解析DOM树的时候会使用lxml的etree,可以很方便的从html源码中得到自己想要的内容。这里主要介绍一下我常用到的两个方法,分别是etree.HTML()和etree.tostrint()。1.etree.HTML()etree.HTML()可以用来解析字符串格式的HTML文档对象,将传进去的字符串转变成_Element对象。作为_Element对象,可以方便的使用getparent()、remove()、xpath()等方法。如果想通过xpath获取html源转载 2020-09-12 13:32:01 · 542 阅读 · 1 评论 -
Python 字符串前面加u,r,b,f的含义
1、字符串前加 u例:u"我是含有中文字符组成的字符串。"作用:后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出现乱码。2、字符串前加 r例:r"\n\n\n\n” # 表示一个普通生字符串 \n\n\n\n,而不表示换行了。作用:去掉反斜杠的转移机制。(特殊字符:即那些,反斜杠加上对应字母,表示对应的特殊含义的,比如最常见的”\n”表示换行,”\t”表示Tab等。 )应用:常用于正则表达式,对应.转载 2020-08-31 09:02:55 · 259 阅读 · 0 评论 -
获取指定月最后一天的日期
calendar库中的知识点对于这个模块,我主要讲述一个monthrange()函数,这个函数是有什么用呢? monthrange(year, month):返回指定年月,由第一天所在的星期和本月的总天数组成的元组。 #这里的year和month就是上面提到的2020和7 importcalendar x,y=calendar.monthrange(year,month) print(x,y) """ 其中:x...转载 2020-08-17 17:40:14 · 700 阅读 · 0 评论 -
错误: pandas ValueError: pattern contains no capture groups
According to thedocs, you need to specify acapture group(i.e., parentheses) forstr.extractto, well, extract.Series.str.extract(pat, flags=0, expand=True)For each subject string in the Series, extract groups from the first match of regular expressi...转载 2020-08-11 16:50:18 · 2849 阅读 · 0 评论 -
10 WAYS TO FILTER PANDAS DATAFRAME
In this article, we will cover various methods to filter pandas dataframe in Python. Data Filtering is one of the most frequent data manipulation operation. It is similar to WHERE clause in SQL or you must have used filter in MS Excel for selecting specifi转载 2020-08-11 16:48:11 · 257 阅读 · 0 评论 -
xvfb探究
有时候我们不关注程序是否有界面(比如自动化测试),只要程序在运行就可以了很感谢 xvfb 这个工具给我们提供了相关的功能比如在没有 X server 的机器上运行 gedit, 可以用下面的命令xvfb-run --auto-servernum --server-args="-screen 0 1280x760x24" geditxvfb 这个工具相当于一个wrapper, 给应用程序提供虚拟的 X server用 ps auxf 观察进程的结构,可以发现xvfb-run 首先...转载 2020-08-03 11:28:01 · 998 阅读 · 0 评论 -
python中“(.*)” 、 “(.*?)”的区别
学习更多相关知识,关注博主知乎账号,用户名Trustintruthhttps://www.zhihu.com/people/suo-yi-xin-90/activities简单说,使用“(.*)”得到的是只有一个元素的列表,里面是很长的字符串“(.*?)”则是得到包含几个元素的列表,每个元素直接对应原来文本中不同的位置匹配的项。例如:十个人肩并肩的并排走着,使用“(.*)”的取到了从第一个人到最后一个人的所有东西在一起,后者则是分别取下了十个人的东西前者成为贪婪模式,获取最长的满足条件...转载 2020-07-08 15:36:17 · 3089 阅读 · 0 评论 -
Django: You are trying to add a non-nullable field stock without a default
I have added a new field in my model but after that I have deleted db.sqlite3 (to ensure I don't get error below)agrawalo@:~/myapp> lsREADME.md config core manage.py requirements.txtBut ...原创 2020-04-26 15:48:27 · 276 阅读 · 0 评论 -
Python进阶(二十九)-Python时间&日期&时间戳处理
https://blog.csdn.net/sunhuaqiang1/article/details/69396305Python进阶(二十九)-Python时间&日期&时间戳处理将字符串的时间转换为时间戳方法:import time#将其转换为时间数组a = "2013-10-10 23:40:00"timeArray = time.strptime(a, "...转载 2020-04-01 20:36:58 · 282 阅读 · 0 评论 -
Execute a String of Code in Python
https://www.geeksforgeeks.org/execute-string-code-python/Given few lines of code inside a string variable and execute the code inside the string.Examples:Input:code = """ a = 6+5 pr...转载 2020-03-29 11:09:41 · 113 阅读 · 0 评论 -
Python | Get unique values from a list
https://www.geeksforgeeks.org/python-get-unique-values-list/Given a list, print all the unique numbers in any order.Examples:Input : 10 20 10 30 40 40Output : 10 20 30 40 I...转载 2020-03-29 10:58:38 · 337 阅读 · 0 评论 -
Q Making queries
>>> Entry.objects.filter(... headline__startswith='What'... ).exclude(... pub_date__gte=datetime.date.today()... ).filter(... pub_date__gte=datetime.date(2005, 1, 30)... )...转载 2020-03-29 10:52:11 · 112 阅读 · 0 评论 -
网页里面的各种path
Selector:#row_change_request_884bb9e5dba1c8901f3a74fcbf9619ca > td:nth-child(3) > a#:代表这是一个元素的idJS path:document.querySelector("#row_change_request_884bb9e5dba1c8901f3a74fcbf9619ca &...原创 2020-03-14 01:21:22 · 536 阅读 · 0 评论 -
How to Switch Between IFrames Using Selenium Python
In this Selenium Python tutorial, we’ll learn to switch between IFrames.An IFrame (Inline Frame) is an HTML element that allows rendering a document within another HTML document on a webpage.We pre...转载 2020-03-13 19:44:54 · 144 阅读 · 0 评论 -
使用Xvfb,让需要X service的程序在后台跑
If you are using Selenium web driver with Python,you can use PyVirtualDisplay, a Python wrapper for Xvfb and Xephyr.PyVirtualDisplay needs Xvfb as a dependency. On Ubuntu, first install Xvfb:sudo...转载 2020-03-13 19:28:54 · 824 阅读 · 0 评论 -
%Y 与 %y 的区别
%Y 与%y 的区别>>> StartTime = datetime.datetime.strptime('04-03-20 09:00:01', '%d-%m-%y %H:%M:%S')>>>>>>>>> StartTime = datetime.datetime.strptime('04-03-19 09:...原创 2020-03-13 19:26:13 · 2883 阅读 · 0 评论 -
virtualenvs
/virtualenvs:https://docs.python-guide.org/dev/virtualenvs/原创 2020-01-14 17:33:54 · 102 阅读 · 0 评论 -
Running makemigrations and migrate, the tables not created in the MySQL database.
File "/home/neptunesharvest/.virtualenvs/neptune-env/lib/python3.7/site-packages/MySQLdb/connections.py", line 226, in query _mysql.connection.query(self, query) django.db.utils.ProgrammingError: (114...原创 2020-01-14 17:32:25 · 95 阅读 · 0 评论 -
Combine two or more querysets in a Django view
I am trying to build the search for a Django site I am building, and in that search, I am searching in 3 different models. And to get pagination on the search result list, I would like to use a generi...转载 2020-01-13 09:44:44 · 96 阅读 · 0 评论 -
file的一些读写操作
The easiest way to create a text stream is withopen(), optionally specifying an encoding:f = open("myfile.txt", "r", encoding="utf-8")In-memory text streams are also available asStringIOobjec...转载 2019-12-31 12:32:37 · 280 阅读 · 0 评论 -
python几个技术网站
1.http://zetcode.com/python2.https://cito.github.io/blog/3.https://www.example-code.com/python/4.https://rszalski.github.io/magicmethods/5.https://pypi.org/project6.https://pillow.read...原创 2019-12-18 12:50:08 · 358 阅读 · 0 评论 -
Python实现图片裁剪的两种方式
在这篇文章里我们聊一下Python实现图片裁剪的两种方式,一种利用了Pillow,还有一种利用了OpenCV。两种方式都需要简单的几行代码,这可能也就是现在Python那么流行的原因吧。首先,我们有一张原始图片,如下图所示:然后,我们利用OpenCV对其进行裁剪,代码如下所示:import cv2 img = cv2.imread("./data/cut/thor.jpg")p...转载 2019-12-18 11:42:56 · 4113 阅读 · 0 评论