也没啥,记下来怕忘了.说明都在代码里面:

麻蛋,这个着色好难看

  1. import csv
  2. import json
  3.  
  4. #从txt变为csv
  5. student_txt=[];
  6. with open("student.txt",mode='r',encoding='utf-8')as student_txt_file_name:
  7. for i in student_txt_file_name.readlines():
  8. student_txt.append(i.strip('\n').split(" ")); #去掉换行符,转为list,使用空格分开(因为txt里面是以空格隔开)
  9. with open("student.csv",mode='w',encoding='ansi',newline='')as student_csv_file_name: #newline='':作用是防止空行产生;encoding='ansi'创建一个以ASCII编码的csv文件,用utf-8的话我的Excel不认,乱的
  10. write=csv.writer(student_csv_file_name); #创建一个编辑对象
  11. for i in student_txt:
  12. write.writerow(i); #把每一个列表(子列表)作为行写入
  13. #我没有主动关闭这两个文件的原因我不说,反正我知道,我自己忘了就让我自己想去.
  14.  
  15. #从csv变为txt
  16. student_csv=[];
  17. student_txt=[];
  18. with open("student.csv",mode='r',encoding='ansi')as student_csv_file_name:#编码读写一致
  19. read_object=csv.reader(student_csv_file_name);
  20. with open("student1.txt",mode='w',encoding='ansi')as student_txt_file_name:
  21. for i in read_object:
  22. j=' '.join(i)+'\n'; #这种奇怪的转化方式亮瞎我的眼.还一闪一闪的像迪厅!,
  23. student_txt_file_name.writelines(j);
  24. #不一定非得是列表,字典也可以
  25. #txt转json
  26. student_json=[];
  27. student_txt=[];
  28. with open('student.txt',mode='r',encoding='utf-8')as student_txt_file_name:
  29. with open("student.json",mode='w',encoding='ansi')as student_json_file_name:
  30. for i in student_txt_file_name.readlines():
  31. student_txt.append(i.strip('\n').split(' '));
  32. key=student_txt[0];#作为键
  33. for i in range(1,len(student_txt)):
  34. student_json_temp=[];
  35. for j in zip(key,student_txt[i]): #zip接受多个可迭代对象作为参数,然后将这些对象中的对应位置的元素组成一个个的元组:zip([1,2,3],[4,5,6])返回[(1,4),(2,5),(3,6)]
  36. k=":".join(j); #这个的作用就是把(1,4)变成"1:4"
  37. student_json_temp.append(k);
  38. student_json.append(student_json_temp);
  39. json.dump(student_json,student_json_file_name);#这种写方式让我有些郁闷,总觉得像创建对象似的
  40. #json转txt
  41. student_json=[];
  42. student_txt=[];
  43. with open('student_json转txt.txt',mode='w',encoding='ansi')as student_txt_file_name:
  44. with open("student.json",mode='r',encoding='ansi')as student_json_file_name:
  45. read_object=json.load(student_json_file_name);
  46. for i in read_object:
  47. head_list=[];
  48. body_list=[];
  49. for j in i:
  50. k=j.split(':');
  51. if len(student_json)==0:
  52. head_list.append(k[0]);
  53. body_list.append(k[1]);
  54. if len(student_json)==0:
  55. student_txt_file_name.write(' '.join(head_list)+'\n');
  56. student_json.append(student_json); #用了一次就没用了
  57. student_txt_file_name.write(' '.join(body_list)+'\n');
  58.  
  59. #csv转json
  60. student_csv=[];
  61. student_json=[];
  62. with open("student.csv",mode='r',encoding='ansi')as student_csv_file_name:
  63. read_object=csv.reader(student_csv_file_name); #用csv模块自带的函数来完成读写操作
  64. with open("student_csv转json.json",mode='w',encoding='ansi')as student_json_file_name:
  65. for i in read_object:
  66. student_csv.append(i);
  67. key=student_csv[0];
  68. for i in range(1,len(student_csv)):
  69. student_json_temp=[];
  70. for j in zip(key,student_csv[i]):
  71. k=":".join(j);
  72. student_json_temp.append(k);
  73. student_json.append(student_json_temp);
  74. json.dump(student_json,student_json_file_name);
  75.  
  76. #json转csv
  77. student_csv=[];
  78. student_json=[];
  79. with open("student.json",mode='r',encoding='ansi')as student_json_file_name:
  80. with open("student_json转csv.csv",mode='w',encoding='ansi',newline='')as student_csv_file_name:
  81. read_object=json.load(student_json_file_name);
  82. write=csv.writer(student_csv_file_name);
  83. for i in read_object: #读出来是列表
  84. ledlist=[];
  85. templist=[];
  86. for a in i:
  87. j=a.split(':');
  88. ledlist.append(j[0]);
  89. templist.append(j[1]);
  90. if len(student_csv)==0:
  91. student_csv.append(ledlist);
  92. student_csv.append(templist);
  93. for i in student_csv:
  94. write.writerow(i);

  

