txt,csv,json互相转化
也没啥,记下来怕忘了.说明都在代码里面:
麻蛋,这个着色好难看
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | import csv import json #从txt变为csv student_txt=[]; with open( "student.txt" ,mode= 'r' ,encoding= 'utf-8' ) as student_txt_file_name: for i in student_txt_file_name.readlines(): student_txt.append(i.strip( '\n' ).split( " " )); #去掉换行符,转为list,使用空格分开(因为txt里面是以空格隔开) with open( "student.csv" ,mode= 'w' ,encoding= 'ansi' ,newline= '' ) as student_csv_file_name: #newline= '' :作用是防止空行产生;encoding= 'ansi' 创建一个以ASCII编码的csv文件,用utf-8的话我的Excel不认,乱的 write=csv.writer(student_csv_file_name); #创建一个编辑对象 for i in student_txt: write.writerow(i); #把每一个列表(子列表)作为行写入 #我没有主动关闭这两个文件的原因我不说,反正我知道,我自己忘了就让我自己想去. #从csv变为txt student_csv=[]; student_txt=[]; with open( "student.csv" ,mode= 'r' ,encoding= 'ansi' ) as student_csv_file_name:#编码读写一致 read_object=csv.reader(student_csv_file_name); with open( "student1.txt" ,mode= 'w' ,encoding= 'ansi' ) as student_txt_file_name: for i in read_object: j= ' ' . join (i)+ '\n' ; #这种奇怪的转化方式亮瞎我的眼.还一闪一闪的像迪厅!, student_txt_file_name.writelines(j); #不一定非得是列表,字典也可以 #txt转json student_json=[]; student_txt=[]; with open( 'student.txt' ,mode= 'r' ,encoding= 'utf-8' ) as student_txt_file_name: with open( "student.json" ,mode= 'w' ,encoding= 'ansi' ) as student_json_file_name: for i in student_txt_file_name.readlines(): student_txt.append(i.strip( '\n' ).split( ' ' )); key=student_txt[0];#作为键 for i in range(1,len(student_txt)): student_json_temp=[]; for j in zip(key,student_txt[i]): #zip接受多个可迭代对象作为参数,然后将这些对象中的对应位置的元素组成一个个的元组:zip([1,2,3],[4,5,6])返回[(1,4),(2,5),(3,6)] k= ":" . join (j); #这个的作用就是把(1,4)变成 "1:4" student_json_temp.append(k); student_json.append(student_json_temp); json.dump(student_json,student_json_file_name);#这种写方式让我有些郁闷,总觉得像创建对象似的 #json转txt student_json=[]; student_txt=[]; with open( 'student_json转txt.txt' ,mode= 'w' ,encoding= 'ansi' ) as student_txt_file_name: with open( "student.json" ,mode= 'r' ,encoding= 'ansi' ) as student_json_file_name: read_object=json.load(student_json_file_name); for i in read_object: head_list=[]; body_list=[]; for j in i: k=j.split( ':' ); if len(student_json)==0: head_list.append(k[0]); body_list.append(k[1]); if len(student_json)==0: student_txt_file_name.write( ' ' . join (head_list)+ '\n' ); student_json.append(student_json); #用了一次就没用了 student_txt_file_name.write( ' ' . join (body_list)+ '\n' ); #csv转json student_csv=[]; student_json=[]; with open( "student.csv" ,mode= 'r' ,encoding= 'ansi' ) as student_csv_file_name: read_object=csv.reader(student_csv_file_name); #用csv模块自带的函数来完成读写操作 with open( "student_csv转json.json" ,mode= 'w' ,encoding= 'ansi' ) as student_json_file_name: for i in read_object: student_csv.append(i); key=student_csv[0]; for i in range(1,len(student_csv)): student_json_temp=[]; for j in zip(key,student_csv[i]): k= ":" . join (j); student_json_temp.append(k); student_json.append(student_json_temp); json.dump(student_json,student_json_file_name); #json转csv student_csv=[]; student_json=[]; with open( "student.json" ,mode= 'r' ,encoding= 'ansi' ) as student_json_file_name: with open( "student_json转csv.csv" ,mode= 'w' ,encoding= 'ansi' ,newline= '' ) as student_csv_file_name: read_object=json.load(student_json_file_name); write=csv.writer(student_csv_file_name); for i in read_object: #读出来是列表 ledlist=[]; templist=[]; for a in i: j=a.split( ':' ); ledlist.append(j[0]); templist.append(j[1]); if len(student_csv)==0: student_csv.append(ledlist); student_csv.append(templist); for i in student_csv: write.writerow(i); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?