Python Regular Expression query -


i have code example i'm reading text file 4 different colors.

this colors.txt:

### ##### #########  #example colors #line of colors #line colors part 1         color gray         color blue   # line colors part 2         color yellow         color green 

where's i'm getting gray , blue part 1, , yellow , blue part 2

my python code example is:

#!/usr/lib/env python import re  file = open("color.txt","r") content = file.read() file.close() content = content.split('part ')[1:] dic = {} part in content:     dic[int(part[0])] = part[1:]    def color(part_index):     color = re.findall('color\s(.*?)\s',dic[part_index] )     return color  print color(1) #colors of part 1 print color(2)# colors of part 2 

after runing code got output:

part 1 : ['gray', 'blue']

part 2 : ['yellow', 'green']

i print colors separated example

color(1) gray, color(2) blue , color(3) yellow , color(4) green

that way output be:

gray

blue

yellow

green

is there possible way that? if i'd grateful. thank community.

you can create list, , use in color instead of dic.

colors = [none] # garbage value preserve 1-based indexing value in dic.values():     colors += value 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -