Home > Blockchain >  how to parse a generated csv string in python
how to parse a generated csv string in python

Time:03-30

So I have this complex string that has been generated in python whilst parsing a csv file. here is the state of the string right now:

[
    {'id': 1463, 'name': 'culture clash'},
    {'id': 2964, 'name': 'future'},
    {'id': 3386, 'name': 'space war'},
    {'id': 3388, 'name': 'space colony'},
    {'id': 3679, 'name': 'society'},
    {'id': 3801, 'name': 'space travel'},
    {'id': 9685, 'name': 'futuristic'},
    {'id': 9840, 'name': 'romance'},
    {'id': 9882, 'name': 'space'},
    {'id': 9951, 'name': 'alien'},
    {'id': 10148, 'name': 'tribe'},
    {'id': 10158, 'name': 'alien planet'},
    {'id': 10987, 'name': 'cgi'},
    {'id': 11399, 'name': 'marine'},
    {'id': 13065, 'name': 'soldier'},
    {'id': 14643, 'name': 'battle'},
    {'id': 14720, 'name': 'love affair'},
    {'id': 165431, 'name': 'anti war'},
    {'id': 193554, 'name': 'power relations'},
    {'id': 206690, 'name': 'mind and soul'},
    {'id': 209714, 'name': '3d'},
    ]

So say that this string is stored in a variable called x. Is there a faster way to parse this string other than using .split('something') over and over again whilst molding the string back together until there are no outliers left?

What I am trying to achieve is to have something that appears much more readable and easy to manipulate like:

id: someNumber, name: someName... repeat until no more data 

What I have attempted was to split the string then mold it back together multiple times until desired result had been achieved. Or is there a much better way to parse as well as manipulate this string?

Basically I need to extract all of the id's and the corresponding names to later insert into a table.

CodePudding user response:

I'm hoping that you have imported and used csv module. Then appended the dictionary in a list. So, it is "list" and not a "string".

You can use the following to print this list:

for i in range(len(test)):
    print(f'id : {test[i]["id"]} name : {test[i]["name"]},', end=" ")

Here test is the name of the list. The output is:

id : 1463, name : culture clash id : 2964, name : future id : 3386, ....

CodePudding user response:

If you really don't want to import any common modules you could do this but note that the implementation is very specific to the data as shown the question:

yourstring = """[{"id": 1463, "name": "culture clash"}, {"id": 2964, "name": "future"}, {"id": 3386, "name": "space war"}, {"id": 3388, "name": "space colony"}, {"id": 3679, "name": "society"}, {"id": 3801, "name": "space travel"}, {"id": 9685, "name": "futuristic"}, {"id": 9840, "name": "romance"}, {"id": 9882, "name": "space"}, {"id": 9951, "name": "alien"}, {"id": 10148, "name": "tribe"}, {"id": 10158, "name": "alien planet"}, {"id": 10987, "name": "cgi"}, {"id": 11399, "name": "marine"}, {"id": 13065, "name": "soldier"}, {"id": 14643, "name": "battle"}, {"id": 14720, "name": "love affair"}, {"id": 165431, "name": "anti war"}, {"id": 193554, "name": "power relations"}, {"id": 206690, "name": "mind and soul"}, {"id": 209714, "name": "3d"}]"""

tokens = yourstring.split()
for i, t in enumerate(tokens):
    if t.endswith('{"id":'):
        print(tokens[i 1][:-1])
  • Related