CSV和JSON格式相互转换

1、为什么要进行CSV与JSON格式之间的转换

  CSV格式常用于一二维数据表示和存储,他是一种纯文本形式存储表格数据的表示方式。JSON也可以表示一二维数据。在网络信息传输中,可能需要统一表示方式,因此,需要在CSV和JSON格式间进行相互转换。

2、代码

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

原文地址:https://www.cnblogs.com/c1q2s3/p/12003070.html

时间: 2024-08-30 04:03:25

CSV和JSON格式相互转换的相关文章

xml和JSON格式相互转换的Java实现

依赖的包: json-lib-2.4-jdk15.jar ezmorph-1.0.6.jar xom-1.2.1.jar commons-lang-2.1.jar commons-io-1.3.2.jar jaxen-1.1.jar 输入xml文件,输出JSON对象 package com.cash.util; import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils;

字典和JSON格式字符串相互转换

在iOS开发中,和服务器交互中,经常用到字典和JSON格式字符串相互转换. 1.JSON格式字符串转字典 + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return nil; } NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *err; NSDi

Python——json格式数据与字典相互转换

import json dict = {'s':'you','d':'are'} #字典 #字典转json格式 dataJson = json.dumps(dict) #json字符串转化为字典 d1 = json.loads(dataJson) 原文地址:https://www.cnblogs.com/ScarecrowMark/p/11281438.html

Scala中json格式、字符串、map相互转换

json对象转换为json字符串: import org.json4s.{Formats,NoTypeHints} import org.json4s.jackson.Serialization import org.json4s.jackson.Serialization.write case class userLableLike(id:String,pos:Float,neg:Float,seg:Double) def userLable2Str(data:userLableLike):

pyhton读取json格式的气象数据

原文关于读取pm25.in气象数据的pyhton方法,以及浅析python json的应用 以pm25.in网站数据为例. 1.方法介绍 首先感谢pm25.in提供了优质的空气污染数据,为他们的辛勤劳动点个赞.是python3.3,windows系统,读取数据的时候用到了python的json处理的4个方法,很经典常用.所谓4个方法是: a)       json.loads() 输入string,返回json. b)       json.dumps() 输入json类型的数据,返回包含jso

C#深入解析Json格式内容

继上一篇<浅谈C#手动解析Json格式内容>我又来分析加入了一些功能让 这个解析类更实用 本章节最会开放我最终制作成功的Anonymous.Json.dll这个解析库 需要的拿走~ 功能继上一篇增加了许多上一篇只是讲述了  解析的步骤但是 至于一些扩展的功能却没有涉及 本文将继续讲解 1.如何将json转换为一个类或者结构 甚至属性 2.如何将一个类或者结构甚至属性转换为json 就这两点就已经很头疼了 诶 废话不多说进入正题 上一篇一直有个很神秘的JsonObject没有讲解 现在先来揭开J

iOS:JSON格式字符串转字典,字典转JSON格式字符串

在iOS开发中,和服务器交互中,经常用到字典和JSON格式字符串相互转换. 代码如下: 1.JSON格式字符串转字典 + (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString { if (jsonString == nil) { return nil; } NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; NSError *err

json字符串和json格式对象的转换

json字符串和json格式对象的转换: 实际编码中json字符串和json格式的对象有时候需要进行相互转换,下面就介绍一下如何实现. 一.jQuery插件支持的转换方式: $.parseJSON( jsonstr ); 以上代码可以将json字符串转换为json格式对象. 二.浏览器支持的转换方式: JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON.stringify(jsonobj); //可以将json对象转换成json对符串 注意:ie7和i

python 读写json文件(dump, load),以及对json格式的数据处理(dumps, loads)

JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. 1.json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串) json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串) json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数