Python实现csv格式转换成JSON格式文件

csv文件内容如下:


1 Twin Oaks Place    
10 Marquette Rd.    
12 Craven Way    
12 Fort Sheriden Ave.    
12 Skokie Valley Rd.    
12 Walker Ave.    
120 high St.

一、使用内置函数处理

# /usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')
#根据列表中是否为空,将不为空的配成键值对更新到字典中
def list_name(keyname, value1, dict1=None):
    dict1 = dict(zip(keyname, value1))
    return dict1
with open(r'D:\address.csv', 'rb') as f:
    for line in f:
        if line == []:
            line =""
        else:
            if line[-1] == "\n":
                line = line[:-1]
                if line[-1] == "\r":
                    line = line[:-1]
            akk = [y for y in line.split(" ")]
            key1 = ['street','namefirst','namelast','address']
            a1 = {}
            arr = list_name(key1,akk,a1)
            arr = json.dumps(arr)
            print arr

输出如下:


{"namelast": "Oaks", "street": "1", "namefirst": "Twin", "address": "Place"}
{"namelast": "Rd.", "street": "10", "namefirst": "Marquette"}
{"namelast": "Way", "street": "12", "namefirst": "Craven"}
{"namelast": "Sheriden", "street": "12", "namefirst": "Fort", "address": "Ave."}
{"namelast": "Valley", "street": "12", "namefirst": "Skokie", "address": "Rd."}
{"namelast": "Ave.", "street": "12", "namefirst": "Walker"}
{"namelast": "St.", "street": "120", "namefirst": "high"}

二、自己定义函数,内容可控

# /usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import json
reload(sys)
sys.setdefaultencoding('utf-8')
#根据列表中是否为空,将不为空的配成键值对更新到字典中
def list_name(keyname, value1, dict1=None):
    for i in range(0, len(value1)):
        if value1[i] == "":
            break
        else:
            dit = {keyname[i]: value1[i]}
            dict1.update(dit)
        i += 1;
    return dict1
with open(r'D:\address.csv', 'rb') as f:
    for line in f:
        if line == []:
            line =""
        else:
            if line[-1] == "\n":
                line = line[:-1]
                if line[-1] == "\r":
                    line = line[:-1]
            akk = [y for y in line.split(" ")]
            key1 = ['street','namefirst','namelast','address']
            a1 = {}
            arr = list_name(key1,akk,a1)
            arr = json.dumps(arr)
            print arr

输出如下:

{"namelast": "Oaks", "street": "1", "namefirst": "Twin", "address": "Place"}
{"namelast": "Rd.", "street": "10", "namefirst": "Marquette"}
{"namelast": "Way", "street": "12", "namefirst": "Craven"}
{"namelast": "Sheriden", "street": "12", "namefirst": "Fort", "address": "Ave."}
{"namelast": "Valley", "street": "12", "namefirst": "Skokie", "address": "Rd."}
{"namelast": "Ave.", "street": "12", "namefirst": "Walker"}
{"namelast": "St.", "street": "120", "namefirst": "high"}


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29668428/viewspace-2215916/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29668428/viewspace-2215916/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值