Home > Mobile >  Read a file from a folder and extract a specific key from the file and save as in CSV file
Read a file from a folder and extract a specific key from the file and save as in CSV file

Time:10-05

I'm new to Python and the task I am performing is to extract a specific key value from a list of .iris ( which contains the list of nested dictionary format) files in a specific directory.

I wanted to extract the specific value and save it as a new .csv file and repeat it for all other files.

Below is my sample of .iris file from which I should extract only for the these keys ('uid','enabled','login','name').

{
    'streamType':'user',
    'uid':'17182',
    'enabled': 'true',
    'login':'xyz',
    'name':'abcdef',
    'comment': '',
    'email': ''
}

I am trying to convert the .iris file to .json and reading the files one by, but unfortunately, I am not getting the exact output as desired.

Please, could anyone help me?


My code (added from comments):

import os
import csv

path = ''
os.chdir(path) 

# Read iris File 
def read_iris_file(file_path): 
    with open(file_path, 'r') as f: 
        print(f.read())
        
# iterate through all files
for file in os.listdir():
    # Check whether file is in iris format or not
    if file.endswith(".iris"):
        file_path = f"{path}\{file}"
        # call read iris file function
        print(read_iris_file(file_path))   

CodePudding user response:

Try the below (the key point here is loading the iris file using ast)

import ast

fields = ('uid','enabled','login','name')
with open('my.iris') as f1:
  data = ast.literal_eval(f1.read())
with open('my.csv','w') as f2:
  f2.write(','.join(fields)   '\n')
  f2.write(','.join(data[f] for f in fields)   '\n')

my.csv

uid,enabled,login,name
17182,true,xyz,abcdef

CodePudding user response:

As it pointed out in this answer, simples way to parse python dict from file is ast.literal_eval(). To iterate over files with certain extension you can use pathlib.glob() with next pattern "*.iris". Then we can use csv.DictWriter() and pass "ignore" to extrasaction argument which will make DictWriter ignore keys which we don't need and write only those which we passed to fieldnames argument.

Code:

import csv
from pathlib import Path
from ast import literal_eval

path = Path(r"path/to/folder")
keys = "uid", "enabled", "login", "name"
with open("result.csv", "w", newline="") as out_f:
    writer = csv.DictWriter(out_f, fieldnames=keys, extrasaction='ignore')
    writer.writeheader()
    for file in path.glob("*.iris"):
        with open(file) as inp_f:
            data = literal_eval(inp_f.read())
        writer.writerow(data)
  • Related