txt,csv,json互相转化的更多相关文章

  1. Python3爬虫(八) 数据存储之TXT、JSON、CSV

    Infi-chu: http://www.cnblogs.com/Infi-chu/ TXT文本存储 TXT文本存储,方便,简单,几乎适用于任何平台.但是不利于检索. 1.举例: 使用requests ...

  2. JS解析json数据并将json字符串转化为数组的实现方法

    json数据在ajax实现异步交互时起到了很重要的作用,他可以返回请求的数据,然后利用客户端的js进行解析,这一点体现出js的强大,本文介绍JS解析json数据并将json字符串转化为数组的实现方法, ...

  3. 将JSON对象转化为数组对象

    package web.helper; import java.util.ArrayList; import net.sf.json.JSONArray; import web.model.Abstr ...

  4. Python处理json字符串转化为字典

    有一个需求,需要用python把json字符串转化为字典 inp_str = " {'k1':123, 'k2': '345','k3','ares'} " import json ...

  5. Data access between different DBMS and other txt/csv data source by DB Query Analyzer

        1 About DB Query Analyzer DB Query Analyzer is presented by Master Genfeng,Ma from Chinese Mainl ...

  6. Data_r_and_w(csv,json,xlsx)

    import osimport sysimport argparsetry:    import cStringIO as StringIOexcept:    import StringIOimpo ...

  7. 爬虫3 requests之json 把json数据转化为字典

    #json 将json数据转化为字典,方便操作数据 res = requests.get('http://httpbin.org/get') print(res.json()) #res.json() ...

  8. JS解析json数据(如何将json字符串转化为数组)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  9. iOS JSON字符串转化为字典-字典转Json字符串-

    1. JSON字符串转化为字典 + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString = ...

随机推荐

  1. linux command 4

    #环境变量扩展 echo $PATH #算数表达式 echo $((3*5)) #参数扩展 echo ls *pdf #"" echo "$PATH $(cal)&quo ...

  2. 【转载】 强化学习(十一) Prioritized Replay DQN

    原文地址: https://www.cnblogs.com/pinard/p/9797695.html ------------------------------------------------ ...

  3. mybatis(一、原理,一对多,多对一查询)

    MyBatis框架及原理分析 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转换 ...

  4. InstallShield-常用prq文件的下载地址

    VC 2010 redist X86: http://saturn.installshield.com/is/prerequisites/microsoft visual c++ 2010 redis ...

  5. node.js 调用第三方服务

    node作为客户端调用第三方服务   nodejs.cn/api 1. let http = require('http'); let util = require("util") ...

  6. BIOS(Basic Input/Output System)是基本输入输出系统的简称

    BIOS(Basic Input/Output System)是基本输入输出系统的简称 介绍 操作系统老师说,平时面试学生或者毕业答辩的时候他都会问这个问题,可见这个问题对于计算机专业的学生来说是如此 ...

  7. 使用parted对大于2T的磁盘进行分区

    使用parted对磁盘进行分区 版本信息 版本 修改日期 修改人 修改内容 备注 V0.1 2018/09/06   初始化版本 讨论稿                                 ...

  8. 用jquery得到select选中的值

    <select class="pushtype" name="push_type" onchange="pushType(this)" ...

  9. MySQL 烂笔头 备份和还原

    备份 mysqldump -u root -p testdb > d:/backupfile.sql 还原 mysql -u root -p testdb2 <d:/backupfile. ...

  10. react-native 获取组件的宽度和高度

    react-native 获取组件的尺寸有两种方式,第一种方式使用元素自身的onLayout属性去获取,但是这种方式有一个局限性,就是只有在初次渲染的时候才会触发这个函数,而且此种方法获取的是组件相对 ...