标签:open lin x86 维数 表格 之间 color span 二维
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);
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);
标签:open lin x86 维数 表格 之间 color span 二维
原文地址:https://www.cnblogs.com/c1q2s3/p/12003070.html