Untitled

mail@pastecode.io avatar
unknown
python
23 days ago
2.5 kB
1
Indexable
Never
import json
import pandas as pd


def reformat(jsn):
    new_json = {}
    new_json['name'] = jsn['INPUT.filename']
    new_json['link'] = jsn['INPUT.cloudUrl']
    # new_json['id'] = jsn['INPUT.data.id'] #jsn['INPUT.data'].split('id\":')[1].split(',')[0]

    for i in range(250, 500):
        question = 'OUTPUT.question.' + str(i)
        answer = 'OUTPUT.answer.' + str(i)
        if question in jsn and jsn[question] != "Style":
            new_json[jsn[question]] = jsn[answer]

    print(new_json.keys())
    return new_json


def main():
    file_name = "Batch_1_1_new"
    file_name2 = "Batch_1_2_new"
    file_name3 = "Batch_1_3"
    file_name4 = "Batch_1_4_new"
    file_name5 = "Batch_1_5_new"

    with open("30.01 delivery/30.01 delivery/" + file_name + ".json") as f:
        jsons = json.load(f)

    reformated_jsons = []

    print(jsons[0])
    print(reformat(jsons[0]))

    for jsn in jsons:
        reformated_jsons.append(reformat(jsn))

    # save as new file
    with open(file_name + "_REFORMATTED.json", 'w') as f:
        json.dump(reformated_jsons, f)



def row_to_json(row):
    print(row)
    new_json = {}

    new_json['name'] = row['INPUT File']
    new_json['link'] = row['INPUT.fileUrl']
    new_json['id'] = row['INPUT.data'].split('id\":')[1].split(',')[0]

    new_json['Subject'] = row['Subject']
    new_json['Environment'] = row['Environment']
    new_json['Camera'] = row['Camera']
    new_json['Description'] = row['Description']
    new_json['Motions'] = row['Motions']

    print(new_json.keys())
    print(new_json['id'])
    return new_json

def exctract_id(row):
    if pd.isna(row["INPUT.fileUrl"]):
        return row

    print(row)
    row["INPUT.data"] = row['INPUT.data'].split('id\":')[1].split(',')[0]
    return row

def main2():
    file_name = "26.01_delivery/Team Sayantan calibration batch - QA copy.csv"
    df = pd.read_csv(file_name)

    json_file = []
    print(df.columns)

    ids_df = df[["INPUT File", "INPUT.fileUrl", "INPUT.data"]].copy()
    ids_df.apply(exctract_id, axis=1)
    ids_df = ids_df.rename(columns={'INPUT.data': 'IDs'})
    ids_df.to_csv("Team Sayantan calibration batch_IDS.csv", index=False)

    # for index, row in df.iterrows():
    #
    #
    #     json_file.append(row_to_json(row))
    #     if row["INPUT.fileUrl"] == "https://staging.saharaa.info/api/resources/download/1078":
    #         break
    #
    # with open("Team Sayantan calibration batch" + "_FORMATTED.json", 'w') as f:
    #     json.dump(json_file, f)


main()
Leave a Comment