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

麻蛋,这个着色好难看

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);

  

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. C# 开发ModBus的服务器程序 实现ModBus数据总站 搭建自定义的Modbus服务器 同时支持tcp和rtu

    前言 本文将使用一个NuGet公开的组件技术来实现一个ModBus TCP的服务器端数据引擎,方便的实现接收来自各种设备的数据.并且该服务器模拟真实的设备,包含了数据池功能,可以接受来自任何支持Mod ...

  2. zabbix3.4安转

    其实很简单  在官网上有安装文档,我只不过是将官网上的拿了下来 安装apache和php 数据库mariadb(我的系统是centos7的所以安装mariadb) 开始zabbix的安装 本次实验 系 ...

  3. Spring @Resource,@Autowired,@Qualifier的注解注入和区别

    spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...

  4. 20155208徐子涵 Exp4 恶意代码分析

    20155208徐子涵 Exp4 恶意代码分析 实践目标 1.1是监控你自己系统的运行状态,看有没有可疑的程序在运行. 1.2是分析一个恶意软件,就分析Exp2或Exp3中生成后门软件:分析工具尽量使 ...

  5. sdn的相关学习系列之一mininet的安装

    Ubuntu16.04安装mininet 1.安装git命令 sudo apt-get install git 2.从Github上下载Mininet官网的源码 git clone git://git ...

  6. (33)关于django中路由自带的admin + 建表关系的讲解

    admin是django自带的后台管理,在初始的时候就默认配置好了 当输入ip地址的时候后面跟admin,就会登陆管理员的后台,这个是django自带的,可以快速管理数据表(增删改查) PS:ip地址 ...

  7. 2.python发展历程

    创始人:吉多·范罗苏姆于1989年圣诞节在阿姆斯特丹编写 python分为: python 2.X python 3.X 使用python的公司: 豆瓣.BT.Dropbox.YouTube.Quor ...

  8. 虚拟机安装及配置(centOs7)

    准备工作 a)下载VMware workstation14 b)下载CentOS7 CentOS7 c)下载xshell.xftp 安装参考 分区设置 补充(解决网络IP问题,设置IP,service ...

  9. 使用新标签兼容低版本IE

    HTML语义化 意义:根据内容的结构化(语义化),选择合适的标签,便于开发者阅读和写出更优雅的代码,同时让流浪器的爬虫和机器更好的解析. 尽可能少的使用无语义的标签 div 和 span 在语义不明显 ...

  10. phpStudy2018 在win7下切换php7不成功解决办法

    phpstudy 由2016升级到2018后,在切换版本时,php5.6及以下版本可以正常切换,切换7.0以上的版本时访问页面报 0xc000007b 错误,网上找了很多方法都没能解决,最后发现是没装 ...