update
This commit is contained in:
		
							
								
								
									
										14
									
								
								2025.08.26_data_file_format/TXT_example.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								2025.08.26_data_file_format/TXT_example.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,14 @@ | ||||
| # 写入 | ||||
| with open('data.txt', 'w', encoding='utf-8') as f: | ||||
|     f.write('test_1\ntest_2') | ||||
|  | ||||
| # 读取 | ||||
| with open('data.txt', 'r', encoding='utf-8') as f: | ||||
|     content = f.read() # 读取全部内容 | ||||
| print(content) | ||||
|  | ||||
| with open('data.txt', 'r', encoding='utf-8') as f:  | ||||
|     lines = f.readlines() # 或按行读取 | ||||
| print(lines) | ||||
| for line in lines: | ||||
|     print(line.strip()) # strip() 去除首尾空白字符 | ||||
		Reference in New Issue
	
	Block a user