> For the complete documentation index, see [llms.txt](https://m0uk4.gitbook.io/notebooks/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://m0uk4.gitbook.io/notebooks/honey/python-notes/json-wen-jian-du-xie.md).

# json文件读写

#### 读

```python
with open("C:\\Users\\daiyifan\\pclady_wiki\\pclady_wiki\\answers1.json",
        "r",
        encoding="utf-8") as js1:
    url1=json.load(js1)
```

#### 写

```python
with open("C:\\Users\\daiyifan\\pclady_wiki\\pclady_wiki\\answers.json","w",encoding="utf-8") as js3:
    json.dump(url_sum,js3)#只能写入字典
    f.write('\n')#可以换行
```

#### replace

经过replace的字符串不改变本身的值，而作为值返回。

```python
str=str.repalce(old,new[,max])#max指最多匹配次数
```

#### 正则表达式

re模块正则表达式匹配的结果会返回为一个对象，可以通过`group`方法来查看该对象的值。`group(0)`为全部匹配的值，`group(1)`为第一次匹配（）中的值如：

<div align="left"><img src="/files/-LTrUNmdBGcOTCzZzzzV" alt=""></div>

<div align="left"><img src="/files/-LTrUQgNGQPvU0mO97_g" alt=""></div>

&#x20;re模块的`sub`的替换结果可以用函数表示

详见[re.sub使用替换函数](/notebooks/mouka/python/python-tricks/re_sub_function.md)

<div align="left"><img src="/files/-LTrUwaqQzUJzF-ISYKn" alt=""></div>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://m0uk4.gitbook.io/notebooks/honey/python-notes/json-wen-jian-du-xie.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
