如果您对Pythonre.sub问题和pythonre.sub感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Pythonre.sub问题的各种细节,并对pythonre.sub进行深入的分析,
如果您对Python re.sub问题和python re. sub感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解Python re.sub问题的各种细节,并对python re. sub进行深入的分析,此外还有关于centos7安装python开发环境(python3_postgresql_sublime_supervisor)、plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory、Python 3.X上面使用mysqlDB问题、python cookielib问题的实用技巧。
本文目录一览:- Python re.sub问题(python re. sub)
- centos7安装python开发环境(python3_postgresql_sublime_supervisor)
- plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
- Python 3.X上面使用mysqlDB问题
- python cookielib问题
Python re.sub问题(python re. sub)
问候大家,
我不确定这是否可行,但我想在正则表达式替换中使用匹配的组来调用变量。
a = ''foo''b = ''bar''text = ''find a replacement for me [[:a:]] and [[:b:]]''desired_output = ''find a replacement for me foo and bar''re.sub(''\[\[:(.+):\]\]'',group(1),text) #is not validre.sub(''\[\[:(.+):\]\]'',''\1'',text) #replaces the value with ''a'' or ''b'', not var value
有什么想法吗?
答案1
小编典典您可以在使用re.sub时指定回调,该回调可以访问组:http : //docs.python.org/library/re.html#text-
munging
a = ''foo''b = ''bar''text = ''find a replacement for me [[:a:]] and [[:b:]]''desired_output = ''find a replacement for me foo and bar''def repl(m): contents = m.group(1) if contents == ''a'': return a if contents == ''b'': return bprint re.sub(''\[\[:(.+?):\]\]'', repl, text)
还注意到额外的吗?在正则表达式中。您要在此处进行非贪心匹配。
我了解这只是说明概念的示例代码,但是对于您给出的示例,简单的字符串格式更好。
centos7安装python开发环境(python3_postgresql_sublime_supervisor)
一. 安装gnome
1.最小化安装centos7。
2.安装X11。yum groupinstall "X Window System"。
3.安装gnome。
全安装:yum groupinstall -y "GNOME Desktop"。
最小安装:yum install gnome-classic-session gnome-terminal。 nautilus-open-terminal control-center liberation-mono-fonts。
4.开机启动图形界面:ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target。
5.重启:reboot。
6.安装字体:yum groupinstall Fonts。
7.更新字体缓存: fc-cache。
8.安装优化工具:yum install gnome-tweak-tool.noarch。
9.安装归档工具:yum install file-roller。
二.安装python3
1.官网下载python。
2.解压下载好的压缩包。
3.创建目录mkdir /usr/local/python_3.5.2。
4.进入解压目录sudo ./configure --prefix=/usr/local/python_3.5.2 。
5.make。
6.make install。
7.创建连接 ln -s /usr/local/python_3.5.2/bin/python3.5 /usr/local/bin/python3。
说明:yum和supversion等使用python2,所以不将/usr/bin/python连接到python3.使用python3时加#!/usr/bin/env python3。
8.python资源。
编程网站:The Python Challenge,Project Euler。
语法参考:《python学习手册》,官方网站。
三.安装PostgreSQL9.5
1.下载。
在postgresql的官方即可找到源码文件目录,地址如下:https://www.postgresql.org/ftp/source/,在下载列表中根据需求选择版本。
2. 配置编译安装。
进入pg压缩包目录通过tar -zxvf ./postgresql-9.5.5.tar.gz进行解压,进入解压目录。
通过./configure --help可以看到编译相关的帮助信息。
编译时指定一个安装目录./configure --prefix=/usr/local/postgresql。配置完成了。
接下来就可以编译安装了:make install clean。
(新系统安装过程中可能出现的问题)没有c编译器,提示缺少readline库,缺少zlib库。分别执行yum install gcc,yum install readline-devel,yum install zlib-devel。
3.用户权限与环境变量。
编译安装成功后,接下来要做的就是创建一个普通用户,因为默认超级用户(root)不能启动postgresql,所以需要创建一个普通用户来启动数据库,执行以下命令创建用户:useradd postgres。
接下来需要设置权限,将postgres的数据目录全部赋权给postgres用户(此处我将postgres的数据目录指定在在/usr/local/postgresql/data目录下):chown -R postgres:postgres /usr/local/postgresql/。
最后为了方便起见设置一下相关的环境变量,此处仅仅设置postgres用户的环境变量,所以首先通过su - postgres切换到postgres用户,打开.bash_profile文件并追加以下内容:PGHOME=/usr/local/postgresql,export PGHOME,PGDATA=/usr/local/postgresql/data,export PGDATA。 ln -s /usr/local/postgresql/bin/pg_ctl /usr/local/bin/pg_ctl。
4.初始化数据库。
由于配置了环境变量,所以此处我们直接执行initdb即可完成db初始化,但在这之前我们可以通过initdb --help看一下初始化相关的帮助信息。可以看到在使用initdb进行初始化的同时我们可以指定参数来同时进行一些初始化工作,例如指定pgdata(postgresql数据目录)、指定encoding(编码)、指定数据库超级用户的用户名和密码等等,在最后面我标记出的这段话指出了如果data目录没有指定,则会默认使用环境变量中的PGDATA,由于之前我们刚刚设置了PGDATA环境变量,所以此处我们也就无需再额外指定,最后执行初始化命令即可:initdb。同时在postgresql的目录可以看到生成的数据目录data以及该目录的相关数据和配置文件。
5.配置文件。
pg_hba.conf和postgresql.conf两个配置文件,一个是访问控制配置(127.0.0.1改为信任的客户端ip网段使其可以远程访问),一个是postgresql主配置文件(listen_address=localhost改为星号使其监听整个网络),方便起见我这里将pg_hba.conf的ip地址修改为0.0.0.0/0,而加密方式改为md5,就表示需要密码访问,算是提供一个最低级的安全防护。开放pg的5432端口。
6.将端口加入防火墙。
firewall-cmd --zone=public --add-port=5432/tcp --permanent 或者 firewall-cmd --add-service=postgresql --permanent 开放postgresql服务
firewall-cmd --reload 重载防火墙
7.启动和连接。
pg_ctl start -D /usr/local/postgresql/data -l /usr/local/postgresql/log/pg_server.log
8.设置postgres用户的密码(默认为空)。
切换用户,su - postgres。
登录数据库,psql -U postgres ,执行后提示符变为 ''postgres=#''。
设置postgres用户密码, ALTER USER postgres WITH PASSWORD ''abc123'' ,会提示输入两次密码。
退出数据库, \q 。
9.postgresql服务脚本。
vim /lib/systemd/system/postgresql.service 。
# It''s not recommended to modify this file in-place, because it will be
# overwritten during package upgrades. If you want to customize, the
# best way is to create a file "/etc/systemd/system/postgresql.service",
# containing
# .include /lib/systemd/system/postgresql.service
# ...make your changes here...
# For more info about custom unit files, see
# http://fedoraproject.org/wiki/Systemd#How_do_I_customize_a_unit_file.2F_add_a_custom_unit_file.3F
# For example, if you want to change the server''s port number to 5433,
# create a file named "/etc/systemd/system/postgresql.service" containing:
# .include /lib/systemd/system/postgresql.service
# [Service]
# Environment=PGPORT=5433
# This will override the setting appearing below.
# Note: changing PGPORT or PGDATA will typically require adjusting SELinux
# configuration as well; see /usr/share/doc/postgresql-*/README.rpm-dist.
# Note: do not use a PGDATA pathname containing spaces, or you will
# break postgresql-setup.
# Note: in F-17 and beyond, /usr/lib/... is recommended in the .include line
# though /lib/... will still work.
[Unit]
Description=PostgreSQL database server
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
# Port number for server to listen on
Environment=PGPORT=5432
# Location of database directory
Environment=PGDATA=/usr/local/postgresql_9.6.2/data
# Where to send early-startup messages from the server (before the logging
# options of postgresql.conf take effect)
# This is normally controlled by the global default set by systemd
# StandardOutput=syslog
# Disable OOM kill on the postmaster
OOMScoreAdjust=-1000
ExecStart=/usr/local/postgresql_9.6.2/bin/pg_ctl start -D ${PGDATA} -s -o "-p ${PGPORT}" -w -t 300 -l /usr/local/postgresql_9.6.2/data/log/pg_server.log
ExecStop=/usr/local/postgresql_9.6.2/bin/pg_ctl stop -D ${PGDATA} -s -m fast
ExecReload=/usr/local/postgresql_9.6.2/bin/pg_ctl reload -D ${PGDATA} -s
# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300
[Install]
WantedBy=multi-user.target
10.自启动脚本都能够添加到systemctl自启动服务。
systemctl enable postgresql.service
systemctl start/restart/stop postgresql.service
四.安装supervisor。
1.安装supervisor。
pip install supervisor。
2.产生配置文件。
echo_supervisord_conf > /usr/local/etc/supervisord.conf。
3.supervisor服务启动脚本。
vim /lib/systemd/system/supervisord.service 。
supervisord开机自启动脚本(各版本系统):https://github.com/Supervisor/initscripts 。
# supervisord service for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /usr/local/etc/supervisord.conf ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target 4.自启动脚本都能够添加到systemctl自启动服务。 systemctl enable supervisord.service systemctl start/restart/stop supervisor.service
五.安装Sublime Text 3。
1.下载解压。
进入解压目录:ln -s /usr/local/sublime-text-3/sublime_text /usr/local/bin/sublime3。
2.安装插件管理器。
从 Sublime Text 3 官方获取用于安装Package Control 的代码。依次点击 View > Show Console (快捷键Ctrl + `)打开控制台。在控制台中粘贴官方代码,然后点击回车。最后重启。
3.安装插件。
主题Flatland,Agila,Material Theme,Brogrammer;扩展侧边栏中菜单选项SideBarEnhancements;终极 Python 插件Anaconda;文件创建AdvancedNewFile;版本控制git;函数生成描述DocBlockr_python;代码静态检查工具框架SublimeLinter-pyflakes。
4.配置。
修改快捷键 Sublime Text > Preferences > Package Settings > 插件 > Key Bindings – User。
修改配置Sublime Text > Preferences > Package Settings > 插件 > Settings – User。
5.配置python3版本。
Sublime Text > Preferences > Package Settings >Anaconda> Settings – default。将"python_interpreter": "python"改为"python_interpreter": "python3"。
原文地址:http://click.aliyun.com/m/20124/
plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
装一台新服务器环境的时候,装uwsgi报错:
plugins/python/uwsgi_python.h:2:20: Fatal error: Python.h: No such file or directory
#include <Python.h>
查了一下解决办法:
yum install python-devel.x86_64
之后再运行安装,问题解决。
Python 3.X上面使用mysqlDB问题
大家看看 这个代码 ,为何老是提示出错,我相关的库都安装了啊? import pymysql from sqlalchemy import Column, String, create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base pymysql.install_as_MySQLdb() Base = declarative_base() # 定义User对象: class User(Base): # 表的名字: __tablename__ = ''user'' # 表的结构: id = Column(String(20), primary_key=True) name = Column(String(20)) # 初始化数据库连接: engine = create_engine(''mysql+mysqlconnector://root:root@localhost:3306/mydatabase'') # 创建DBSession类型: DBSession = sessionmaker(bind=engine)
Traceback (most recent call last): File "E:\eclipse_workspace\javaEE\pymysql\app\run.py", line 19, in <module> engine = create_engine(''mysql+mysqlconnector://root:root@localhost:3306/mydatabase'') File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\sqlalchemy\engine\__init__.py", line 386, in create_engine return strategy.create(*args, **kwargs) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\sqlalchemy\engine\strategies.py", line 75, in create dbapi = dialect_cls.dbapi(**dbapi_args) File "C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\sqlalchemy\dialects\mysql\mysqlconnector.py", line 107, in dbapi from mysql import connector ImportError: No module named ''mysql''
python cookielib问题
初学练习模拟购物网站把商品加入购物车,使用cookielib保持cookie,可以登录成功,访问会员中心等都可以成功,但是把商品加入购物车这个操作就实现不了,只有用fiddler抓一次包后的cookie加入到urllib2 opener的header里面才可以把商品加入购物车这个操作做成功,代码如下
#coding=utf-8 import urllib2,urllib,cookielib import os from bs4 import BeautifulSoup from StringIO import StringIO import gzip import sys import re def youpiao(): url=''http://jiyou.11185.cn'' #!/usr/bin/python cj = cookielib.CookieJar() print(''1'') opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) login_url=''http://jiyou.11185.cn/login.html'' #username=raw_input(''username'') logindata=urllib.urlencode({''action'':''login'',''forward'':'''',''username'':''byew'',''password'':''879021q2''}) print(logindata) req=urllib2.Request(login_url,logindata) #print(req.code()) #wfile1=open("2.html","w") #wfile2=open("3.html","w") print(''-------------------------------------------------------------------------------------------------------------------'') print cj resp=opener.open(req) cjlist=[] for c in cj: j= c.name+''=''+c.value cjlist.append(j) cook=cjlist[1]+'';''+cjlist[0] print cook print type(cook) #print(resp.getcode()) #print(resp.geturl()) addcarturl=''http://jiyou.11185.cn/widget?type=shop_cart&action=add&ajax=yes'' addcartdata=urllib.urlencode({''num'':''1'',''productid'':''775414'',''itemtype'':''4'',''action'':''add'',''type_id'':''49'',''depot_id'':''1''}) opener.addheaders = [(''Host'', ''jiyou.11185.cn''), (''User-Agent'', ''Mozilla/5.0 (Windows NT 5.2; rv:29.0) Gecko/20100101 Firefox/29.0''), (''Accept'', ''application/json, text/javascript, */*; q=0.01''), (''Content-Type'',''application/x-www-form-urlencoded; charset=UTF-8''), (''X-Requested-With'',''XMLHttpRequest''), (''Content-Length'',''100''), #(''Cookie'',''JSESSIONID=xrsCVmyNtGTD65qGFHXkm1LStGdCRMvKwLvCFBk2hkwx2NxKjv0G!1623092871; BIGipServerjiyou_qiantian7009_pool=673097920.15733.0000''), #这是用fiddler抓包获取到的cookie值 (''Pragma'', ''no-cache''), (''Connection'', ''keep-alive''),] print(''*****************************'') resp1=opener.open(addcarturl,addcartdata) #file=resp.read() print cj fil2e=resp1.read() print fil2e cartresp=opener.open(''http://jiyou.11185.cn/cart.html'') wfile1=open("2.html","w") carttxt=cartresp.read() #print carttxt wfile1.write(carttxt) wfile1.close() #getitemid #print resp1.read() #wfile1.write(file) #wfile2.write(fil2e) #print(file) #print(resp.info()) print(''-------------------------------------------------------------------------------------------------------------------'') #print(fil2e) if __name__==''__main__'': print(''2'') youpiao() print(''3'')
这是用原本cookielib获取到的cookie值进行加入购物车操作
{"result":0,"message":"系统繁忙,稍后再试。"}
这是用fiddler抓包获取到的cookie值,加入header进行加入购物车操作
{"result":1,"message":"商品成功添加至购物车"}
今天关于Python re.sub问题和python re. sub的分享就到这里,希望大家有所收获,若想了解更多关于centos7安装python开发环境(python3_postgresql_sublime_supervisor)、plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory、Python 3.X上面使用mysqlDB问题、python cookielib问题等相关知识,可以在本站进行查询。
本文标签: