【发布时间】:2021-09-25 04:07:27
【问题描述】:
我想将我的 python 代码结果存储到 csv 文件中,但这是我的 python 代码,我没有在我的 csv 文件中显示我的 python 结果
import csv
import json
from collections import Counter
with open('result.csv', 'w') as output:
with open('simplejson2.json', 'r') as f:
str1=f.read()
output_data=csv.writer(output, delimiter=',')
data_csv= csv.reader(f, delimiter=',')
data=json.loads(str1)
c = Counter(k[:] for d in data for k, v in d.items() if k.startswith('sta') and v)
output_data.writerow(d)
print("there are total", c['status'], "test case")
c = Counter(k[:] for d in data for k, v in d.items() if k.startswith('status') and v.startswith('failed'))
output_data.writerow(d)
if c['status'] > 0:
print("There are", c['status'], "failed cases")
else:
print("there are", c['status'], "sucessfully pass")
这是我的 simplejson2.json 文件
[
{
"status": "passed",
"name": "Whiskers",
"species" : "cat",
"foods": {
"likes": ["celery", "strawberries"],
"dislikes": ["carrots"]
}
},
{
"status": "failed",
"name": "Woof",
"species" : "dog",
"foods": {
"likes": ["dog food"],
"dislikes": ["cat food"]
}
},
{
"status": "failed",
"name": "Fluffy",
"species" : "cat",
"foods": {
"likes": ["canned food"],
"dislikes": ["dry food"]
}
}
]
我的python代码输出是:-
总共有 3 个测试用例 有 2 个测试用例失败
【问题讨论】:
-
将其保存在 txt 文件中? @东京
-
不,我想保存为 csv 文件格式
-
实际问题是什么?
-
您的代码似乎有缩进问题
-
实际问题是我的 python 代码输出未存储在 csv 文件中,但生成了 csv 文件我希望在 csv 文件中显示我的 python 代码结果
标签: python csv export-to-csv