python学习之路---文件和异常


一、从文件中读取数据

open()中需要传入路径和编码方式

file_path = '../Tkinter/dbBase.py'
with open(file_path,encoding='utf-8') as file_object:
    content = file_object.read()
print(content)

逐行读取

file_path = '../Tkinter/dbBase.py'
with open(file_path,encoding='utf-8') as file_object:
    for line in file_object:
        print(line.rstrip())

创建一个包含文件各行内容的列表

file_path = '../Tkinter/dbBase.py'
with open(file_path,encoding='utf-8') as file_object:
    lines = file_object.readlines()

for line in lines:
        print(line.rstrip())

二、写入文件

1.写入空文件

file_path = 'D:\Develop  software\PyCharm\data.txt'
with open(file_path,'a') as file_object:
    file_object.write("i am handsome")

三、异常

四、存储数据

使用json.dump()和json.load()

import json
numbers = [2,3,5,7,11,13]

filename = './number.json'

with open(filename,'w') as f:
    json.dump(numbe
# 读取json文件
import json
filename = 'number.json'
with open(filename) as f:
    numbers = json.load(f)

print(numbers)

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值