
5、结构化数据分析
{'姓名': '张三', '语文': '98', '数学': '80', '英语': '87'},{'姓名': '李四', '语文': '65', '数学': '92', '英语': '78'},{'姓名': '王五', '语文': '83', '数学': '78', '英语': '93'},{'姓名': '李四', '语文': '65', '数学': '92', '英语': '78'},{'姓名
·
# 打开文件 fo = open("test.txt", "w+") print("文件名: ", fo.name) str = '''www.sugonedu.com01 www.sugonedu.com02 www.sugonedu.com03 www.sugonedu.com04 www.sugonedu.com05 ''' line = fo.write(str) # 关闭文件 fo.close() fo = open("test.txt", "r+") line = fo.read() print("读取的字符串: %s" % (line)) # 关闭文件 fo.close() fo = open("test.txt", "r+") line = fo.read(10) print("读取的字符串: %s" % (line)) # 关闭文件 fo.close() fo = open("test.txt", "r+") line = fo.readline() print("读取第一行 %s" % (line)) line = fo.readline() print("读取第二行 %s" % (line)) # 关闭文件 fo.close() fo = open("test.txt", "r") # 依次读取每行 for line in fo.readlines(): # 去掉每行头尾空白 line = line.strip() print("读取的数据为: %s" % (line)) # 关闭文件 fo.close() fo = open("test.txt", "r+") str = "www.sugonedu.com06\n" # 在文件末尾写入一行 fo.seek(0, 2) line = fo.write(str) # 关闭文件 fo.close() # 打开文件 fo = open("test.txt", "r+") print("文件名为: ", fo.name) # 换行需要制定换行符\n strs = ["www.sugonedu.com07\n", "www.sugonedu.com08\n"] # 在文件末尾写入 fo.seek(0, 2) fo.writelines(strs) # 关闭文件 fo.close() # 导入os模块 import os # 输出当前目录 print('当前目录为%s' % os.getcwd()) print('test.txt的绝对路径为%s' % os.path.abspath('test.txt')) print('/home/ubuntu和test.txt路径拼接结果为%s' % os.path.join('/home/ubuntu', 'test.txt')) # 判断存在的路径 print('/home/ubuntu路径是否存在=%s' % os.path.exists('/home/ubuntu')) # 判断不存在的路径 print('/home/hello路径是否存在=%s' % os.path.exists('/home/hello')) # 判断存在的文件 print('/home/ubuntu/test.txt文件是否存在=%s' % os.path.exists('/home/ubuntu/test.txt')) # 判断不存在的文件 print('/home/ubuntu/aaa.txt文件是否存在=%s' % os.path.exists('/home/ubuntu/aaa.txt')) # 创建的目录 path = "/home/ubuntu/testdir" os.mkdir(path, 0o755) print("目录已创建") # 创建的目录 path = "/home/ubuntu/monthly/daily" os.makedirs(path, 0o777) print("路径被创建") # 显示文件"test.txt"信息 statinfo = os.stat('test.txt') print(statinfo) # 重命名目录 os.renames("/home/ubuntu/testdir", "/home/ubuntu/newdir") # 重命名文件 os.renames("test.txt", "newdir/aanew.txt") os.rmdir("/home/ubuntu/monthly/daily") os.remove("newdir/aanew.txt") for root, dirs, files in os.walk("/home/ubuntu", topdown=False): for name in files: print(os.path.join(root, name)) for name in dirs: print(os.path.join(root, name)) import csv header = ['姓名', '语文', '数学', '英语'] data = [['张三', '98', '80', '87'], ['李四', '65', '92', '78'], ['王五', '83', '78', '93'], ['赵六', '54', '66', '43'], ['刘七', '72', '75', '69']] with open('scores.csv', 'w', encoding='utf-8', newline='') as f: writer = csv.writer(f) writer.writerow(header) writer.writerows(data) import csv header = ['姓名', '语文', '数学', '英语'] data = [{'姓名': '张飞', '语文': '43', '数学': '53', '英语': '49'}, {'姓名': '赵云', '语文': '95', '数学': '100', '英语': '98'}] with open('scores2.csv', 'w', encoding='utf-8', newline='') as f: writer = csv.DictWriter(f, header) writer.writeheader() writer.writerows(data) import csv with open('scores.csv', encoding='utf-8') as f: reader = csv.reader(f) header = next(reader) print(header) for row in reader: print(row) import csv with open('scores.csv', encoding='utf-8') as f: reader = csv.DictReader(f) for row in reader: if row['姓名'] == '张三': print('姓名=%s, 语文成绩=%s' % (row['姓名'], row['语文'])) import json data = [ {'姓名': '张三', '语文': '98', '数学': '80', '英语': '87'}, {'姓名': '李四', '语文': '65', '数学': '92', '英语': '78'}, {'姓名': '王五', '语文': '83', '数学': '78', '英语': '93'}, {'姓名': '赵六', '语文': '54', '数学': '66', '英语': '43'}, {'姓名': '刘七', '语文': '72', '数学': '75', '英语': '69'}] with open("scores.json", "w", encoding='utf-8') as f: # indent 超级好用,格式化保存字典,默认为None,小于0为零个空格 json.dump(data, f, indent=4, ensure_ascii=False) import json with open("scores.json", "r", encoding='utf-8') as f: datas = json.load(f) for data in datas: if data['姓名'] == '张三': print(f'姓名:{data["姓名"]},语文成绩:{data["语文"]}') import pickle data = [ {'姓名': '张三', '语文': '98', '数学': '80', '英语': '87'}, {'姓名': '李四', '语文': '65', '数学': '92', '英语': '78'}, {'姓名': '王五', '语文': '83', '数学': '78', '英语': '93'}, {'姓名': '赵六', '语文': '54', '数学': '66', '英语': '43'}, {'姓名': '刘七', '语文': '72', '数学': '75', '英语': '69'}] with open("scores.dmp", "wb") as f: # indent 超级好用,格式化保存字典,默认为None,小于0为零个空格 pickle.dump(data, f) import pickle with open("scores.dmp", "rb") as f: datas = pickle.load(f) for data in datas: if data['姓名'] == '张三': print(f'姓名:{data["姓名"]},语文成绩:{data["语文"]}')
更多推荐
所有评论(0)