栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录
  • 一、从文件中读取数据
      • 逐行读取
      • 创建一个包含文件各行内容的列表
  • 二、写入文件
    • 1.写入空文件
  • 三、异常
  • 四、存储数据
      • 使用json.dump()和json.load()
  • 总结


一、从文件中读取数据

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  softwarePyCharmdata.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提供了大量能使我们快速便捷地处理数据的函数和方法。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1030294.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号