-1
fo = open("asset_database_2_16_0_Release.txt", "r")
fo.seek(9)
sep = fo.read(24 - 9)
print (sep)

What I want is to extract data between these {"code":" and _icon"," characters
{"code":"Abarth_500_2014_icon","type":"8","version":"95"}
{"code":"AlfaRomeo_4CCoupeCrew_2013_icon","type":"8","version":"54"},

I want to extract like this
Abarth_500_2014 only
AlfaRomeo_4CCoupeCrew_2013

I have over 1000 lines all with different car names,
so I am trying to get the .py to read from starting {"code":" <---- data I don't want ---> _icon","type":"8","version":"95"},

All 1000 row have these characters in it {"code":" and _icon","

The code I test works but only for 1 line, and I can't figure out how to
Extract every line, Now this only works for the example I am using above 24 - 9.

Now this file is not a .Json format, I was told that the data looks like Json
so that is why I listed the title as Json.
I rename this file asset_database_2_16_0_Release to asset_database_2_16_0_Release.txt

I have tried using Batch files, but I can't figure out how to get around all the special characters using batch

JR Santos
  • 137
  • 1
  • 11
  • 1
    Python has a JSON parser (if the file is genuinely JSON). Also, JSON files should have .json extensions, not .txt. – jarmod Oct 20 '20 at 03:21
  • 2
    Duplicate of [How to parse data in JSON format?](https://stackoverflow.com/questions/7771011/how-to-parse-data-in-json-format) – esqew Oct 20 '20 at 03:22
  • If you have a json file, then you'd parse it as-is, you would not treat it as plaintext as only read parts of the file. You can parse contents later – OneCricketeer Oct 20 '20 at 03:23
  • Please show your exact file content, or few lines if it's large – OneCricketeer Oct 20 '20 at 03:26
  • OK, the before format is a Json file, but after I extract the data it converts to a .txt file, and the problem is that I tried using a batch file and it's very hard, someone told me to learn Python and I am, but in order to understand it, I need to get an idea on how to improve what I am testing now, I have seen other formats, but because the question that was asked, looks nothing like mine, I get confused on how it suppose to work – JR Santos Oct 20 '20 at 19:48

2 Answers2

0

So, each line of the file is a json object?

Each line has a code key?

Each code value ends in "_icon" and you want to remove it?

import json 
with open("asset_database_2_16_0_Release.txt") as f:
    for line in f:
        code = json.loads(line.rstrip())["code"]
        code = code[:len("_icon")-1]
        print(code)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
-1

Try:

import json
Sep = json.loads(sep)
print(Sep.get("code", None))

json.loads() is a standard library function that will parse a string into a Python data structure, that you conveniently access.

Bernd Wechner
  • 1,854
  • 1
  • 15
  • 32
  • This assumes the file only has one line that matches what's given, also, what's sep? – OneCricketeer Oct 20 '20 at 03:25
  • It assumes nothing. It answers the question given he read: `{"code":"Abarth_500_2014_icon","type":"8","version":"95"}`. All he need do is parse the JSON and fetch the `code` item. Done. The question does not provide much more context to work with. Clearly if you have many lines of similar structures, you do this per line. But who am I or you to guess the full context of this question. He just needs a pointer tot eh json library from what I can tell. – Bernd Wechner Oct 20 '20 at 03:29
  • "the code I test works but only for 1 line, and I can't use that for every line", assumes the file has more than one line, so where are you getting sep from? – OneCricketeer Oct 20 '20 at 03:33
  • Code only answers are discouraged on SO. Consider adding an explanation to benefit future visitors to apply your solutions to their own issues, and to improve quality. Also it is good to note any circumstances that this code does/does not work well with, edge cases, etc. – SherylHohman Oct 20 '20 at 05:54
  • "where are you getting sep from" - from his simple example. – Bernd Wechner Oct 20 '20 at 06:50
  • So the original file is a Json format, BUT it does not have a .json for an ext. The file has no extension and I use a unpacker that will open and rename the file to a .txt, now inside the file I have over 1000 lines, and each line has {"code":" and _icon","type, adn what I am trying to do is grab all the data/content in between that, in each line, what I have only does one line, and I can't figure out how to get the rest of the lines. PLease keep in mind I just started using python 3 days ago – JR Santos Oct 20 '20 at 19:55