简体   繁体   中英

Running Python program with a file

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())
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM