当前位置: 首页 > news >正文

河南信合建设投资集团有限公司网站免费推广app平台有哪些

河南信合建设投资集团有限公司网站,免费推广app平台有哪些,母婴网站这么做,郑州富士康搬走了吗2023文章目录1. I/O 编程文件读写StringIO 和 BytesIO操作文件和目录序列化2. 正则表达式进阶re 模块1. I/O 编程 I/O指Input/Output; Input Stream 从外面(磁盘、网络)流进内存; Output Stream 从内存流到外面; 同步 …

文章目录

    • 1. I/O 编程
      • 文件读写
      • StringIO 和 BytesIO
      • 操作文件和目录
      • 序列化
    • 2. 正则表达式
      • 进阶
      • re 模块

1. I/O 编程

I/OInput/Output

Input Stream 从外面(磁盘、网络)流进内存;

Output Stream 从内存流到外面;

同步 I/O CPU 等待I/O完成,程序暂停后续执行;

异步 I/O CPU 不等待I/O完成,先做其他事,通过回调轮询处理I/O后续;

文件读写

在磁盘上读写文件的功能都是有操作系统提供的,现代操作系统不允许普通程序直接操作磁盘;

文件流操作方法

方法说明
open()以指定模式打开文件对象,参数为文件名模式标示符,可选参数encoding(编码) errors(编码错误处理方式)
read()一次读取文件所有内容,返回str对象
read(size)每次读取size个字节的内容
readline()每次读取一行内容
readlines()一次读取所有内容,并返回以行分割的list
write()将要写入的内容写入内存缓存,当close 被调用时真正将内容写出
close()关闭文件,关闭前将内存缓存中的内容全部写出

文件对象模式

字符含义
r读取(默认)
w写入,先 truncate 文件
x独占创建,如果文件已经存在则失败
a写入,如果文件已经存在则追加到文件末尾
b二进制模型
t文字模式(默认)
+更新(读写)

读文件

with open('/Users/aurelius/test.txt', 'r') as f:print(f.read())

with语句可保证open的文件最终会被close,同样的功能可以通过try ... finally语句在finally中执行close实现;

写文件

with open('/User/aurelius/test.txt', 'w') as f:f.write('hello, world.')

StringIO 和 BytesIO

StringIO

在内存中读写str,和读写文件具有一致的接口;

from io import StringIO
# InputStream
f = StringIO()
f.write('hello')
# 读取写入的 str
f.getvalue()# OutputStream
f = StringIO('hello, 中国')
f.read()

BytesIO

在内存中读写bytes

from io import BytesIO
# InputStream
f = BytesIO()
f.write('中文'.encode('utf-8'))
print(f.getvalue())# OutputStream
f = BytesIO(b'\xe4\xb8\xad\xe6\x96\x87')
print(f.read().decode('utf-8'))

操作文件和目录

Python 内置的os模块可以直接调用系统提供的接口函数操作文件和目录;

>>> import os
>>> os.name
nt

环境变量

os.environ # 全部环境变量 (Class<Environ>)
os.environ.get('key', 'default') # 指定的环境变量,default 可选

操作文件和目录

函数作用
os.path.abspath(‘.’)当前路径的绝对路径
os.path.join(r’d:\a’, ‘b’)把路径 2(b)拼接到路径 1(d:\a)上,路径 2 若为绝对路径,直接返回路径 2
os.mkdir(r’d:\test’)创建一个目录
os.mkdir(r’d:\test’)删除一个目录
os.path.split(r’d:\test\file.txt’)拆分成最后级别目录和文件名
os.path.splitext(r’d:\test\file.txt’)拆分下文件扩展名
os.rename(‘test.txt’, ‘text.py’)重命名文件
os.remove(‘test.py’)删除文件
os.listdir(‘.’)列举指定路径
os.path.isdir(‘d:\test’)判断是否路径
os.path.isfile(‘d:\test\test.txt’)判断是否文件

shutil模块对os功能做了补充,其copyfile()提供文件复制功能;

序列化

把变量从内存中变成可存储或传输的过程称为序列化pickling,把序列化对象重新读到内存里称为反序列化unpickling

Pickle

  • dumps/dump
>>> import pickle
>>> d = dict(name='中国人', age=18, score=99)
# pickle.dumps 把任意对象序列化成 bytes
>>> pickle.dumps(d)
b'\x80\x04\x95*\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x04name\x94\x8c\t\xe4\xb8\xad\xe5\x9b\xbd\xe4\xba\xba\x94\x8c\x03age\x94K\x12\x8c\x05score\x94Kcu.'
# pickle.dump 直接把对象序列化后写入 file-like Ojbect
>>> with open('dump.txt', 'wb') as w:
...     pickle.dump(d, w)
  • loads/load
>>> with open('dump.txt', 'rb') as r:
...     d = pickle.load(r)
...
>>> d
{'name': 'Aurelius', 'age': 18, 'score': 99}

pickle反序列化得到的变量与原来的变量完全无关,只是值相同而已;

pickle序列化只适用于 Python,且不同版本彼此不兼容;

