python 文件处理 https://www.cnblogs.com/foremostxl/p/9450820.html
- li = [['语文','数学','英语'],['100','122','123']]
- for i in li:
- print(','.join(i))
- # join 字符串拼接
语文, 数学, 英语
100,122,123
从原文件末尾开始写入
- # 方法一
- with open('user_info.txt','r+',encoding='utf-8') as f:
- f.read()
- f.write('a1')
- # 方法二
- with open('user_info.txt','r+',encoding='utf-8') as f:
- f.seek(0,2)
- f.write('a2')
- # 方法三
- with open('user_info.txt','a',encoding='utf-8') as f:
- f.write('a3')
- # 删除原文件的内容
- file.seek(0)
- file.truncate()
- # 重新设置文件读取指针到开头
- fo.seek(0, 0)
- # 重新设置文件读取指针到末尾
- fo.seek(0, 2)
pymysql 模块
- import pymysql
- # 建立连接
- conn = pymysql.connect(
- host='127.0.0.1',
- port=3306,
- user='root',
- password='123',
- db='db1',
- charset='utf8'
- )
- # 拿到游标
- cursor = conn.cursor()
- sql = 'select * from course'
- rows = cursor.execute(sql)
- res=cursor.fetchall()
- print(res)
- try:
- # 执行语句
- cursor.execute(sql)
- # 提交事物
- conn.commit()
- except Exception as e:
- print(e)
- # 事物的原子性
- conn.rollback()
- cursor.close()
- conn.close()
- # fetchone(每次查询一个),fetchmany(查询多个),fetchall 全部
正则表达式
- # 正则表达式 就是用来匹配字符串的一种 (语言) 工具
- # 它本身也有用自己的规则组成的一个字符串, 他是用自己的方式去表达目标字符串
- import re
- # 3 中常用的匹配模式
- #1, re.match(pattern,string), 匹配字符串开头的, 当匹配不到信息时返回 None
- string='abcddddddefg123 python_3.5123abc'
- # pattern = 'abc'
- # ret = re.match(pattern,string)
- # print(ret.group())
- # print(ret.span())
- #2,re.search(pattern,string) # 在字符串中间查找, 返回第一个匹配的
- # pattern='123'
- # pattern='wu'
- # ret = re.search(pattern,string)
- # print(ret.groups())
- # print(ret.span())
- # 3,re.findall(pattern,string)# 根据规则将字符串中所有信息都提取出来, 返回列表
- # pattern = 'abc'
- # ret = re.findall(pattern,string)
- # print(ret)
- # 一, 原子
- # 时正则表达式中最小的单位
- # 可以用普通字符做原子
- # string = "http://www.baidu.com"
- # pattern = 'baidu'
- # print(re.findall(pattern,string))
- #
- # # 非打印字符做原子
- # string = '''\nhttp://www.baidu.com/\n\thttp://www.baidu.com/\n'''
- # pattern = '\n'
- # print(re.findall(pattern,string))
- # 通用字符做原子
- # \d 匹配数值字符 0-9 \D 表示取反
- # \w 匹配 字符 数字 中文 下划线, \W 取反
- # \s 匹配空白字符 \S 表示取反
- # .(点) 匹配除了换行符 (\n) 外所有字符
- string = '''\nhttp://www456.baidu.com/\n\thttp://www123.baidu.com/\n'''
- # pattern='\d'
- # pattern='\D'
- # pattern = '\s'
- # print(re.findall(pattern,string))
- # string='abcdefg123 python 3.5$ 中文 \ t\n'
- # pattern = '\w\w\w\w\w\w\s\d.\d'
- # print(re.findall(pattern,string))
- # 二, 原子表(制定指定的单个字符规则), 用来表示一组字符, 可以指定范围和内容
- # ^ 表示取反, 写在原子表的里面
- # string = '2ython$ bcdefgcACV.67# [email protected] 1234567890! python¥_3.5 Zython'
- # pattern = '[\d\s]'
- # print(re.findall(pattern,string))
- # pattern = '[^1-3]'
- # # pattern = '[1-3a-fA-Z]'
- # pattern = '[.]'
- # print(re.findall(pattern,string))
- # 注意: 整个原子表只能表示一位字符
- # 练习, 将上面字符穿着所有的特殊符号拿下来
- # pattern = '[^\w\s.]'
- # print(re.findall(pattern,string))
- # 元字符
- # 边界限定元字符
- # ^ 目标在字符串的开头
- # $ 目标在字符串的结尾
- # \b 匹配单词边界
- # \B 匹配非单词边界
- # string = '123abcython$ abcdefgcACV# [email protected] 1234567890!abc python¥_3.5 abcs'
- # pattern = '^123'
- # pattern = 'abc$'
- # pattern = r'\babc'
- # pattern = r'abc\b'
- # # pattern = '\Babc'
- # print(re.findall(pattern,string))
- # 次数限定元字符
- # * 重复前面的一个字符 0 到 无穷次
- # + 重复前面的一个字符 1 到无穷次,
- # ? 重复前面的一个字符 0 - 1 次
- #{n} 重复前面的字符 n 次
- #{n,} 重复前面的字符 n 到无穷次
- #{n,m} 重复 n 到 m 次
- # string='123defg abc123python_abcdd_3.5abcddd'
- # pattern = 'abcd*'
- # pattern = 'abcd+'
- # pattern = 'abcd?'
- # pattern = 'abcd{
- 3
- }' # abcddd
- # pattern = 'abcd{
- 2,3
- }'# abcdd abcddd
- # print(re.findall(pattern,string))
- # 模式选择符 |
- # string1='abcdefg123 python_3.5abc 运动 python 特 php 价 php'
- # pattern = 'python|php'
- # print(re.findall(pattern,string1))
- # 模式单元符 () 小括号里面的表达式为 1 组
- # string='abc#python#cdcdcdcdefg123 abc#python#php_3333.52322abcpython'
- # pattern = '(cd)+'
- # print(re.findall(pattern,string))
- # 经常在其前后加个限定, 但是只会打印括号中表达式目标, 括号外的限定不会被打印
- # pattern = '#(python)#'
- # pattern = '#([a-z]+_?)(\d+.\d+)'
- # print(re.findall(pattern,string))
- # 贪婪模式 尽量多的匹配信息
- # 懒惰模式 在有多个结束条件时, 选择最前面的一个条件结束匹配
- # string='abcddddddefg123 python_3.5123abc'
- # pattern = 'abc.+123' # 贪婪模式
- # print(re.findall(pattern,string))
- # pattern = 'abc.+?123' # 懒惰模式
- # print(re.findall(pattern,string))
- # complie 编译正则表达式, 当正则表达式 重复多次使用时, 避免重复编译
- # re.S 让正则中 .(点) 能够匹配换行符
- # re.I 忽略大小写
- # pattern = re.compile('c.*123',re.S)
- # print(re.findall(pattern,string))
- # string='abcddddddefgA123 pythZon_3.5123abc'
- # string2='abc#python#cdcdcdcdefg\n123 abc#python#php_3333.52322abcpython'
- # print(re.findall(pattern,string2))
- # pattern2 = re.compile('[a-z]',re.I)
- # print(re.findall(pattern2,string))
- # split() 可以指定匹配字符进行分割
- # string = 'a1b2c3d4e5'
- # print(string.split('1'))
- # print(string.split('[0-9]'))# 正则表达式不起作用
- # pattern = '[0-9]'
- # ret = re.split(pattern,string,2)# 可以指定分割次数
- # print(ret)
- #sub() 指定匹配字符进行替换
- # string='a1 python b2c3d4 cython dython cython cython'
- # pattern = '[pcd]ython'
- # ret = re.sub(pattern,'php',string,3)# 指定替换次数
- # print(ret)
- # 练习: 写正则判断 ip 地址是否合法 '255.167.200.175'
- # 255.255.255.255 每一小段 0-255 之间均视为合法
- # string = '255.167.a200.175fsfs255.167.200.175fdfsdf255.167.200.175'
- # pattern = '\d{
- 1,3
- }\.\d{
- 1,3
- }\.\d{
- 1,3
- }\.\d{
- 1,3
- }' # \ 让点号 转义下, 只能表示他自身
- # pattern = '^(1\d{
- 2
- }|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{
- 2
- }|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{
- 2
- }|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{
- 2
- }|2[0-4]\d|25[0-5]|[1-9]\d|\d)$'
- # pattern = '([0-9.]{
- 4
- })'
- # print(re.findall(pattern,string))
- # 练习:
- # s='name: 小明 age:12,name: 小汪 age:3,name: 小李飞刀 age:18 我说: 哈哈哈'
- # ret = re.findall('name:(\w+) age:(\d+)',s)
- # print(ret)
- # 判断输入的数字是否是手机号码 # 138-8888-6666
- # phone = input("请输入一个手机号:")
- # if re.match('1[356789]\d{
- 9
- }',phone):
- # print("是个手机号码")
- # else:
- # print("不是手机号码")
- # 用正则表达式 将字符串中的字符和数字符号替换掉, 最终结果为 "北风网 上海"
- # string ="abcdefg 北风网 上海 404 not found"
- # pattern = '[a-z0-9\s]'
- # # print(re.findall(pattern,string))
- # # print(re.sub('[a-z0-9]','',string).strip())
- #
- # # -----------------------------------------------------------------
- # pattern = '[a-z0-9\s]'
- # print(re.findall(pattern,string))
- # # 这个正则的过程等价于下面的程序
- # str1 = 'abcdefghijklmnopqrstuvwxyz'
- # num = '0123456789'
- # w = ' '
- # target=[]
- # for i in string:
- # if i in str1 or i in num or i in w:
- # target.append(i)
- # print(target)
- # ----------------------------------------------------------------
- # pattern = re.compile("\s([一二三四五六七八九十]{1,3})\s{0,3}(\w{7})\s")
- # f = open('D:\File_Class \ 倚天屠龙记. txt','r',errors='ignore')
- # c = f.read()
- # # print(c)
- # t = re.findall(pattern,c)
- # print(t)
- # f.close()
- View Code
来源: http://www.bubuko.com/infodetail-3277123.html