《大数据和人工智能交流》头条号向广大初学者新增C 、Java 、Python 、Scala、javascript 等目前流行的计算机、大数据编程语言,希望大家以后关注本头条号更多的内容。
Python文件读写操作详细整理(一)
open函数参数说明:
file: 文件的路径
mode: 读写模式
encoding: 编码格式
(1)读写字符
r --- 从开始就是读
w ----从开始写
a ----从末尾写
(2)读写字节
rb----从开始就是读
wb----从开始写
ab----从末尾写
(3)w+
打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,
即原有内容会被删除。如果该文件不存在,创建新文件
(4)wb+
以二进制格式打开一个文件用于读写。如果该文件已存在则打开文件,并从开头开始编辑,
即原有内容会被删除。如果该文件不存在,创建新文件。一般用于非文本文件如图片等
(5)a+
打开一个文件用于读写。如果该文件已存在,文件指针将会放在文件的结尾。
文件打开时会是追加模式。如果该文件不存在,创建新文件用于读写
(6)ab+
以二进制格式打开一个文件用于追加。如果该文件已存在,文件指针将会放在文件的结尾。
如果该文件不存在,创建新文件用于读写
示例-1:
#读取文件,获取文件流对象
file = open(file='d:/test/hello.txt',mode='r',encoding='utf-8')
#读取文件所有内容
content = file.read()
print(content)
#关闭文件
file.close()
示例-2:使用with open
with open(file='d:/test/hello.txt',mode='r',encoding='utf-8') as file:
content = file.read()
print(content)
#关闭文件
file.closed
示例-3:读取文件的每一行
with open(file='d:/test/hello.txt',mode='r',encoding='utf-8') as file:
line = file.readline()
print(line)
line = file.readline()
print(line)
line = file.readline()
示例-4:读取文件内容,返回值为List
with open(file='d:/test/hello.txt',mode='r',encoding='utf-8') as file:
#返回值为List
list = file.readlines()
print(list)
结果:['hello you', 'hello me']
示例-5:
file = open(file='d:/test/hello.txt',mode='w',encoding='utf-8')
#写并换行
file.write('hello everbody')
file.write('hello world')
file.close()
Python文件读写操作详细整理(二)
一、文件夹操作
示例-1:
#coding=utf-8
#导入os模块
import os
import shutil
# 创建文件夹
os.mkdir('d:/test/d1')
#递归创建文件夹
os.makedirs('d:/test/a/b/c/')
示例-2:
#导入os模块
import os
import shutil
# 获取当前的目录
print(os.getcwd())
# 改变默认目录
os.chdir('/home')
d1 = os.getcwd()
print(d1)
示例-3:
#导入os模块
import os
import shutil
# 删除文件夹
os.rmdir('d:/test/d1')
示例-4:
#导入os模块
import os
import shutil
# 列出信息
list = os.listdir('d:/test')
print(list)
二、文件操作
示例-1:
#coding=utf-8
#导入os模块
import os
import shutil
# 重命名
os.rename('d:/test/test1.py','d:/test/test2.py')
示例-2:
#导入os模块
import os
import shutil
# 分隔文件
file_path = 'd:/test/hello.py'
f1 = os.path.splitext(file_path)
print(f1)
f2 = os.path.split(file_path)
print(f2)
结果:
('d:/test/hello', '.py')
('d:/test', 'hello.py')
示例-3:
#coding=utf-8
#导入os模块
import os
import shutil
# 拼接路径
print(os.path.join('d:/test/','hello.py'))
结果:d:/test/hello.py
示例-4:
import os
import shutil
#判断
file_path = 'd:/test/hello.py'
flg1 = os.path.exists(file_path)
print(flg1)
flg2 = os.path.isfile(file_path)
print(flg2)
flg3 = os.path.isdir(file_path)
print(flg3)
Python文件读写操作详细整理(三)
示例-1:
from io import StringIO
#创建对象
f = StringIO()
f.write('hello')
#获取值
v = f.getvalue()
print(v)
#关闭
f.close()
结果:hello
示例-2:读取5个字符
#coding=utf-8
from io import StringIO
f = StringIO('hello you')
s1 = f.read(5)
结果:hello
示例-2:读取1行字符,注意有个换行
#coding=utf-8
from io import StringIO
f = StringIO('hello youhello me')
s2 = f.readline()
print(s2)
结果:hello you
示例-3:读取所有行字符,注意有个换行
#coding=utf-8
from io import StringIO
f = StringIO('hello youhello me')
s3 = f.readlines()
print(s3)
f.close()
结果:['hello you', ' hello me']
示例-4:
from io import BytesIO
f = BytesIO()
f.write('汉字'.encode('utf-8'))
v = f.getvalue()
print(v)
f.close()
结果:b'xe6xb1x89xe5xadx97'
示例-5:
#coding=utf-8
import pickle
list = [1,2,3]
#将List序列化为字节对象
obj = pickle.dumps(list)
print(obj)
#将字节转对象
new_list = pickle.loads(obj)
print(new_list)
结果:
b'x80x03]qx00(Kx01Kx02Kx03e.'
[1, 2, 3]
示例-6:
#coding=utf-8
import pickle
list = [1,2,3]
#将List序列化为字节对象
obj = pickle.dumps(list)
file = open('d:/test/hello.txt','wb')
#将对象写到文件中
file.write(obj)
file.close()
file = open('d:/test/hello.txt','rb')
content = file.read()
print(content)
new_obj = pickle.loads(content)
print(new_obj)
示例-7:使用with open
#coding=utf-8
import pickle
list = [1,2,3]
#将List对象序列化后写入文件
with open('d:/test/hello.txt','wb') as file:
pickle.dump(list,file)
#从文件中读取对象
with open('d:/test/hello.txt','rb') as file:
obj = pickle.load(file)
print(obj)
示例-8:
#coding=utf-8
import json
students={
"sid" : "s001",
"sname": "lily",
'age':21
}
#将字典对象编程json字符串对象
s1 = json.dumps(students)
print(s1)
#查看转换后的类型(字符串类型)
print(type(s1))
#将s1对象转化为字典对象
s2 = json.loads(s1)
print(s2)
#查看转换后的类型(字典类型)
print(type(s2))
结果:
{"sid": "s001", "sname": "lily", "age": 21}
{'sid': 's001', 'sname': 'lily', 'age': 21}
《大数据和人工智能交流》的宗旨
1、将大数据和人工智能的专业数学:概率数理统计、线性代数、决策论、优化论、博弈论等数学模型变得通俗易懂。
2、将大数据和人工智能的专业涉及到的数据结构和算法:分类、聚类 、回归算法、概率等算法变得通俗易懂。
3、最新的高科技动态:数据采集方面的智能传感器技术;医疗大数据智能决策分析;物联网智慧城市等等。
根据初学者需要会有C语言、Java语言、Python语言、Scala函数式等目前主流计算机语言。
根据读者的需要有和人工智能相关的计算机科学与技术、电子技术、芯片技术等基础学科通俗易懂的文章。