正文
Python支持多种文件和数据格式的读写,如文本、二进制、JSON、CSV等。 示例: ```python # 文本文件 with open('test.txt', 'w', encoding='utf-8') as f: f.write('Hello, world!') # 读取 with open('test.txt', 'r', encoding='utf-8') as f: print(f.read()) # JSON import json data = {'a': 1, 'b': 2} with open('data.json', 'w') as f: json.dump(data, f) ```