1. 文件操作
1.計算機數(shù)據(jù)的存儲
計算機的存儲系統(tǒng)分為運行內(nèi)存和硬盤兩種
運行內(nèi)存:用來保存程序運行過程中產(chǎn)生的數(shù)據(jù)入偷,程序運行結(jié)束后會自動銷毀
硬盤 :用來保存文件掉蔬,保存在文件中的數(shù)據(jù)就是保存在硬盤中的,除非手動刪除煤裙,否則數(shù)據(jù)會一直在
2.數(shù)據(jù)持久化
數(shù)據(jù)持久化就是將數(shù)據(jù)以各種形式保存在硬盤中(保存到本地文件中)
3.文件操作
文件操作基本步驟:打開文件 -> 操作文件(讀楼镐、寫) -> 關(guān)閉文件
打開文件
open(file, mode='r', encoding=None) - 以指定的模式打開指定的文件,并且返回一個文件對象
說明:
file - 文件路徑锣笨,字符串類型
絕對路徑:文件或者文件夾的全路徑(一般不寫絕對路徑)
相對路徑:只寫文件絕對路徑的一部分,另一部分用特殊符號代替
特殊符號:./ -> 表示當(dāng)前路徑 (可以省略)
../ -> 表示當(dāng)前目錄的上層目錄
.../
mode - 打開方式道批,字符串類型
第一組:控制操作類型:
r - 以只讀的方式打開文件(默認值)
w - 以只寫的方式打開文件错英,打開前會先清空原文件的內(nèi)容
a - 以只寫的方式打開文件,不會清空
第二組:控制數(shù)據(jù)類型(文本數(shù)據(jù)-str/二進制數(shù)據(jù)-bytes):
t - 操作的數(shù)據(jù)是文本數(shù)據(jù)(默認值)
b - 操作的數(shù)據(jù)是二進制數(shù)據(jù)
注意:每一組只選一個屹徘,兩組值進行組合使用
encoding - 文本編碼方式走趋;直接寫'utf-8'
注意:如果打開方式中帶b,就不能設(shè)置encoding
close()
open('/Users/zhangzhishuai/Desktop/千峰/01語言基礎(chǔ)/day14-文件操作/files/text.txt')
open('./files/text.txt')
2.文件操作的讀寫操作
1.打開文件方式一:
文件對象 = open()
操作文件對象
文件對象.close
with open(文件路徑, 文件打開方式, encoding=文本編碼方式) as 文件對象:
操作文件對象
f = open('files/text.txt', encoding='utf-8')
print(f.read())
f.close()
with open('files/text.txt', encoding='utf8') as f:
print(f.read())
2.文件讀操作
1)文件對象.read() - 從文件讀寫位置開始,讀到文件的結(jié)尾(默認情況下讀寫位置在開頭)
2)文件對象.readline() - 讀文本文件的一行內(nèi)容噪伊,從當(dāng)前讀寫位置讀到一行的結(jié)束
3)文件對象.readline() - 一行一行的讀簿煌,讀完為止,返回的是一個列表鉴吹,列表元素是每一行的內(nèi)容
with open('text2.txt', 'r', encoding='utf8') as f:
print('第一次', f.read())
print('第二次', f.read())
with open('text2.txt', 'r', encoding='utf8') as f:
print('第一次', f.readline())
print('第er次', f.readline())
3.數(shù)據(jù)持久化
1.數(shù)據(jù)持久化的基本操作
a. 數(shù)據(jù)保存在文件中
b.需要數(shù)據(jù)時候從文件中取讀數(shù)據(jù)
c.當(dāng)數(shù)據(jù)發(fā)生改變的時候姨伟,對保存數(shù)據(jù)的文件進行更新
注意:如果以讀的方式打開一個不存在的文件,會報錯豆励,如果是以寫的方式打開一個不存在的文件夺荒,不會報錯,而且會自動新建這個文件
練習(xí):寫一個程序良蒸,統(tǒng)計這個程序啟動的次數(shù)
with open('text2.txt', encoding='utf8') as f:
count = int(f.read())
count += 1
print(count)
with open('text2.txt', 'w', encoding='utf8') as f:
f.write(str(count))
4.字典和列表的數(shù)據(jù)持久化
names = [{'name': '張三', 'age': '34', 'tel': 213},
{'name': '張三', 'age': '34', 'tel': 213},
{'name': '張三', 'age': '34', 'tel': 213}
]
#1.字典和列表的寫操作 先將列表或字典轉(zhuǎn)換成字符串
with open('text2.txtf', 'w', encoding='utf-8') as f:
f.write(str(names))
#2.字典和列表的讀操作
with open('text2.txt', 'r', encoding='utf-8') as f:
content = f.read()
print(content)
print(type(content))
new_content = eval(content)
print(new_content)
print(type(new_content))
for item in new_content:
print(item)
def add_students():
name = input('姓名:')
age = input('age:')
tel = input('tel:')
stu = {'name': name, 'age': age, 'tel': tel}
with open('text2.txt', 'r', encoding='utf-8') as f:
all_students = eval(f.read())
all_students.append(stu)
with open('text2.txt', 'w', encoding='utf-8') as f:
f.write(str(all_students))
add_students()
5.json數(shù)據(jù)
1.什么是json
import json
1)存在的意義
json就是不同編程語言之間進行數(shù)據(jù)交流的一種通用格式
2)概念
json是一種數(shù)據(jù)格式
a.一個json有且只有一個數(shù)據(jù)
b.這個數(shù)據(jù)是json支持的數(shù)據(jù)類型的數(shù)據(jù)
3)json支持的數(shù)據(jù)類型
數(shù)字類型 字符串 布爾 數(shù)組 字典(對象) 空值(null)
數(shù)字類型:所有的數(shù)字對應(yīng)的類型 支持科學(xué)計數(shù)法
字符串:只能是雙引號引起來技扼,支持轉(zhuǎn)義字符
布爾:true false 都要小寫
數(shù)組:相當(dāng)于Python的列表
字典:相當(dāng)于Python字典但是字典的key只能是字符串,{key1: value1, key2:value2...}
空值:null 相當(dāng)于Python中的none
2.json轉(zhuǎn)Python
json Python
數(shù)字類型 數(shù)字
字符串 字符串(可能會將雙引號變成單引號)
布爾 布爾(小寫變大寫)
數(shù)組 列表
字典 字典
空值(null) 空值(none)
json.loads(字符串) : 將json格式的字符串轉(zhuǎn)換成Python對應(yīng)的數(shù)據(jù)(這的字符串的內(nèi)容必須滿足json格式)
3.Python轉(zhuǎn)json
Python json
int/float 數(shù)字類型
字符串 字符串都變成雙引號
布爾 布爾(大寫變小寫)
列表/元組 數(shù)組
字典 字典
None null
json.dumps(數(shù)據(jù)) : 將指定的Python數(shù)據(jù)轉(zhuǎn)換成json格式的字符串
result = json.loads('"abc"')
print(result)
print(type(result))
result = json.dumps([1, 'qeq', True, None])
print(result)
print(type(result))