T
T
tupoi2016-06-27 16:55:59
Python
tupoi, 2016-06-27 16:55:59

How to fix a small fragment in a txt file using python?

Good day, I have a small problem. You need to get the file from the site standards.ieee.org/develop/regauth/oui/oui.txt
download the file no problems, I would like to convert all this text to json format, I found the script on github, but for some reason it does not work correct, it writes the following to the file

{ "oui": "3C-D9-2B", "vendor": "Hewlett Packard
"},

and due to the fact that the quote and the closing bracket are transferred to a new line, pycharms gives an error that everything is wrong and redo it, and there are 13k lines of code, I would like to fix it programmatically, but I don’t know how to work with files in python 3.5, but it is to perform this procedure, for example, do a backspace every 2 lines. If anyone knows how to solve the problem, then do not be lazy, please explain) thanks
in advance, just in case, here is the parser code from txt to json
#!/usr/bin/awk -f
# Parse IEEE OUI vendor list to JSON
# http://standards.ieee.org/develop/regauth/oui/oui.txt
# ---
# Usage:
#   $ chmod +x parseoui.awk
#   $ wget http://standards.ieee.org/develop/regauth/oui/oui.txt
#   $ ./parseoui.awk oui.txt > oui.json

BEGIN {
    print "[";
}

/(hex)/ {
    printf "{ \"oui\": \"%s\", \"vendor\": \"%s\"},\n", $1, substr($0, index($0, $3));
}

END {
    print "]";
}

UPD: Not necessarily python for the fix, any other language is fine too, the main thing is the result

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Biryukov-Romanov, 2016-06-27
@tupoi

#!/usr/bin/awk -f
# Parse IEEE OUI vendor list to JSON
# http://standards.ieee.org/develop/regauth/oui/oui.txt
# ---
# Usage:
#   $ chmod +x parseoui.awk
#   $ wget http://standards.ieee.org/develop/regauth/oui/oui.txt
#   $ ./parseoui.awk oui.txt > oui.json

BEGIN {
    print "[";
}

/(hex)/ {
    printf "{ \"oui\": \"%s\", \"vendor\": \"%s\"},\n", $1, substr($0, index($0, $3),length($3));
}

END {
    print "]";
}

Here is the correct script. What is the problem - that you have here:
It was extracted "from the beginning of the third element to the end of the line", where the newline character got.
Option
Already extraction from the beginning of the third element to the length of the third element, where the newline character does not fall and everything is printed perfectly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question