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

麻蛋,这个着色好难看

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. 利用ASK/OOK 发射模块,实现信号重放

    本文以打开无线控制的电动车库卷帘门为目标,深入研究了ASK/OOK的编/解码,并用树莓派+五元钱的ASK/OOK 发射模块 背景 车库装了电动卷帘门,为了了解其安全性,也是为了能自主控制,研究了下其遥 ...

  2. 自动化测试-4.selenium的xpath定位

    前言 在上一篇简单的介绍了用工具查看目标元素的xpath地址,工具查看比较死板,不够灵活,有时候直接复制粘贴会定位不到.这个时候就需要自己手动的去写xpath了,这一篇详细讲解xpath的一些语法. ...

  3. JDBC driver连接MySQL运行报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than

    出错原因: 因为安装mysql的时候时区设置的不正确. mysql默认的是美国的时区,而我们中国大陆要比他们迟8小时,采用GMT+8:00格式. 也就是说是数据库和系统时区差异所造成的. 验证:运行c ...

  4. 用vs2008打开vs2005项目

    1 使用记事本打开*.sln解决方案文件,将Visual Studio 2005改为Visual Studio 2008 将版本号改为9.00 2 打开扩展名为*.csproj的项目文件,在Defau ...

  5. PHP 框架

    LARAVEL/LUMEN,  CI  ,THINKPHP, YII ,SYMFONY YAF, PHALCON ,ICE FRAMEWORK

  6. if 循环

    age_of_princal = 56guess_age = int(input(">>:")) if guess_age == age_of_princal: pri ...

  7. android ListView 可缩放,支持左右上下手势

    public class ZoomListView extends ListView implements SwipeListener { public static enum Action { Le ...

  8. PythonStudy——数据类型总结 Data type summary

    按存储空间的占用分(从低到高) 数字 字符串 集合:无序,即无序存索引相关信息,可变.集合中的元素必须是可hash的,即不可变的数据类型. 元组:有序,需要存索引相关信息,不可变 列表:有序,需要存索 ...

  9. python 保存对象文件

    之前用 R 语言一直感觉 .Rdata 格式的文件很好用,可以把每次执行的中间文件保存便于下次调用,刚熟悉 Python 还没接触这块知识,所以有时候做项目不太顺手,索性上网搜了下,整理如下: 模型存 ...

  10. ASP.NET资源大全-知识分享 【转载】

    API 框架 NancyFx:轻量.用于构建 HTTP 基础服务的非正式(low-ceremony)框架,基于.Net 及 Mono 平台.官网 ASP.NET WebAPI:快捷创建 HTTP 服务 ...