mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-03 23:40:14 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
f000a021a9
@ -0,0 +1,72 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (heguangzhi)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-11130-1.html)
|
||||
[#]: subject: (Become a lifelong learner and succeed at work)
|
||||
[#]: via: (https://opensource.com/open-organization/19/7/informal-learning-adaptability)
|
||||
[#]: author: (Colin Willis https://opensource.com/users/colinwillishttps://opensource.com/users/marcobravo)
|
||||
|
||||
|
||||
成为终身学习者,并在工作中取得成功
|
||||
======
|
||||
|
||||
> 在具有适应性文化的开放组织中,学习应该一直持续 —— 但不会总是出现在正式场合。我们真的明白它是如何工作的吗?
|
||||
|
||||
![Writing in a notebook][1]
|
||||
|
||||
持续学习是指人们为发展自己而进行的持续的、职业驱动的、有意识的学习过程。对于那些自认是持续学习者的人来说,学习从未停止 —— 这些人从日常经历中看到学习机会。与同事进行辩论、反思反馈、在互联网上寻找问题的解决方案、尝试新事物或冒险都是一个人在工作中可以进行的非正式学习活动的例子。
|
||||
|
||||
持续学习是开放组织中任何人的核心能力。毕竟,开放的组织是建立在同行相互思考、争论和行动的基础上的。在开放组织的模棱两可、话语驱动的世界中茁壮成长,每天都需要员工具备这些技能。
|
||||
|
||||
不幸的是,科学文献在传播我们在工作中学习的知识方面、帮助个人欣赏和发展自己的学习能力方面,做得很差。因此,在本文系列中,我将向你介绍非正式学习,并帮助你理解将学习视为一种技能会如何帮助你在任何组织中茁壮成长,尤其是在开放式组织中。
|
||||
|
||||
### 为什么这么正式?
|
||||
|
||||
迄今为止,对组织中学习的科学研究主要集中在正式学习而不是非正式学习的设计、交付和评估上。
|
||||
|
||||
投资于员工知识、技能和能力的发展是一个组织保持其相对于竞争对手优势的重要方式。组织通过创建或购买课程、在线课程、研讨会等来使学习机会正规化。这些课程旨在像个人传授与工作相关的内容,就像学校里的班级一样。对于一个组织来说,提供一门课程是一种简单(如果成本高昂的话)的方法,可以确保其员工的技能或知识保持最新。同样,教室环境是研究人员的天然实验室,使得基于培训的研究和工作不仅可能,而且强大。
|
||||
|
||||
当然,有些东西人们不需要培训来学习;通常,人们通过研究答案、与同事交谈、思考、实验或适应变化来学习。事实上,[最近的评估表明][2] 70% 到 80% 的与工作相关的知识不是在培训中学到的,而是在工作中非正式学到的。这并不是说正规的培训无效;培训可能非常有效,但它是一种精确的干预方式。在工作的大部分方面正式培训一个人是不现实的,尤其是当这些工作变得更加复杂的时候。
|
||||
|
||||
因此,非正式学习,或者任何发生在结构化学习环境之外的学习,对工作场所来说是极其重要的。事实上,[最近的科学证据][3]表明,非正式学习比正式培训更能预测工作表现。
|
||||
|
||||
那么,为什么机构和科学界如此关注培训呢?
|
||||
|
||||
### 循环过程
|
||||
|
||||
除了我前面提到的原因,研究非正式学习可能非常困难。与正式学习不同,非正式学习发生在非结构化环境中,高度依赖于个人,很难或不可能观察到。
|
||||
|
||||
直到最近,大多数关于非正式学习的研究都集中在定义非正式学习的合格特征和确定非正式学习在理论上是如何与工作经验联系在一起的。研究人员描述了一个[动态的周期性过程][4],通过这个过程,个人可以在组织中非正式地学习。
|
||||
|
||||
与正式学习一样,非正式学习发生在非结构化环境中,高度依赖于个人,很难或不可能观察。
|
||||
|
||||
在这个过程中,个人和组织都有创造学习机会的机构。例如,一个人可能对学习某样东西感兴趣,并为此表现出学习行为。组织以向个人提供反馈的形式,可能表明需要学习。这可能是一个糟糕的绩效评估、一个在项目中发表的评论、或者一个不是个人指导的组织环境的更广泛的变化。这些力量在组织环境中(例如,有人尝试了一个新想法,他或她的同事认识到并奖励了这种行为)或者通过在个人的头脑中反思(例如,有人反思了关于他或她的表现的,并决定在学习工作中付出更多的努力)。与培训不同,非正式学习不遵循正式的线性过程。个人可以随时体验过程的任何部分,同时体验过程的多个部分。
|
||||
|
||||
### 开放组织中的非正式学习
|
||||
|
||||
具体而言,在开放组织中,既减少了对等级制度的重视程度,又更加注重对参与式文化的重视程度,这两者都推动了这种非正式的学习。简而言之,开放式组织只是为个人和组织环境提供了更多互动和激发学习的机会。此外,想法和变革需要开放式组织中员工给予更广泛的认同 —— 而认同需要对他人的适应性和洞察力的欣赏。
|
||||
|
||||
也就是说,仅仅增加学习机会并不能保证学习会发生或成功。有人甚至可能会说,开放式组织中常见的模糊性和公开性话语可能会阻止不擅长持续学习的人——同样,随着时间的推移学习的习惯和开放式组织的核心能力——尽可能有效地为组织做出贡献。
|
||||
|
||||
解决这些问题需要一种以一致的方式跟踪非正式学习。最近,科学界呼吁创造衡量非正式学习的方法,这样就可以进行系统的研究来解决非正式学习的前因后果的问题。我自己的研究集中在这一呼吁上,我花了几年时间发展和完善我们对非正式学习行为的理解,以便可以衡量它们。
|
||||
|
||||
在本文系列的第二部分,我将重点介绍我最近在一个开放式组织中进行的一项研究的成果,在该研究中,我测试了我对非正式学习行为的研究,并将它们与更广泛的工作环境和个人工作成果联系起来。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/open-organization/19/7/informal-learning-adaptability
|
||||
|
||||
作者:[Colin Willis][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[heguangzhi](https://github.com/heguangzhi)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/colinwillishttps://opensource.com/users/marcobravo
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/notebook-writing-pen.jpg?itok=uA3dCfu_ (Writing in a notebook)
|
||||
[2]: https://www.groupoe.com/images/Accelerating_On-the-Job-Learning_-_White_Paper.pdf
|
||||
[3]: https://www.researchgate.net/publication/316490244_Antecedents_and_Outcomes_of_Informal_Learning_Behaviors_a_Meta-Analysis
|
||||
[4]: https://psycnet.apa.org/record/2008-13469-009
|
@ -1,134 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (HankChow)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Save and load Python data with JSON)
|
||||
[#]: via: (https://opensource.com/article/19/7/save-and-load-data-python-json)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
Save and load Python data with JSON
|
||||
======
|
||||
The JSON format saves you from creating your own data formats, and is
|
||||
particularly easy to learn if you already know Python. Here's how to use
|
||||
it with Python.
|
||||
![Cloud and databsae incons][1]
|
||||
|
||||
[JSON][2] stands for JavaScript Object Notation. This format is a popular method of storing data in key-value arrangements so it can be parsed easily later. Don’t let the name fool you, though: You can use JSON in Python—not just JavaScript—as an easy way to store data, and this article demonstrates how to get started.
|
||||
|
||||
First, take a look at this simple JSON snippet:
|
||||
|
||||
|
||||
```
|
||||
{
|
||||
"name":"tux",
|
||||
"health":"23",
|
||||
"level":"4"
|
||||
}
|
||||
```
|
||||
|
||||
That's pure JSON and has not been altered for Python or any other language. Yet if you’re familiar with Python, you might notice that this example JSON code looks an awful lot like a Python dictionary. In fact, the two are very similar: If you are comfortable with Python lists and dictionaries, then JSON is a natural fit for you.
|
||||
|
||||
### Storing data in JSON format
|
||||
|
||||
You might consider using JSON if your application needs to store somewhat complex data. While you may have previously resorted to custom text configuration files or data formats, JSON offers you structured, recursive storage, and Python’s JSON module offers all of the parsing libraries necessary for getting this data in and out of your application. So, you don’t have to write parsing code yourself, and other programmers don’t have to decode a new data format when interacting with your application. For this reason, JSON is easy to use, and ubiquitous.
|
||||
|
||||
Here is some sample Python code using a dictionary within a dictionary:
|
||||
|
||||
|
||||
```
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
|
||||
# instantiate an empty dict
|
||||
team = {}
|
||||
|
||||
# add a team member
|
||||
team['tux'] = {'health': 23, 'level': 4}
|
||||
team['beastie'] = {'health': 13, 'level': 6}
|
||||
team['konqi'] = {'health': 18, 'level': 7}
|
||||
```
|
||||
|
||||
This code creates a Python dictionary called **team**. It’s empty initially (you can create one that's already populated, but that’s impossible if you don’t have the data to put into the dictionary yet).
|
||||
|
||||
To add to the **dict** object, you create a key, such as **tux**, **beastie**, or **konqi** in the example code, and then provide a value. In this case, the value is _another_ dictionary full of player statistics.
|
||||
|
||||
Dictionaries are mutable. You can add, remove, and update the data they contain as often as you please. This format is ideal storage for data that your application frequently uses.
|
||||
|
||||
### Saving data in JSON format
|
||||
|
||||
If the data you’re storing in your dictionary is user data that needs to persist after the application quits, then you must write the data to a file on disk. This is where the JSON Python module comes in:
|
||||
|
||||
|
||||
```
|
||||
with open('mydata.json', 'w') as f:
|
||||
json.dump(team, f)
|
||||
```
|
||||
|
||||
This code block creates a file called **mydata.json** and opens it in write mode. The file is represented with the variable **f** (a completely arbitrary designation; you can use whatever variable name you like, such as **file**, **FILE**, **output**, or practically anything). Meanwhile, the JSON module’s **dump** function is used to dump the data from the **dict** into the data file.
|
||||
|
||||
Saving data from your application is as simple as that, and the best part about this is that the data is structured and predictable. To see, take a look at the resulting file:
|
||||
|
||||
|
||||
```
|
||||
$ cat mydata.json
|
||||
{"tux": {"health": 23, "level": 4}, "beastie": {"health": 13, "level": 6}, "konqi": {"health": 18, "level": 7}}
|
||||
```
|
||||
|
||||
### Reading data from a JSON file
|
||||
|
||||
If you are saving data to JSON format, you probably want to read the data back into Python eventually. To do this, use the Python JSON module’s **json.load** function:
|
||||
|
||||
|
||||
```
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
|
||||
f = open('mydata.json')
|
||||
team = json.load(f)
|
||||
|
||||
print(team['tux'])
|
||||
print(team['tux']['health'])
|
||||
print(team['tux']['level'])
|
||||
|
||||
print(team['beastie'])
|
||||
print(team['beastie']['health'])
|
||||
print(team['beastie']['level'])
|
||||
|
||||
# when finished, close the file
|
||||
f.close()
|
||||
```
|
||||
|
||||
This function implements the inverse, more or less, of saving the file: an arbitrary variable (**f**) represents the data file, and then the JSON module’s **load** function dumps the data from the file into the arbitrary **team** variable.
|
||||
|
||||
The **print** statements in the code sample demonstrate how to use the data. It can be confusing to compound **dict** key upon **dict** key, but as long as you are familiar with your own dataset, or else can read the JSON source to get a mental map of it, the logic makes sense.
|
||||
|
||||
Of course, the **print** statements don’t have to be hard-coded. You could rewrite the sample application using a **for** loop:
|
||||
|
||||
|
||||
```
|
||||
for i in team.values():
|
||||
print(i)
|
||||
```
|
||||
|
||||
### Using JSON
|
||||
|
||||
As you can see, JSON integrates surprisingly well with Python, so it’s a great format when your data fits in with its model. JSON is flexible and simple to use, and learning one basically means you’re learning the other, so consider it for data storage the next time you’re working on a Python application.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/7/save-and-load-data-python-json
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bus_cloud_database.png?itok=lhhU42fg (Cloud and databsae incons)
|
||||
[2]: https://json.org
|
@ -1,75 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (heguangzhi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Become a lifelong learner and succeed at work)
|
||||
[#]: via: (https://opensource.com/open-organization/19/7/informal-learning-adaptability)
|
||||
[#]: author: (Colin Willis https://opensource.com/users/colinwillishttps://opensource.com/users/marcobravo)
|
||||
|
||||
|
||||
成为终身学习者,并在工作中取得成功
|
||||
======
|
||||
|
||||
在具有适应性文化的开放组织中,学习应该一直持续——不会总是在正式场合发生。我们真的明白它是如何工作的吗?
|
||||
|
||||
![Writing in a notebook][1]
|
||||
|
||||
持续学习是指人们为发展自己而进行的持续的、职业驱动的、有意识的学习过程。对于那些认为自己是持续学习者的人来说,学习从未停止——这些人从日常经历中看到学习机会。与同事进行辩论、反思反馈、在互联网上寻找问题的解决方案、尝试新事物或冒险都是一个人在工作中可以进行的非正式学习活动的例子。
|
||||
|
||||
|
||||
持续学习是开放组织中任何人的核心能力。毕竟,开放的组织是建立在同行相互思考、争论和行动的基础上的。在开放组织的模棱两可、话语驱动的世界中茁壮成长,每天都需要员工具备这些技能。
|
||||
|
||||
不幸的是,科学文献在传播我们在工作中学习的知识方面,帮助个人欣赏和发展自己的学习能力方面,做得很差。因此,在本文系列中,我将向您介绍非正式学习,并帮助您理解将学习视为一种技能如何帮助您在任何组织中茁壮成长,尤其是在开源组织中。
|
||||
|
||||
### 为什么这么正式?
|
||||
|
||||
迄今为止,对组织中学习的科学研究主要集中在正式学习而不是非正式学习的设计、实施和评估上。
|
||||
|
||||
投资于员工知识、技能和能力的发展是一个组织保持其相对于竞争对手优势的重要方式。组织通过创建或购买课程、在线课程、研讨会等来提供正式确定学习机会。意在指导个人学习与工作相关的内容,就像学校里的一节课。对于一个组织来说,提供一门课程是一种简单(如果昂贵的话)的方法,可以确保其员工的技能或知识保持最新。同样,教室环境是研究人员的天然实验室,使得基于培训的研究和工作不仅可能,而且强大。
|
||||
|
||||
最近的评估表明,70%到80%的工作相关知识不是在培训中学到的,而是非正式的在职学习中得到的。
|
||||
|
||||
当然,人们不需要训练来学习一些东西;通常,人们通过研究答案、与同事交谈、思考、实验或适应变化来学习。事实上,[最近的评估表明][2]70%到80%的与工作相关的知识不是在培训中学到的,而是在工作中非正式学到的。这并不是说正式培训无效;培训可能非常有效,但它是一种精确的干预方式。在工作的大部分方面正式培训一个人是不现实的,尤其是当这些工作变得更加复杂的时候。
|
||||
|
||||
因此,非正式学习,或者任何发生在结构化学习环境之外的学习,对工作场所来说是极其重要的。事实上,[最近的科学证据][3]表明,非正式学习比正式培训更能预测工作表现。
|
||||
|
||||
那么,为什么组织和科学界如此关注培训呢?
|
||||
|
||||
### 循环过程
|
||||
|
||||
除了我前面提到的原因,研究非正式学习可能非常困难。与正式学习不同,非正式学习发生在非结构化环境中,高度依赖于个人,很难或不可能观察并研究。
|
||||
|
||||
直到最近,大多数关于非正式学习的研究都集中在定义非正式学习的合格特征和确定非正式学习在理论上是如何与工作经验联系在一起的。研究人员描述了一个[动态的周期性过程][4],通过这个过程,个人可以在组织中非正式地学习。
|
||||
|
||||
与正式学习一样,非正式学习发生在非结构化环境中,高度依赖于个人,很难或不可能观察。
|
||||
|
||||
在这个过程中,个人和组织都有创造学习机会的机构。例如,一个人可能对学习某样东西感兴趣,并为此表现出学习行为。组织以反馈的形式传递给个人,可能表明需要学习。这可能是一个糟糕的绩效评估,一个在项目中发表的评论,或者一个不是个人指导的组织环境的更广泛的变化。这些力量在组织环境中(例如,有人尝试了一个新想法,他或她的同事认识到并奖励了这种行为)或者通过在个人的头脑中反思(例如,有人反思了关于他或她的表现的,并决定在学习工作中付出更多的努力)。与培训不同,非正式学习不遵循正式的线性过程。一个人可以在任何时候体验过程的任何部分,同时体验过程的多个部分。
|
||||
|
||||
### 开放组织中的非正式学习
|
||||
|
||||
具体而言,在开放组织中,对等级制度的重视程度降低,对参与式文化的重视程度提高,这两者都推动了这种非正式的学习。简而言之,开放的组织只是为个人和组织环境提供了更多互动和激发学习的机会。此外,想法和变革需要开放组织中员工更广泛的认同——而认同需要对他人的适应性和洞察力的欣赏。
|
||||
|
||||
也就是说,仅仅增加学习机会并不能保证学习会发生或成功。有人甚至可能会说,开放组织中常见的模糊性和开放性话语可能会阻止不擅长持续学习的人——再一次,随着时间的推移学习的习惯和开放组织的核心能力——尽可能有效地为组织做出贡献。
|
||||
|
||||
解决这些问题需要一种以一致的方式跟踪非正式学习。最近,科学界呼吁创造衡量非正式学习的方法,这样就可以进行系统的研究来解决非正式学习的前因后果的问题。我自己的研究集中在这一呼吁上,我花了几年时间发展和完善我们对非正式学习行为的理解,以便对它们进行测量。
|
||||
|
||||
在本文系列的第二部分,我将重点介绍我最近在一个开放组织中进行的一项研究的成果,在该研究中,我测试了我对非正式学习行为的研究,并将它们与更广泛的工作环境和个人工作成果联系起来。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/open-organization/19/7/informal-learning-adaptability
|
||||
|
||||
作者:[Colin Willis][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/heguangzhi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/colinwillishttps://opensource.com/users/marcobravo
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/notebook-writing-pen.jpg?itok=uA3dCfu_ (Writing in a notebook)
|
||||
[2]: https://www.groupoe.com/images/Accelerating_On-the-Job-Learning_-_White_Paper.pdf
|
||||
[3]: https://www.researchgate.net/publication/316490244_Antecedents_and_Outcomes_of_Informal_Learning_Behaviors_a_Meta-Analysis
|
||||
[4]: https://psycnet.apa.org/record/2008-13469-009
|
134
translated/tech/20190716 Save and load Python data with JSON.md
Normal file
134
translated/tech/20190716 Save and load Python data with JSON.md
Normal file
@ -0,0 +1,134 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (HankChow)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Save and load Python data with JSON)
|
||||
[#]: via: (https://opensource.com/article/19/7/save-and-load-data-python-json)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
|
||||
使用 Python 处理 JSON 格式数据
|
||||
======
|
||||
如果你不希望从头开始创造一种数据格式来存放数据,JSON 是一个很好的选择。如果你对 Python 有所了解,就更加事半功倍了。下面就来介绍一下如何使用 Python 处理 JSON 数据。
|
||||
![Cloud and databsae incons][1]
|
||||
|
||||
[JSON][2] 的全称是 JavaScript <ruby>对象表示法<rt>Object Notation</rt></ruby>。这是一种以键值对的形式存储数据的格式,并且很容易解析,因而成为了一种被广泛使用的数据格式。另外,不要因为 JSON 名称而望文生义,JSON 并不仅仅在 JavaScript 中使用,它也可以在其它语言中使用。下文会介绍它是如何在 Python 中使用的。
|
||||
|
||||
首先我们给出一个 JSON 示例:
|
||||
|
||||
|
||||
```
|
||||
{
|
||||
"name":"tux",
|
||||
"health":"23",
|
||||
"level":"4"
|
||||
}
|
||||
```
|
||||
|
||||
上面是一个和编程语言无关的原生 JSON 数据。熟悉 Python 的人会看出来这个 JSON 数据跟 Python 中的<ruby>字典<rt>dictionary</rt></ruby>长得很像。而这两者之间确实非常相似,如果你对 Python 中的列表和字典数据结构有一定的理解,那么 JSON 理解起来也不难。
|
||||
|
||||
### 使用字典存放数据
|
||||
|
||||
如果你的应用需要存储一些结构复杂的数据,不妨考虑使用 JSON 格式。对比你可能曾经用过的自定义格式的文本配置文件,JSON 提供了更加结构化的可递归的存储格式。同时,Python 自带的 `json` 模块已经提供了可以将 JSON 数据导入/导出应用时所需的所有解析库。因此,你不需要针对 JSON 自行编写代码用于解析,而其他开发人员在与你的应用进行数据交互的时候也不需要去解析新的数据格式。正是这个原因,JSON 在数据交换时被广泛地采用了。
|
||||
|
||||
以下是一段在 Python 中使用嵌套字典的代码:
|
||||
|
||||
|
||||
```
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
|
||||
# instantiate an empty dict
|
||||
team = {}
|
||||
|
||||
# add a team member
|
||||
team['tux'] = {'health': 23, 'level': 4}
|
||||
team['beastie'] = {'health': 13, 'level': 6}
|
||||
team['konqi'] = {'health': 18, 'level': 7}
|
||||
```
|
||||
|
||||
这段代码声明了一个名为 `team` 的字典,并初始化为一个空字典。
|
||||
|
||||
如果要给这个字典添加内容,首先需要创建一个键,例如上面示例中的 `tux`、`beastie`、`konqi`,然后为这些键一一提供对应的值。上面示例中的值由一个个包含游戏玩家信息的字典充当。
|
||||
|
||||
字典是一种可变的变量。字典中的数据可以随时添加、删除或更新。这样的特性使得字典成为了应用程序存储数据的极好选择。
|
||||
|
||||
### 使用 JSON 格式存储数据
|
||||
|
||||
如果存放在字典中的数据需要被持久性存储,这些数据就需要写到文件当中。这个时候就需要用到 Python 中的 `json` 模块了:
|
||||
|
||||
|
||||
```
|
||||
with open('mydata.json', 'w') as f:
|
||||
json.dump(team, f)
|
||||
```
|
||||
|
||||
上面的代码首先创建了一个名为 `mydata.json` 的文件,然后以写模式打开了这个文件,这个被打开的文件以变量 `f` 表示(当然也可以用任何你喜欢的名称,例如 `file`、`output` 等)。而 `json` 模块中的 `dump()` 方法则是用于将一个字典输出到一个文件中。
|
||||
|
||||
从应用中导出数据就是这么简单,同时这些导出的数据是结构化的、可理解的。现在可以查看导出的数据:
|
||||
|
||||
|
||||
```
|
||||
$ cat mydata.json
|
||||
{"tux": {"health": 23, "level": 4}, "beastie": {"health": 13, "level": 6}, "konqi": {"health": 18, "level": 7}}
|
||||
```
|
||||
|
||||
### 从 JSON 文件中读取数据
|
||||
|
||||
如果已经将数据以 JSON 格式导出到文件中了,也有可能需要将这些数据读回到应用中去。这个时候,可以使用 Python `json` 模块中的 `load()` 方法:
|
||||
|
||||
|
||||
```
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import json
|
||||
|
||||
f = open('mydata.json')
|
||||
team = json.load(f)
|
||||
|
||||
print(team['tux'])
|
||||
print(team['tux']['health'])
|
||||
print(team['tux']['level'])
|
||||
|
||||
print(team['beastie'])
|
||||
print(team['beastie']['health'])
|
||||
print(team['beastie']['level'])
|
||||
|
||||
# when finished, close the file
|
||||
f.close()
|
||||
```
|
||||
|
||||
这个方法实现了和保存文件大致相反的操作。使用一个变量 `f` 来表示打开了的文件,然后使用 `json` 模块中的 `load()` 方法读取文件中的数据并存放到 `team` 变量中。
|
||||
|
||||
其中的 `print()` 展示了如何查看读取到的数据。在过于复杂的字典中迭代调用字典键的时候有可能会稍微转不过弯来,但只要熟悉整个数据的结构,就可以慢慢摸索出其中的逻辑。makes sense.
|
||||
|
||||
当然,这里使用 `print()` 的方式太不灵活了。你可以将其改写成使用 for 循环的形式:
|
||||
|
||||
|
||||
```
|
||||
for i in team.values():
|
||||
print(i)
|
||||
```
|
||||
|
||||
### 使用 JSON
|
||||
|
||||
如上所述,在 Python 中可以很轻松地处理 JSON 数据。因此只要你的数据符合 JSON 的模式,就可以选择使用 JSON。JSON 非常灵活易用,下次使用 Python 的时候不妨尝试一下。
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/7/save-and-load-data-python-json
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[HankChow](https://github.com/HankChow)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bus_cloud_database.png?itok=lhhU42fg (Cloud and databsae incons)
|
||||
[2]: https://json.org
|
Loading…
Reference in New Issue
Block a user