0

I am converting .csv to JSON but the strings are not in a array form, and I need them to be inside an array.

Code I use to convert .csv to JSON:

    def import_passwords(self):
        options = QFileDialog.Options()
        fileName, _ = QFileDialog.getOpenFileName(self,"Passwords", "Passwords","Excel File (*.csv)", options=options)
        if fileName:
            print(fileName)
            csvfile = open(fileName, 'r')
            jsonfile = open(password_dir + 'passwords.json', 'w')

            fieldnames = ("site name","username","password")
            reader = csv.DictReader(csvfile, fieldnames)
            out = json.dumps([row for row in reader])
            # out = out.replace("\\", '')
            # jsonfile.write(out)

            # with open(password_dir + 'passwords.json', mode='w+', encoding='utf-8') as file:
            #     json.dump(out, file)

            # with open(password_dir + 'passwords.json') as file:
            passwords_json = json.loads(out)
            # sort json file
            sorted_obj = sorted(passwords_json, key=lambda x : x['site name'], reverse=False)
            # Write to passwords file
            with open(password_dir + 'passwords.json', mode='w+', encoding='utf-8') as file:
                json.dump(sorted_obj, file, ensure_ascii=True, indent=4, sort_keys=True, separators=(',', ': '))

I have no idea how I would convert them in a efficient way.

What I get:

[
    {
        "password": "asd",
        "site name": "asd",
        "username": "asd"
    },
    {
        "password": "asd",
        "site name": "asdasd",
        "username": "asd"
    },
    {
        "password": "asd",
        "site name": "asdasdasd",
        "username": "asd"
    }
]

What I want:

[
    {
        "password": ["asd"],
        "site name": ["asd"],
        "username": ["asd"]
    },
    {
        "password": ["asd"],
        "site name": ["asdasd"],
        "username": ["asd"]
    },
    {
        "password": ["asd"],
        "site name": ["asdasdasd"],
        "username": ["asd"]
    }
]

I get the as string, and I want array.

1 Answer 1

2

you can just do it:

data = [
    {
        "password": "asd",
        "site name": "asd",
        "username": "asd"
    },
    {
        "password": "asd",
        "site name": "asdasd",
        "username": "asd"
    },
    {
        "password": "asd",
        "site name": "asdasdasd",
        "username": "asd"
    }
]


for dic in data:
    for key in dic.keys():
        dic[key] = [dic[key]]

or use list comprehension:

data = [{k: [v] for k,v in d.items()} for d in data]

output:

[{'password': ['asd'], 'site name': ['asd'], 'username': ['asd']}, {'password': ['asd'], 'site name': ['asdasd'], 'username': ['asd']}, {'password': ['asd'], 'site name': ['asdasdasd'], 'username': ['asd']}]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.