Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 仅当CSV中存在所有值时,CSV到JSON输出_Python_Json_Pandas_Dataframe_Csv - Fatal编程技术网

Python 仅当CSV中存在所有值时,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

我有一个连接的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.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文件。谢谢你的澄清!