网站首页 > 技术文章 正文
- 读写txt文件
使用Python自带的文件读写库,可以轻松地读写txt文件。例如,可以使用open函数打开txt文件并读取其中的内容,也可以使用write函数将文本写入到txt文件中。以下是一个读取和写入txt文件的示例代码:
# 打开文件并读取其中的内容
with open('example.txt', 'r') as f:
content = f.read()
print(content)
# 将文本写入到文件中
with open('example.txt', 'w') as f:
f.write('Hello, world!')
- 读写csv文件
使用Python自带的csv库,可以轻松地读写csv文件。csv文件是一种常见的数据格式,其中数据按行和列组织,每行数据使用逗号分隔。以下是一个读取和写入csv文件的示例代码:
import csv
# 读取csv文件
with open('example.csv', 'r') as f:
reader = csv.reader(f)
for row in reader:
print(row)
# 将数据写入到csv文件中
data = [
['Name', 'Age', 'Gender'],
['Alice', 20, 'Female'],
['Bob', 25, 'Male'],
['Charlie', 30, 'Male']
]
with open('example.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(data)
- 读写JSON文件
使用Python自带的json库,可以轻松地读写JSON文件。JSON文件是一种常见的数据格式,它使用键值对组织数据。以下是一个读取和写入JSON文件的示例代码:
import json
# 读取JSON文件
with open('example.json', 'r') as f:
data = json.load(f)
print(data)
# 将数据写入到JSON文件中
data = {
'Name': 'Alice',
'Age': 20,
'Gender': 'Female'
}
with open('example.json', 'w') as f:
json.dump(data, f)
- 读写XML文件
使用Python自带的xml.etree.ElementTree库,可以轻松地读写XML文件。XML文件是一种常见的数据格式,它使用标签和属性组织数据。以下是一个读取和写入XML文件的示例代码:
import xml.etree.ElementTree as ET
# 读取XML文件
tree = ET.parse('example.xml')
root = tree.getroot()
for child in root:
print(child.tag, child.attrib, child.text)
# 将数据写入到XML文件中
root = ET.Element('data')
name = ET.SubElement(root, 'name')
name.text = 'Alice'
age = ET.SubElement(root, 'age')
age.text = '20'
tree = ET.ElementTree(root)
tree.write('example.xml')
- 读写Excel文件
使用Python自带的openpyxl库,可以轻松地读写Excel文件。Excel文件是一种常见的电子表格文件格式,其中数据按行和列组织,每个单元格都可以包含文本、数字或公式。以下是一个读取和写入Excel文件的示例代码:
import openpyxl
# 读取Excel文件
workbook = openpyxl.load_workbook('example.xlsx')
sheet = workbook.active
for row in sheet.iter_rows(min_row=1, max_col=3, max_row=4, values_only=True):
print(row)
# 将数据写入到Excel文件中
data = [
['Name', 'Age', 'Gender'],
['Alice', 20, 'Female'],
['Bob', 25, 'Male'],
['Charlie', 30, 'Male']
]
workbook = openpyxl.Workbook()
sheet = workbook.active
for row in data:
sheet.append(row)
workbook.save('example.xlsx')
以上是一些常见文件格式的读写操作示例,Python自带的文件读写库的应用非常灵活,可以通过简单的代码实现各种文件格式的读写。当然,对于不同的文件格式,也可以使用第三方库来进行读写操作,这些库通常具有更丰富的功能和更高的性能。
猜你喜欢
- 2024-12-20 Python学习 -- 常用数据交换格式(CSV、XML、JSON)
- 2024-12-20 Python 3 实现在线xml sitemap索引文件提取URL到指定文件
- 2024-12-20 Python操作XML和HTML,LXML类库的使用
- 2024-12-20 Python 文件读写(txt、json、xml、ini)
- 2024-12-20 Python实现XML快速转换json
- 2024-12-20 BeautifulSoup,一个解析HTML与XML文档无敌的 Python 库!
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)