1、re的正则匹配字符串中不能带空格,否则无法匹配!
例如:
>>> re.findall('z','z ff')
此时要将空格去掉再匹配:
>>> a = 'z ff'.replace(' ','')
>>> re.findall('z',a)
2、验证匹配关系
如:
a = ['a','b','c','d','e']
b = ['a','e']
判断b是否属于a(b∈a),需要用set()将a和b均转化成集合再比较:
a = set(a)
b = set(b)
if b <= a:
print('xxx')
3、str转int (读取文件中适用)
a = '100'
b = int(a)
ValueError: invalid literal for int() with base 10
原因是str中包含非数字成分,所以要去除非数字成分,即可解决:
c = re.sub("\D", "", a)
b = int(a)
这里\D表示非数字成分
4、连续推出两层循环:
for a in xrange(10):
for b in xrange(20):
if something(a, b):
# Break the inner loop...
break
else:continue # Continue if the inner loop wasn't broken.
break # Inner loop was broken, break the outer.
5、socket.send()无法直接传入变量,如socket.send('hello,%s' % user)将会报错。此时要先定义好变量再传入:
data = 'hello,%s' % user
socket.send(data.encode('utf-8'))
6、string类型转成raw string类型(即r''):
背景:
repr(a)[1:-1]
7、当文件以二进制的格式打开时不能带编码方式,如:
with open(ab_file, "wb", encoding='utf-8') as f:
会报错:ValueError: binary mode doesn't take an encoding argument
去掉编码类型即可:
with (ab_file"wb"as :
8、pycharm中执行python程序时自动弹出Edit configuration配置项,无法执行程序。
原因:pycharm环境配置错误,例如python解释器找不到。
解决:Settings–>Project: python–>Python Interpreter设置正确的python解释器。
9、访问同一个类中另一个函数的变量:
在定义变量时加上 self. ,后期访问前加上对函数的调用,如:
class A():
def b(self):
self.b1 = 3
def c(self):
c1 = self.b1 + 5
print(c1)
obj = A()
obj.b()
obj.c()
# 此时打印c1的结果是8。这里是将b1定义为成员变量,只要在类中都可以访问。
注意:调用时一定要先调用b方法,否则找不到b1变量的声明会报错。
10、中断函数,不往下执行后面的代码:
return xxx
11、已经声明的队列(queue)属性不可更改,即没有设置队列持久化的队列不可重新设置成队列持久化,反之亦然。否则会报以下错误:
pika.exceptions.ChannelClosed: (406, "PRECONDITION_FAILED - inequivalent arg 'durable' for queue 'hello1' in vhost '/': received 'false' but current is 'true'")
12、Django admin搜索时报错:
django.core.exceptions.FieldError: Related Field got invalid lookup: icontains
原因:search_fields中的项存在非字符类型(特别注意没有__str__的外键字段,需要指向到表中的字符字段),修改如下:
search_fields = ['user__username', 'user__position', 'message']
再执行还是报错:
pymysql.err.InternalError: (1271, "Illegal mix of collations for operation 'like'")
原因:在 MySQL 5.5 以上,在编程时要对每个字段进行查找,在执行时可能就会出现时间字段 like '%中文%' 这种语法,需要修改源码:即将所有icontains的值与其上的contains一致
site-packages/django/db/backends/mysql/base.py
...
'icontains': 'LIKE BINARY %s',
...
'icontains': "LIKE BINARY CONCAT('%%', {}, '%%')",
13、Django执行migrate时报错:Duplicate column name
(helpdesk) [root@Fone helpdesk]# python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, captcha, contenttypes, django_celery_beat, helpdesk, sessions, sites
Running migrations:
Applying admin.0002_logentry_user...Traceback (most recent call last):
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.InternalError: (1060, "Duplicate column name 'user_id'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
self.execute(*args, **cmd_options)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
output = self.handle(*args, **options)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 203, in handle
fake_initial=fake_initial,
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/migrations/executor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
field,
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/mysql/schema.py", line 42, in add_field
super().add_field(model, field)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 435, in add_field
self.execute(sql, params)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 133, in execute
cursor.execute(sql, params)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 71, in execute
return self.cursor.execute(query, args)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/connections.py", line 517, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/connections.py", line 732, in _read_query_result
result.read()
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/connections.py", line 1075, in read
first_packet = self.connection._read_packet()
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/connections.py", line 684, in _read_packet
packet.check_error()
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/protocol.py", line 220, in check_error
err.raise_mysql_exception(self._data)
File "/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/pymysql/err.py", line 109, in raise_mysql_exception
raise errorclass(errno, errval)
django.db.utils.InternalError: (1060, "Duplicate column name 'user_id'")
error output
原因:/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/contrib/admin/migrations/0002_logentry_user.py与原有表结构冲突
解决方案:删除/root/.virtualenvs/helpdesk/lib/python3.7/site-packages/django/contrib/admin/migrations/中的所有迁移文件,重新执行makemigrations和migrate
注意:
Ⅰ. 项目原始使用django原生User表,当项目跑了一段时间要新增User字段(继承AbstractUser)时,要注意models中是否有关联到User表的字段(尽量使用settings.AUTH_USER_MODEL便于扩展),在迁移数据时注意不要将表结构覆盖了。
Ⅱ. 当迁移数据一直报错无法解决时使用以下方式迁移数据:
⒈ 将数据导出(-t 参数表示只导出数据,只用数据库名表示导出表结构和数据)
mysqldump -uroot -p -B {数据库名} --tables {表名} -t > backup_data.sql
mysqldump -uroot -p {数据库名} > backup_old.sql
⒉ 删除/创建数据库
drop database {数据库名};
create database {数据库名} charset='utf8';
⒊ 删除迁移文件(注意不要将migrations目录下的__init__.py误删)
rm {工程目录}/migrations/00*.py
rm {虚拟环境}/site-packages/django/contrib/admin/migrations/00*.py
4. 生成迁移文件&创建表结构
python manage.py makemigraions
python manage.py migrate
⒌ 导出数据(backup_new.sql)与原数据(backup_old.sql)进行比对
mysqldump -uroot -p {数据库名} > backup_new.sql
vimdiff backup_new.sql backup_old.sql
注意:此步是为了防止django创建表结构的字段顺序不同而出现的数据无法同步问题,如下:
(补图)
出现此类问题时需要将新表字段创建顺序改为旧表的创建顺序,保留有改变的表结构,再执行数据导入。
⒍ 去除backup_data.sql中已删除表数据,或将新增表的数据单独抽出来(抽出来的数据.sql还需要再执行一次导入)
⒎ 执行数据导入
mysql -uroot -p {数据库名} < backup_new.sql # 如果步骤5中比对有改变的话需要执行
mysql -uroot -p {数据库名} < backup_data.sql
mysql -uroot -p {数据库名} < 抽出来的数据.sql # 如果有的话
⒏ 启动项目检查数据展示情况
python manage.py runserver 0:8001