Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Pull data from an a2l file and save to json?

MrO

New Coder
I will admit i am not very good with python, but what i set out to do i thought was very easy. i learn by example, and i can figure almost anything out eventually and im sure i will after enough time, but man at the moment i am so lost lol. basically i need to pull the name of all the characteristics, and their description from a node in an a2l file or even a txt file saved from the a2l, and save it to a json file in a simple format.

here is the layout of one of many CHARACTERISTICs in the a2l:

/begin CHARACTERISTIC
ABOMX
"max. numbers of starts for recognition of fuel in oil"
VALUE
0x1D86AE
KwUb
255.0
dez
0.00
255.0
FORMAT "%5.1"
/begin IF_DATA ASAP1B_CCP DP_BLOB 0x00 0x5D86AE 1 /end IF_DATA
/begin IF_DATA ETK DP_BLOB 0x9186AE 0x1 /end IF_DATA
/begin IF_DATA ASAP1B_KWP2000 DP_BLOB 0x5D86AE 0x1 /end IF_DATA
/end CHARACTERISTIC


i simply need to pull the second line after /begin CHARACTERISTIC (ABOMX) and the third line ("max. numbers of starts for recognition of fuel in oil") and output them into a JSON file in the format of:

"ABOMX": "max. numbers of starts for recognition of fuel in oil",

then move to the next characteristic in the a2l file and do the same, output to a JSON or even just a txt file with the ID and DEFINITION, but indented to next line so its all pretty and nice like. essentially making a list of the characteristics id's and their definitions. i realize i clearly need to learn more about python, and ive actually gotten myself 3 books to read in my free time to learn basic python haha. but i feel like this isn't a super complex thing to achieve. and im getting so tired of doing it all by hand. Im having a hard time wrapping my head around parsing through and pulling only certain lines. If anyone could point me in the right direction, or give me an example of how to go about pulling specific lines after /begin CHARACTERISTIC, and then outputting them in the "NAME": "DEFINITION", format, or even slightly help nudge me closer to my end goal, i would be crazy thankful. anyway, thanks for taking the time to read all this.
 
Assuming that all the data will have the format shown in your example, you might could do something like this.
This will create a dict. which can easily be converted to a json file.

Code:
begin CHARACTERISTIC
ABOMX
"max. numbers of starts for recognition of fuel in oil"
VALUE
0x1D86AE
KwUb
255.0
dez
0.00
255.0
FORMAT "%5.1"
/begin IF_DATA ASAP1B_CCP DP_BLOB 0x00 0x5D86AE 1 /end IF_DATA
/begin IF_DATA ETK DP_BLOB 0x9186AE 0x1 /end IF_DATA
/begin IF_DATA ASAP1B_KWP2000 DP_BLOB 0x5D86AE 0x1 /end IF_DATA
/end CHARACTERISTIC

/begin CHARACTERISTIC
ABC
"some kind of text here"
VALUE
0x1D86AE
KwUb
255.0
dez
0.00
255.0
FORMAT "%5.1"
/begin IF_DATA ASAP1B_CCP DP_BLOB 0x00 0x5D86AE 1 /end IF_DATA
/begin IF_DATA ETK DP_BLOB 0x9186AE 0x1 /end IF_DATA
/begin IF_DATA ASAP1B_KWP2000 DP_BLOB 0x5D86AE 0x1 /end IF_DATA
/end CHARACTERISTIC

Python:
# Get the file
file = 'test.a2l'

# Create a dict
mydict = {}

# Open and read file
data = open(file).read()

# Split the file into chunks at the /begin CHARACTERISTIC using list comp.
chunks = [chunk for chunk in data.split('/begin CHARACTERISTIC')]

# Loop through the chunks and find the list that have a length greater than 0
for chunk in chunks:
    if len(chunk) > 0:

        # Split the chunk into a list of lines using the newline
        chunk = chunk.split('\n')

        # Add the needed info into a dict. using index 1 for the key and index2 for value
        mydict[chunk[1]] = chunk[2].strip('"')

print(mydict)
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom