0

File includes:

Honda
color white
trim black
Chevy
color blue
trim chrome
Hyundai
color red
trim black

Let's say I have hypothetical data from above. I'm trying to create a loop so that when Hyundai is found I get color red. The problem I'm having is when Hyundai is found the loop starts again from the beginning and I get white.

for line in data:
    if Hyundai in line:
       for line in data:
           if 'color' in line:
               color = get_color(line)
               print (color)

Hope this makes sense.

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • How far ahead after you find hyundai do you want to look for color? 1 line? 2 lines? until the end of the data? – SyntaxVoid Aug 14 '19 at 17:41
  • Thank you khelwood for formatting correctly. I was trying to that figure it out. – user11928526 Aug 14 '19 at 17:41
  • SyntaxVoid, I want to exit the loop when the color is found. – user11928526 Aug 14 '19 at 17:43
  • I had a similar problem. Check this post: https://stackoverflow.com/questions/54316679/pythonic-way-of-processing-a-file-between-two-previously-known-strings&ved=2ahUKEwjCsMaD9YLkAhXKRxUIHcbyDawQFjAAegQIBRAB&usg=AOvVaw3vQ34nMVjlwZDAvU7jE8me – Gonzalo Murillo Aug 14 '19 at 17:46

2 Answers2

0

Put it in a function and return:

def getCarColor(car, data):

    car_found = False

    for line in data:
        if car in line:
            car_found = True
        if 'color' in line and car_found:
            return get_color(line)

    return None

print(getCarColor("Hyundai", data))

Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
  • I tried the function as you prescribed and I still get 'white' instead of 'red'. I also put it in a loop and all the cars color is 'white'. It seems like the second for loop starts from the beginning of the file and matching color white. – user11928526 Aug 14 '19 at 18:12
  • Should be fixed – Michael Bianconi Aug 14 '19 at 18:15
0

The loops starts again from the beginning because you went out of your way to tell it to do so. Instead, you need a simple state machine. In the "start state", you're looking for the specific model name. Once that's found, you're in the "want color" state, in which you continue through the data to find the next line with "color".

model = "Hyundai"
state = 0

for line in data:
    if state == 0:   # Look for desired model:
        if model in line:
            state = 1
    elif state == 1:
        if "color" in line:
            color = get_color(line)
            break

This is detailed and dirty, but the concept works. However, if you have to do this repeatedly then change your algorithm altogether. Build a reference dict with the various models and their attributes. You'll make one pass through the data; after that, all uses will be by direct look-up, not by re-parsing the input text. You'll want to finish with something like:

look_up = [
    {"model": "Honda",
     "color": "white",
     "trim": "black"},
    {"model": "Chevy",
     "color": "blue",
     "trim": "chrome"},
    {"model": "Hyundai",
     "color": "red",
     "trim": "black"}
]

Doing so is a different question ... give it a try.

Prune
  • 76,765
  • 14
  • 60
  • 81