Python 仅当CSV中存在所有值时,CSV到JSON输出
我有一个连接的CSV文件,我正试图将其输出为JSON格式。我应该如何实现CSV文件仅转换为JSON对象的逻辑?所有字段都有一个值Python 仅当CSV中存在所有值时,CSV到JSON输出,python,json,pandas,dataframe,csv,Python,Json,Pandas,Dataframe,Csv,我有一个连接的CSV文件,我正试图将其输出为JSON格式。我应该如何实现CSV文件仅转换为JSON对象的逻辑?所有字段都有一个值 import glob , os import pandas as pd import json import csv with open('some.csv', 'r', newline='') as csvfile, \ open('output.json', 'w') as jsonfile: for row in csv.DictRe
import glob , os
import pandas as pd
import json
import csv
with open('some.csv', 'r', newline='') as csvfile, \
open('output.json', 'w') as jsonfile:
for row in csv.DictReader(csvfile):
restructured = {
'STATION_CODE': row['STORE_CODE'],
'id': row['ARTICLE_ID'],
'name': row['ITEM_NAME'],
'data':
{
# fieldname: value for (fieldname, value) in row.items()
'STORE_CODE': row['STORE_CODE'],
'ARTICLE_ID': row['ARTICLE_ID'],
'ITEM_NAME': row['ITEM_NAME'],
'BARCODE': row['BARCODE'],
'SALE_PRICE': row['SALE_PRICE'],
'LIST_PRICE': row['LIST_PRICE'],
'UNIT_PRICE': row['UNIT_PRICE'],
}
}
json.dump(restructured, jsonfile, indent=4)
jsonfile.write('\n')
目前,这会将CSV文件中的所有值提供到JSON输出中,这是意外行为。关于如何更正此问题的任何输入?首先我循环通过
CSV
的所有元素,并将其添加到JSON数组中。如果任何行元素值为空
,则该行将被忽略
。一旦我获得了JSON数组中的所有行
,我将把它输出到JSON
文件中
import json
import csv
csvjsonarr = []
with open('some.csv', 'r', newline='') as csvfile :
for row in csv.DictReader(csvfile):
hasemptyvalues = False
for rowidx in row :
if row[rowidx] == "" :
hasemptyvalues = True
break
if hasemptyvalues == True :
continue
restructured = {
'STATION_CODE': row['STORE_CODE'],
'id': row['ARTICLE_ID'],
'name': row['ITEM_NAME'],
'data': {
'STORE_CODE': row['STORE_CODE'],
'ARTICLE_ID': row['ARTICLE_ID'],
'ITEM_NAME': row['ITEM_NAME'],
'BARCODE': row['BARCODE'],
'SALE_PRICE': row['SALE_PRICE'],
'LIST_PRICE': row['LIST_PRICE'],
'UNIT_PRICE': row['UNIT_PRICE'],
}
}
csvjsonarr.append(restructured)
if len(csvjsonarr) > 0 :
with open('output.json', 'w') as jsonfile :
json.dump(csvjsonarr, jsonfile, indent=4)
首先,我循环遍历CSV
的所有元素,并将其添加到JSON数组中。如果任何行元素值为空
,则该行将被忽略
。一旦我获得了JSON数组中的所有行
,我将把它输出到JSON
文件中
import json
import csv
csvjsonarr = []
with open('some.csv', 'r', newline='') as csvfile :
for row in csv.DictReader(csvfile):
hasemptyvalues = False
for rowidx in row :
if row[rowidx] == "" :
hasemptyvalues = True
break
if hasemptyvalues == True :
continue
restructured = {
'STATION_CODE': row['STORE_CODE'],
'id': row['ARTICLE_ID'],
'name': row['ITEM_NAME'],
'data': {
'STORE_CODE': row['STORE_CODE'],
'ARTICLE_ID': row['ARTICLE_ID'],
'ITEM_NAME': row['ITEM_NAME'],
'BARCODE': row['BARCODE'],
'SALE_PRICE': row['SALE_PRICE'],
'LIST_PRICE': row['LIST_PRICE'],
'UNIT_PRICE': row['UNIT_PRICE'],
}
}
csvjsonarr.append(restructured)
if len(csvjsonarr) > 0 :
with open('output.json', 'w') as jsonfile :
json.dump(csvjsonarr, jsonfile, indent=4)
需要明确的是,如果CSV
文件在任何行上有空值
;如果CSV
行有任何空值,则不应输出JSON文件只有那些行
不应输出到JSON文件
?@ChathurangaK如果CSV行的值为空,则只有那些行不应输出到JSON文件。谢谢你的澄清!需要明确的是,如果CSV
文件在任何行上有空值
;如果CSV
行有任何空值,则不应输出JSON文件只有那些行
不应输出到JSON文件
?@ChathurangaK如果CSV行的值为空,则只有那些行不应输出到JSON文件。谢谢你的澄清!