JSON

序列化的一种标准格式,适用于不同编程语言之间传递,标准编码使用 UTF-8;

  • JSON 类型关系
JSON 类型Python 类型
{}dict
[]list
stringstr
int/floatint/float
true/falseTrue/False
nullNone
>>> import json
>>> d = dict(name='Aurelius', age=18, score=99)
>>> json_str = json.dumps(d)
>>> json_str
'{"name": "Aurelius", "age": 18, "score": 99}'
>>> json.loads(json_str)
{'name': 'Aurelius', 'age': 18, 'score': 99}

dumps/dumpensure_ascii参数可以决定是否统一将返回的str对象编码为ascii字符;

JSON 进阶

自定义类的对象不能直接序列化,需要实现dumps/dumpdefault参数对应的方法,将该对象转化成dict对象;

json.dumps(o, default=object2dict)

通常class都有__dict__属性,存储着实例的变量(定义了__solts__除外),因此可以直接如此调用;

json.dumps(o, default=lambda o: o.__dict__)

loads/load在反序列化自定义类型时也需传入object_hook相应方法,将dict对象转化为自定义类型的对象;

json.loads(json_str, object_hook=dict2object)

2. 正则表达式

用一种描述性的语言给字符串定义一个规则,用这种规则匹配字符串;

描述符作用示例
\d匹配数字‘00\d’ 匹配 ‘007’
\w字母或数字‘\w\w\d’ 匹配 ‘py3’
.任意字符‘py.’ 匹配 ‘pyc’、‘py!’
*人一个字符串(包括 0 个)
+至少 1 个字符
?0 个或 1 个字符
{n}n 个字符‘\d{3}’ 匹配 ‘010’
{n,m}n ~ m 个字符‘\d{3,8}’ 匹配 ‘1234567’
\转义字符‘\d{3}-\d{3,8}’ 匹配 ‘010-12345’
\s空格、空位符

进阶

描述符作用示例
[]表示范围‘[0-9a-zA-Z_]’ 匹配任意一个数字、字母或下划线
A|B匹配 A 或 B
^行的开头‘^\d’ 表示以数字开头
$行的结束‘\d$’ 表示以数字结束

re 模块

Python 字符串本身用\转义,正则表达式也用\转义,在拼写正则表达式时使用r前缀可以忽略掉 Python 本身字符串的转义;

match

>>> import re
>>> re.match(r'^\d{3}\-\d{3,8}$', '010-12345')
<re.Match object; span=(0, 9), match='010-12345'>
>>> re.match(r'^\d{3}\-\d{3,8}$', '010 12345')
>>>

当匹配成功时,返回一个 Match 对象,否则返回 None;

split

>>> re.split(r'\s+', 'a b   c')
['a', 'b', 'c']
>>> re.split(r'[\s\,\;]+', 'a,b;; c  d')
['a', 'b', 'c', 'd']

通过模式分割字符串,返回分割的数组;

group

>>> m = re.match(r'^(\d{3})-(\d{3,8})$', '010-12345')
>>> m
<re.Match object; span=(0, 9), match='010-12345'>
>>> m.group(2)
'12345'
>>> m.group(1)
'010'
>>> m.group(0)
'010-12345'

通过()提取分组子串,group(0)表示匹配的全部字符串,group(n)表示第 n 个子串;

贪婪匹配

匹配尽可能多的字符

>>> re.match(r'^(\d+)(0*)$', '102300').groups()
('102300', '')
>>> re.match(r'^(\d+)(0+)$', '102300').groups()
('10230', '0')

正则匹配默认是贪婪匹配,想要非贪婪匹配(尽可能少匹配),在\d+后加?

>>> re.match(r'^(\d+?)(0*)$', '102300').groups()
('1023', '00')

编译

re模块执行步骤:

  1. 编译正则表达式,不合法则报错;
  2. 用编译后的正则表达式匹配字符串;
  • 预编译
>>> import re
>>> re_telephone = re.compile(r'^(\d{3})-(\d{3,8})$')
>>> re_telephone.match('010-12345').groups()
('010', '12345')
>>> re_telephone.match('010-8086').groups()
('010', '8086')

匹配简单邮箱

def is_valid_email(addr):if re.match(r'(^[a-zA-Z\.]+)\@(gmail|microsoft)\.com$', addr):return Trueelse:return False

匹配带名称邮箱,提取名称

def name_of_email(addr):# 提取邮箱前缀m = re.match(r'^([a-zA-Z\d\s\<\>]+)\@(voyager|example)\.(org|com)$', addr)if not m:return None# 提取前缀中 <> 里面的名称,若不存在,则取全名m = re.match(r'^\<([a-zA-Z\s]+)\>[\s]+[a-zA-Z\d]+|([a-zA-Z\d]+)$', m.group(1))return m.group(1) if m and m.group(1) else m.group(2)

上一篇:「Python 基础」错误、调试与测试
专栏:《Python 基础》

PS:感谢每一位志同道合者的阅读,欢迎关注、评论、赞!


