0

I have a text file like this:

[0.52, '1_1man::army'], stack 
[0.45, '3_3man::army'], flow
[0.52, '1_1man::army'], testing 
[0.52, '2_2man:army'], expert

How can I load into the file and print all the values for

'1_1man::army', '3_3man::army', '1_1man::army' and '2_2man:army'

My code:

text = open("text.txt", "r").readlines()
print(text[1])

Then to implement the solutions some good people have shared. I cant use their codes since the file I have now is different from the one I posted(I wish to try out this new example).

How can I arrange the list according to similar item in certain location

6
  • 2
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem individually. A good way to show this effort is to include a Minimal, complete, verifiable example. Check the intro tour you finished before posting, especially How to Ask. Commented Aug 28, 2018 at 0:05
  • I have the code so I will add it in right now Commented Aug 28, 2018 at 0:06
  • 1
    might try the search option. this is a pretty basic question which has probably been asked many times before. Commented Aug 28, 2018 at 0:07
  • I tried to search for it too but unfortunately nothing with the similar format as my file. Commented Aug 28, 2018 at 0:09
  • 1
    Side-note: Are all those far right brackets really unmatched by left brackets? That's... odd. If there were properly matched, line by line parsing with json would work. Otherwise, your format looks custom to the point of lunacy; you'd likely have to write any parser from scratch. Commented Aug 28, 2018 at 0:09

2 Answers 2

5

If that format is rigid throughout the file. You could simply use split() to extract those values in between quotes

with open("text.txt", "r") as file:
    for line in file:
        print (line.split("'")[1])

line.split("'") slices the string up whenever it sees a '. In your case, every line would be sliced into a list of 3 elements:

[0.52, 
1_1man::army
], stack

You want the middle one, which has index [1]. So line.split("'")[1] gives you exactly that.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much and I am currently trying to see whether I could use this to implement the codes I received from previous post
1

An easier approach to this would to make a json file instead. Python was a good built in json reading library. This is what the json would look like:

{
    "1_1man::army": "stack",
    "3_3man::army": "flow",
    "1_1man::army": "testing",
    "2_2man::army": "expert",
}

You would enter this and change the file extension from .txt to .json. You can read it like this:

import json

with open("YourText/JsonFileHere.json") as f:
    data = json.load(f)

// Get first 1_1man::army value
data[0]["1_1man::army"]

// Get 3_3man::army value
data["3_3man::army"]

// Get second 1_1man::army value
data["1_1man::army"]

// Get 1_1man::army value
data[1]["1_1man::army"]

// in order to add things to the json do this:
data["What you want the new key to be called"] = "What the value is"

Let me know if this helps!

2 Comments

This text file is actually lead to another example I would like to try and I had asked about it on another topic. If I were to change it to json file then it will be rejecting my whole intention of trying it out with text file. This is my previous topic : stackoverflow.com/questions/52035779/…
You should now be able to add stuff to the .json file.

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.