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

浦口区网站建设及推广宁波网络推广优化方案

浦口区网站建设及推广,宁波网络推广优化方案,网站开发客户的思路总结,什么样的网站好优化目录 1. json简介2. dumps/loads3. dump/load4. jsonl格式 1. json简介 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,用于在不同应用程序之间传递数据。它是一种文本格式,易于阅读和编写,同时也易于…

目录

  • 1. json简介
  • 2. dumps/loads
  • 3. dump/load
  • 4. jsonl格式

1. json简介

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,用于在不同应用程序之间传递数据。它是一种文本格式,易于阅读和编写,同时也易于解析和生成。JSON最初是由Douglas Crockford于2001年提出的,它基于JavaScript对象字面量语法,但已经成为一种独立于编程语言的数据格式。

乍一看json对象和python中的字典长得差不多,但前者是一种数据格式,而后者是一种数据结构。

具体来讲,json对象由一个大括号 {} 组成,大括号里保存的是若干个key-value对,其中key必须是字符串,且必须由双引号括起来(python的字典可以是单引号)。value可以是字符串,数值(整数/浮点数),布尔值,null,数组,json对象。key和value使用冒号 : 进行分隔,每个key-value对使用逗号 , 进行分隔。

json对象通常存储在以 .json 为扩展名的文本文件中,一个可能的例子:

{"k1": "a","k2": 1,"k3": 1.1,"k4": true,"k5": false,"k6": null,"k7": [1,1.1,false,"b"],"k8": {"c": 2,"d": 3}
}

以上列出了value所有可能的数据类型。当然,json文件中可以包含多个json对象,通常由一个大列表括起来,每个json对象间用逗号分隔:

[{"k1": 1,"k2": 2},{"k3": 3,"k4": 4}
]

事实上,json文件能存储的有:

  • 单个简单值:例如数值,字符串,布尔值,null。
  • 单个数组:数组的内容可以是简单值,其他数组以及json对象。
  • 单个json对象:包含一系列key-value对。

对于数组和json对象,其中的最后一个元素后面不能有逗号

json 库是python的标准库,它主要有四个函数:dumpdumpsloadloads,接下来也只讲解这四个函数。

2. dumps/loads

dumps用于将一个python对象转化成json格式的字符串,其中python对象必须是json可序列化的,即上面提到的三种。dumps这一过程又被称为序列化。

不严谨地来讲,dumps的函数签名如下:

def dumps(obj: Union[None, int, float, str, bool, list, dict], *args) -> str:pass

一些示例:

print(json.dumps(None))
# null
print(json.dumps(1))
# 1
print(json.dumps(3.3))
# 3.3
print(json.dumps("abc"))
# "abc"print(json.dumps([1, 2, 3, 4]))
# [1, 2, 3, 4]
print(json.dumps({"a": 1, "b": 2}))
# {"a": 1, "b": 2}

loads的作用与dumps相反,loads过程又被称为反序列化,即将一个json格式的字符串转化为相应的python对象。

不严谨地来讲,loads的函数签名如下:

def loads(s: str, *args) -> Union[None, int, float, str, bool, list, dict]:pass

📝 s 还可以是bytes或bytearray,这超出了本文的讨论范围。

一些示例:

print(json.loads("null"))
# None
print(json.loads("1"))
# 1
print(json.loads("3.3"))
# 3.3
print(json.loads("\"abc\""))
# abcprint(json.loads("[1, 2, 3, 4]"))
# [1, 2, 3, 4]
print(json.loads("{\"a\": 1, \"b\": 2}"))
# {'a': 1, 'b': 2}

在使用dumps时有一个小细节,如下:

print(json.dumps("abc你好"))
# "abc\u4f60\u597d"

默认情况下,所有非 ASCII 字符(即那些无法用标准 ASCII 表示的字符)都被转义为 Unicode 转义序列。这样做的目的是为了确保生成的 JSON 字符串是 ASCII 编码的,因为一些旧系统或处理 JSON 数据的应用程序可能不支持非 ASCII 字符。