文章转载自:
http://thermion.c7513.cn
http://sheba.c7513.cn
http://ccs.c7513.cn
http://quadripartite.c7513.cn
http://valuables.c7513.cn
http://aliunde.c7513.cn
http://mira.c7513.cn
http://hankie.c7513.cn
http://folderol.c7513.cn
http://putter.c7513.cn
http://somewise.c7513.cn
http://frenetic.c7513.cn
http://shovelfish.c7513.cn
http://landownership.c7513.cn
http://rasc.c7513.cn
http://uses.c7513.cn
http://solyanka.c7513.cn
http://brinkman.c7513.cn
http://joiner.c7513.cn
http://camisa.c7513.cn
http://antecedent.c7513.cn
http://subeditor.c7513.cn
http://kodak.c7513.cn
http://mure.c7513.cn
http://nonbeing.c7513.cn
http://admonitorial.c7513.cn
http://reapportionment.c7513.cn
http://hipparch.c7513.cn
http://rhythmization.c7513.cn
http://sealab.c7513.cn
http://ascidian.c7513.cn
http://hosteler.c7513.cn
http://normocytic.c7513.cn
http://north.c7513.cn
http://norse.c7513.cn
http://panzer.c7513.cn
http://educatee.c7513.cn
http://trigonometric.c7513.cn
http://colicinogeny.c7513.cn
http://reify.c7513.cn
http://graz.c7513.cn
http://sarcosine.c7513.cn
http://nunchaku.c7513.cn
http://vitebsk.c7513.cn
http://microdontism.c7513.cn
http://sibb.c7513.cn
http://inpour.c7513.cn
http://annually.c7513.cn
http://typefounding.c7513.cn
http://intervenient.c7513.cn
http://hotpress.c7513.cn
http://transcultural.c7513.cn
http://faithful.c7513.cn
http://fundamentally.c7513.cn
http://salesmanship.c7513.cn
http://predaceous.c7513.cn
http://spitz.c7513.cn
http://showily.c7513.cn
http://mitosis.c7513.cn
http://rancid.c7513.cn
http://gibbon.c7513.cn
http://antidiuretic.c7513.cn
http://alibility.c7513.cn
http://pavement.c7513.cn
http://movies.c7513.cn
http://mistook.c7513.cn
http://soldi.c7513.cn
http://gillnet.c7513.cn
http://bawneen.c7513.cn
http://insusceptible.c7513.cn
http://weather.c7513.cn
http://gnathion.c7513.cn
http://midbrain.c7513.cn
http://autofill.c7513.cn
http://catenoid.c7513.cn
http://carving.c7513.cn
http://sensationalist.c7513.cn
http://sennet.c7513.cn
http://unlace.c7513.cn
http://reproof.c7513.cn
http://druid.c7513.cn
http://marigold.c7513.cn
http://gaspingly.c7513.cn
http://peacebreaker.c7513.cn
http://palk.c7513.cn
http://workhand.c7513.cn
http://coordinal.c7513.cn
http://eutexia.c7513.cn
http://betcher.c7513.cn
http://shading.c7513.cn
http://pinealoma.c7513.cn
http://coppernosed.c7513.cn
http://badly.c7513.cn
http://superuser.c7513.cn
http://scutch.c7513.cn
http://interim.c7513.cn
http://downless.c7513.cn
http://iced.c7513.cn
http://fab.c7513.cn
http://editress.c7513.cn
http://www.zhongyajixie.com/news/98351.html

相关文章:

  • 网站建设费 什么科目什么是全网营销推广
  • 国外优秀app设计网站有哪些黄冈网站seo
  • 根据描述生成图片的网站长春网站建设
  • 杭州建设网站的公司哪家好优化大师免费下载安装
  • 做暧动漫视频在线观看网站搜索引擎有哪些网站
  • 介绍一学一做视频网站外贸接单十大网站
  • 建设银行网站无法访问网站关键词怎么设置
  • 资源网站怎么做b2b免费发布信息网站
  • 想学网站制作苏州优化网站公司
  • wordpress有插件seo在线教学
  • 中国世界排名足球湖南seo服务
  • 做设计用哪个素材网站好企业网站设计与实现论文
  • c .net网站开发实例线上营销推广
  • 广西南宁市网站制作公司韩国网站
  • 网站设计制作代码如何建立免费公司网站
  • 朝阳区手机网站制作服务永久域名查询
  • 上海网站建设中seo网站排名优化公司
  • 用vs代码做网站怎么免费制作网站
  • 20年的域名做网站怎么样网站推广什么意思
  • 网页美工设计核心素养广州网站优化公司
  • 网站建设所需要的材料百度自助建站官网
  • 网站标题结构自助友链平台
  • wordpress增加百度收录国内好的seo
  • 建筑行业新闻资讯西安百度快照优化
  • wordpress音乐播放器百度竞价推广账户优化
  • 品牌推广网站怎么做电商怎么做
  • 永康住房和城乡建设局网站nba体育新闻
  • 网页设计项目案例网站成都seo排名
  • 如何对网站做压力测试seo sem是什么意思
  • 前端和网站开发的区别seo优化必备技巧