0

I want to run this program to convert the Json file into a dictionary, Im using linux mint, what commands do i use to run the program and convert the file.

import csv
import json


class Coverters:

    def covert_to_dict(self, filename):
        """ Read data from file and transform it to dictionary """
        out = []
        with open(filename, "r") as file:
            output = csv.DictReader(file, fieldnames=self.__labels)
            print(output)
            for row in output:
                print(row)
                out.append(row)
            # for line in file:
            # out.append(dict(zip(self.__labels, line.split('#'))))
        return out

    def json_to_csv_file(self, csv_filename, json_filename):
        """  Helper function to conver JSON to CSV file"""
        with open(json_filename) as file:
            data = json.load(file)

        with open(csv_filename, "wb+") as file:
            csv_file = csv.writer(file)
            for item in data:
                # Need to add all indexes for items
                csv_file.writerow([item['ts'], item['visitor_uuid']] + item['fields'].values())
1
  • json.load(open(fname))? (I'm answering your text, as you code doesn't actually do anything.., at least not something that is obviously related to your question text) Commented Dec 6, 2015 at 17:15

1 Answer 1

1
import csv
import json

def covert_to_dict(filename):
    """ Read data from file and transform it to dictionary """
    out = []
    with open(filename, "r") as file:
        output = csv.DictReader(file, fieldnames=self.__labels)
        print(output)
        for row in output:
            print(row)
            out.append(row)
            # for line in file:
            # out.append(dict(zip(self.__labels, line.split('#'))))
    return out

covert_to_dict("filename") #change ot to the file name

save the above code in a file name it somthing( xyz.py) place the file to convert in same directory

open terminal ->> go the directory ->> and run this command --> python xyz.py

Sign up to request clarification or add additional context in comments.

1 Comment

What about the fact that self.__labels is undefined and covert_to_dict() returns a list?