3

I need to parse a text file and get output and add them to list.

    with open(qwer.txt, 'r') as my_file:
        a = my_file.readlines()
        for line in a:
            for part in line.split():
                if "color=" in part:
                    p1 = part.split('=')[1] 
                    print(p1)
                    list1 = []  
                    p1.append(list1)

so, i have a line where i have two/more "color=" on same line and i need output to get both the colors:

my color=red and my color=green

my desired output is:

red
green

and i need to add them to a list seperately as ['red','green'] .please help! answers will be appreciated.

4 Answers 4

5

You can of course, use regex:

import re
str = "my color=red and my color=green"
p = r'color=([a-z]+)'
x = re.findall(p, str)
Sign up to request clarification or add additional context in comments.

4 Comments

i need to add to list,how should i do it?
@adarshram The variable x WILL be a list. You can read about re.findall() here
how should i open my files and perform this operation rath?er than a single str
@adarshram After for line in a:, use line instead of str
3

Your solution is almost correct, you print the correct lines. Here is how to save them to a list:

result = []
with open('qwer.txt') as my_file:
    for line in my_file:
        for part in line.strip().split():
            if "color=" in part:
                p1 = part.split('=')[1] 
                result.append(p1)

If you need a list of lists, do it like this:

result = []
with open('qwer.txt') as my_file:
    for line in my_file:
        values = []
        for part in line.strip().split():
            if "color=" in part:
                p1 = part.split('=')[1] 
                values.append(p1)
        if values:
            result.append(values)

2 Comments

I'd do line.strip().split() , I doubt if he wants "red\n" in his list.
Indeed, added the .strip().
2

Perhaps you can do it without regex:

parts = itertools.chain.from_iterable(line.split() for line in my_file)
colors = [part.split('color=')[1] for part in parts if part.startswith('color=')]

Although regex do seem to fit here best.

Comments

1

Most probably we should avoid using groups. You could get the value of color through lookbehind,

>>> import re
>>> str = 'my color=red and my color=green'
>>> m = re.findall('(?<=color=)\S+', str)
>>> m
['red', 'green']

Comments

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.