python文件操作案例

题目:

 1、将提供的 test.csv 文件,具体内容如下: 

python文件操作案例_第1张图片

intend为json缩进量,用来实现换行

test.csv数据:

城市 环比 同比 定基
北京 101.5 120.7 121.4
上海 101.2 127.3 127.8
广州 101.3 119.4 120
深圳 102 140.9 145.5
沈阳 100.1 101.4 101.6
import json

import jieba

article = open('test.csv', 'r').read()
listans=[]
words=article.split('\n')
length=len(words)
for i in range(length):
    words[i]=words[i].split(',')
dictionary=words[0]
for i in range(1,length-1):
    dictionary_seq=words[i]
    dic=dict(zip(dictionary,dictionary_seq))
    listans.append(dic)
out=json.dumps(listans,ensure_ascii=False,indent=4)
print('''"本书作者:{}"'''.format(out))

 

你可能感兴趣的:(pathon)