9 文件操作
Posted 蟹黄小笼包
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了9 文件操作相关的知识,希望对你有一定的参考价值。
-
生成50个MAC地址并写入文件mac.txt中,MAC地址前6位(16进制)为4A-3F-56。
import random import string # 随机生成一个mac地址 def create_mac(): MAC = \'01-AF-3B\' hex_num = string.hexdigits for i in range(3): n = random.sample(hex_num, 2) sn = \'-\' + \'\'.join(n).upper() MAC += sn return MAC # print(create_mac()) # 随机生成50个MAC 地址 def main(): with open(\'mac.txt\', \'w\') as f: for i in range(50): mac = create_mac() print(mac) # 每生成一个MAC地址,存入文件 f.write(mac + \'\\n\') main() def count(s): alpha,num,space,other=0,0,0,0 for i in s: if i.isalpha(): alpha+=1 elif i.isdigit(): num+=1 elif i.isspace(): space+=1 else: other+=1 print(\'英文字符数,数字字符数,空格字符数,其他字符数\'.format(alpha,num,space,other)) count(input("请输入一个字符串:"))
输出结果:
"C:\\Program Files\\Python39\\python.exe" E:/课件/python/作业10/1随机生成MAC地址/MAC地址.py 01-AF-3B-8B-D2-90 01-AF-3B-6F-FE-C0 01-AF-3B-A3-CB-F8 01-AF-3B-D8-AD-28 01-AF-3B-B4-56-73 01-AF-3B-BA-39-CB 01-AF-3B-B2-12-BA 01-AF-3B-47-6B-BA 01-AF-3B-A4-C4-B5 01-AF-3B-F4-F3-07 01-AF-3B-AE-AF-F9 01-AF-3B-5C-6A-A9 01-AF-3B-CA-0B-D6 01-AF-3B-40-A9-02 01-AF-3B-B6-75-5A 01-AF-3B-AD-A4-B6 01-AF-3B-9C-31-DF 01-AF-3B-D8-3C-BC 01-AF-3B-CB-F2-2A 01-AF-3B-DE-21-1B 01-AF-3B-AB-C0-C1 01-AF-3B-2F-5A-DB 01-AF-3B-02-0A-FF 01-AF-3B-D7-AC-E2 01-AF-3B-A2-CA-8A 01-AF-3B-FE-8F-EA 01-AF-3B-7B-FB-B0 01-AF-3B-EF-CF-B2 01-AF-3B-CB-52-6F 01-AF-3B-F9-2F-FA 01-AF-3B-54-6A-C2 01-AF-3B-F4-FB-DB 01-AF-3B-CE-9E-0B 01-AF-3B-7D-D0-A6 01-AF-3B-53-CA-FD 01-AF-3B-7C-C1-B8 01-AF-3B-87-AB-6E 01-AF-3B-13-E6-5B 01-AF-3B-E7-AF-64 01-AF-3B-8F-8E-E3 01-AF-3B-10-FA-3E 01-AF-3B-59-B5-C3 01-AF-3B-BF-E1-CF 01-AF-3B-40-CD-59 01-AF-3B-AD-B4-75 01-AF-3B-FC-A8-7D 01-AF-3B-68-1C-AC 01-AF-3B-56-DE-4F 01-AF-3B-4E-9B-AB 01-AF-3B-FC-80-65
- 读取文本文件ABC中的内容,统计其频率最高的10个单词,将结果写入CSV文件中。
import re import csv def order_dict(dicts, n): result = [] result1 = [] p = sorted([(k, v) for k, v in dicts.items()], reverse=True) s = set() for i in p: s.add(i[1]) for i in sorted(s, reverse=True)[:n]: for j in p: if j[1] == i: result.append(j) for r in result: result1.append(r[0]) return result1 def order_dict1(dicts, n): # 截取排序结果想要的部分返回 list1 = sorted(dicts.items(), key=lambda x: x[1]) return list1[-1:-(n + 1):-1] # return list1[-2:-(n+2):-1] #去除统计结果为""的情况(前面步骤中,字典没有提前""去掉的情况下) if __name__ == "__main__": # 1 获取文本 f = open("ABC.txt", "r", encoding=\'UTF-8\') txt = f.read() txt = txt.lower() # 将所有字符转换为小写 f.close() # 2 划分单词 array = re.split(\'[ ,.\\n]\', txt) # print(\'分词结果\',array) # 3 词频统计 dic = for i in array: if i not in dic: dic[i] = 1 else: dic[i] += 1 # 4 除掉无价值的词 del [dic[\'\']] # 5 输出出现频率最高的10个单词 print(\'\\n\') print(order_dict1(dic, 10), ) # -*- coding: utf-8 -*- # 统计其频率最高的10个单词,将结果写入CSV文件中 with open("统计结果.csv", "a", newline=\'\') as f: writer = csv.writer(f) writer.writerow(["word", "amount"]) row = [(\'to\', 6), (\'or\', 4), (\'her\', 4), (\'your\', 4), (\'you\', 3), (\'at\', 3), (\'say\', 3), (\'in\', 3), (\'trouble\', 3), (\'try\', 2)] for r in row: writer.writerow(r)
输出结果:输出出现频率最高的10个单词 [(\'to\', 6), (\'or\', 4), (\'her\', 4), (\'your\', 4), (\'you\', 3), (\'at\', 3), (\'say\', 3), (\'in\', 3), (\'trouble\', 3), (\'try\', 2)]
ABC.txt 文本内容如下Parents often get angry because of their trouble in their lives. Let\'s say that your mother is not happy about her boss. If she doesn\'t have other ways of expressing her emotions, she might come home and yell at you, scream at your dad, kick at the dog, or even say something mean to you.www.xiao84.com Here\'s how to handle it when an adult in your life has trouble controlling his or her anger: Don\'t make it worse. Angry people can have trouble thinking clearly, so try not to do or say anything to make things worse. Wait till your parent cools off, then talk to him or her in a calm tone, and try to explain how the anger is affecting you.
#代码参考(Python实现文本词频统计——读取英文文本进行词频统计并输出)
python写入数据到csv或xlsx文件的3种方法
- 编码将文件JsonData文件中数据,按如下图所示格式的数据输出。
Data = [ "ID": 1, "Name": "张永华", "Sex": "男", "Score": 92.7 , "ID": 2, "Name": "Test", "Sex": "女", "Score": 91.3 , "ID": 3, "Name": "DingY", "Sex": "男", "Score": 95.6 , "ID": 4, "Name": "李佳", "Sex": "女", "Score": 98.8 , "ID": 5, "Name": "张仁德", "Sex": "男", "Score": 93.6 ] print(\'编号\', \'\\t\', \'姓名\', \'\\t\\t\', \'性别\', \'\\t\\t\', \'成绩\') print(\'-\' * 38) for data in Data: Id = data[\'ID\'] name = data[\'Name\'] sex = data[\'Sex\'] score = data[\'Score\'] print(Id, \'\\t\\t\', name, \'\\t\\t\', sex, \'\\t\\t\', score)
输出结果:
"C:\\Program Files\\Python39\\python.exe" "E:/课件/python/作业10/3json数据格式输出/3 JsonData.py" 编号 姓名 性别 成绩 -------------------------------------- 1 张永华 男 92.7 2 Test 女 91.3 3 DingY 男 95.6 4 李佳 女 98.8 5 张仁德 男 93.6 进程已结束,退出代码0
data.json文件内容如下
[ "ID": 1, "Name": "张永华", "Sex": "男", "Score": 92.7 , "ID": 2, "Name": "Test", "Sex": "女", "Score": 91.3 , "ID": 3, "Name": "DingY", "Sex": "男", "Score": 95.6 , "ID": 4, "Name": "李佳", "Sex": "女", "Score": 98.8 , "ID": 5, "Name": "张仁德", "Sex": "男", "Score": 93.6 ]
-
编码将文件student.json中数据导入到文件“student.xls”中。
import xlwt read = open(\'student.json\', \'r\', encoding=\'utf-8\') data_list = eval(read.read()) work = xlwt.Workbook() sheet = work.add_sheet(\'Sheet1\', cell_overwrite_ok=True) for data in data_list.items(): row = data[0] write_data = data[1] print(write_data) sheet.write(int(row)-1, 0, row) sheet.write(int(row)-1, 1, write_data[0]) sheet.write(int(row)-1, 2, write_data[1]) sheet.write(int(row)-1, 3, write_data[2]) sheet.write(int(row)-1, 4, write_data[2]) work.save(\'student.xls\')
输出结果:
"C:\\Program Files\\Python39\\python.exe" "E:/课件/python/作业10/4json数据转xls/student.json to xls.py" [\'张三\', 93, 97, 99] [\'李四\', 89, 96, 86] [\'王五\', 87, 99, 88]
"1": ["张三", 93, 97, 99], "2": ["李四", 89, 96, 86], "3": ["王五", 87, 99, 88]
以上是关于9 文件操作的主要内容,如果未能解决你的问题,请参考以下文章