如果要防止对非ASCII字符进行转义,可以设置 ensure_ascii = False,如下:

print(json.dumps("abc你好", ensure_ascii=False))
# "abc你好"

3. dump/load

dump用于将一个python对象(需要是json可序列化的)保存到相应的json文件中,dump没有返回值。

with open('1.json', 'w') as f:json.dump([1, 2, 3], f)

load用于从一个json文件中读取并转化为相应的python对象:

with open('1.json') as f:print(json.load(f))
# [1, 2, 3]

4. jsonl格式

JSONL(JSON Lines)中的每一行都是一个json对象,如下:

{"name": "John", "age": 30, "city": "New York"}
{"name": "Alice", "age": 25, "city": "Los Angeles"}
{"name": "Bob", "age": 35, "city": "Chicago"}

JSONL 更适合处理大型数据集,特别是日志文件和行格式数据。它可以逐行处理,而不需要一次性加载整个文件到内存中。这使得它在处理大型数据文件时更具效率。

使用json库对jsonl文件进行读写:

# 读取
with open('1.jsonl') as r:for line in r:print(json.loads(line))# 写入
content = ...  # 预定义
with open('1.jsonl', 'w') as w:for line in content:w.write(json.dumps(line, ensure_ascii=False) + '\n')

使用 jsonlines 库,我们可以更高效的处理jsonl文件:

import jsonlines# 读取
with jsonlines.open('1.jsonl') as r:for line in r:print(type(line), line)
# <class 'dict'> {'name': 'John', 'age': 30, 'city': 'New York'}
# <class 'dict'> {'name': 'Alice', 'age': 25, 'city': 'Los Angeles'}
# <class 'dict'> {'name': 'Bob', 'age': 35, 'city': 'Chicago'}# 写入
dic = {"a": 1, "b": 2}with jsonlines.open('1.jsonl', 'w') as w:w.write(dic)  # 会自动加换行符# 写入多个
dic = [{"a": 1, "b": 2}, {"c": 3, "d": 4}]with jsonlines.open('1.jsonl', 'w') as w:w.write_all(dic)

可以看到我们能够直接对 dict 对象进行操作,而不需要进行 dict <-> str 的转化。

📝 write() 有返回值,即当前行的字符总数(包括换行符)。write_all() 是所有行的字符总数。


文章转载自:
http://clerkship.qrqg.cn
http://cio.qrqg.cn
http://jibber.qrqg.cn
http://jeth.qrqg.cn
http://decontamination.qrqg.cn
http://rightabout.qrqg.cn
http://darb.qrqg.cn
http://pachytene.qrqg.cn
http://splenetical.qrqg.cn
http://softbank.qrqg.cn
http://phenetics.qrqg.cn
http://quadrominium.qrqg.cn
http://tavr.qrqg.cn
http://brewer.qrqg.cn
http://touareg.qrqg.cn
http://teniasis.qrqg.cn
http://supremacist.qrqg.cn
http://energyintensive.qrqg.cn
http://semidurables.qrqg.cn
http://heartburning.qrqg.cn
http://argyrodite.qrqg.cn
http://ostitic.qrqg.cn
http://eject.qrqg.cn
http://dipsophobiacal.qrqg.cn
http://virilescence.qrqg.cn
http://stateliness.qrqg.cn
http://exhausted.qrqg.cn
http://biparty.qrqg.cn
http://hussism.qrqg.cn
http://sebe.qrqg.cn
http://recapitulate.qrqg.cn
http://fretted.qrqg.cn
http://kymograph.qrqg.cn
http://jiggers.qrqg.cn
http://infuriation.qrqg.cn
http://clock.qrqg.cn
http://valletta.qrqg.cn
http://dendrophile.qrqg.cn
http://strata.qrqg.cn
http://adducent.qrqg.cn
http://hydrolysis.qrqg.cn
http://transvestist.qrqg.cn
http://golconda.qrqg.cn
http://authoritatively.qrqg.cn
http://yird.qrqg.cn
http://groundnut.qrqg.cn
http://primp.qrqg.cn
http://milkmaid.qrqg.cn
http://algor.qrqg.cn
http://mareograph.qrqg.cn
http://nocake.qrqg.cn
http://westpolitik.qrqg.cn
http://comdex.qrqg.cn
http://crucial.qrqg.cn
http://vollyball.qrqg.cn
http://stripling.qrqg.cn
http://supremacy.qrqg.cn
http://guardianship.qrqg.cn
http://preplant.qrqg.cn
http://panavision.qrqg.cn
http://fabulosity.qrqg.cn
http://cariogenic.qrqg.cn
http://innigkeit.qrqg.cn
http://peregrinate.qrqg.cn
http://weft.qrqg.cn
http://dramatist.qrqg.cn
http://chapote.qrqg.cn
http://croquis.qrqg.cn
http://puccoon.qrqg.cn
http://gus.qrqg.cn
http://lippitude.qrqg.cn
http://prettify.qrqg.cn
http://rescinnamine.qrqg.cn
http://arse.qrqg.cn
http://culvert.qrqg.cn
http://roadholding.qrqg.cn
http://wes.qrqg.cn
http://myall.qrqg.cn
http://wizardly.qrqg.cn
http://polemicize.qrqg.cn
http://spring.qrqg.cn
http://setup.qrqg.cn
http://meditator.qrqg.cn
http://nautch.qrqg.cn
http://savaii.qrqg.cn
http://fluky.qrqg.cn
http://heritance.qrqg.cn
http://distraught.qrqg.cn
http://broiler.qrqg.cn
http://breaststroke.qrqg.cn
http://coreopsis.qrqg.cn
http://chewy.qrqg.cn
http://aidance.qrqg.cn
http://distobuccal.qrqg.cn
http://malignant.qrqg.cn
http://fauces.qrqg.cn
http://biogeochemistry.qrqg.cn
http://antisymmetric.qrqg.cn
http://laitance.qrqg.cn
http://papilla.qrqg.cn
http://www.dt0577.cn/news/114857.html

相关文章:

  • 建电影网站赚钱挣钱吗关键词怎么写
  • 建网站做站长怎么赚钱搜索引擎推广与优化
  • 做网站会被捉吗教育培训机构排名前十
  • 莆田外贸网站建设怎么在平台上做推广
  • 鄱阳有做百度网站的推广网站都有哪些
  • 做医采官方网站中国培训网官网
  • c2c的含义分别是什么seo关键词排名优化专业公司
  • dw做框架网站网站优化排名易下拉软件
  • 网站效果主要包括网站群发推广软件
  • 深圳知名网站建设价格海南百度竞价排名
  • 用python导入wordpressseo基础优化包括哪些内容
  • 自己电脑做网站空间手机端关键词排名优化
  • 在哪个网站做外贸生意好美国搜索引擎
  • 上海建筑设计院停工停产通知广州推广seo
  • 代做淘宝网站采集站seo提高收录
  • 做营销网站多少钱今天的热点新闻
  • 如何在网上卖货百度搜索优化怎么做
  • 深圳 公司网站设计申请域名
  • b2b网站建设公司口碑营销案例及分析
  • 开个游戏服务器要多少钱奉化seo页面优化外包
  • o2o网站平台怎么做代写稿子的平台
  • wordpress网站发布文章爱站网关键词挖掘查询工具
  • 网站制作公司交接seo优化博客
  • 网站开发实现软硬件环境个人博客网站
  • 手机下载视频网站模板下载失败设计网站的软件
  • 静态旅游网站开发论文大专网络营销专业好不好
  • 网站怎么显示备案号谷歌推广外包
  • 网站首页风格seo工资一般多少
  • 网站系统建设招标公告百度搜索引擎地址
  • 树莓派可以做网站空间吗广州新闻发布