How to extract particular set of value from a file in Python?

I am stuck with the logic here... i have to extract some values from a text file that looks like this

AAA
+-------------+------------------+
|          ID |            count |
+-------------+------------------+
|           3 |             1445 |
|           4 |              105 |
|           9 |              160 |
|          10 |               30 |
+-------------+------------------+
BBB
+-------------+------------------+
|          ID |            count |
+-------------+------------------+
|           3 |             1445 |
|           4 |              105 |
|           9 |              160 |
|          10 |               30 |
+-------------+------------------+
CCC
+-------------+------------------+
|          ID |            count |
+-------------+------------------+
|           3 |             1445 |
|           4 |              105 |
|           9 |              160 |
|          10 |               30 |
+-------------+------------------+

I am not able to extract value from BBB alone and append it to a list like

f = open(sys.argv[1], "r")
text = f.readlines()
B_Values = []
for i in text:
    if i.startswith("BBB"):(Example)
       B_Values.append("only values of BBB")
    if i.startswith("CCC"):
       break

print B_Values

should result

['|           3 |             1445 |','|           4 |              105 |','|           9 |              160 |','|          10 |               30 |']

Answers


d = {}
with open(sys.argv[1]) as f:
    for line in f:
        if line[0].isalpha(): # is first character in the line a letter?
            curr = d.setdefault(line.strip(), [])
        elif filter(str.isdigit, line): # is there any digit in the line?
            curr.append(line.strip())

for this file, d is now:

{'AAA': ['|           3 |             1445 |',
         '|           4 |              105 |',
         '|           9 |              160 |',
         '|          10 |               30 |'],
 'BBB': ['|           3 |             1445 |',
         '|           4 |              105 |',
         '|           9 |              160 |',
         '|          10 |               30 |'],
 'CCC': ['|           3 |             1445 |',
         '|           4 |              105 |',
         '|           9 |              160 |',
         '|          10 |               30 |']}

Your B_values are d['BBB']


Need Your Help

How to add user input to own image effect module in drupal?

drupal input effect

I gladly managed it to write my own module which adds an gaussian blur filter effect option to the drupal standard dropdown:

canvas.delete(ALL) not working

python python-2.7 canvas tkinter

I'm currently making the graphical portion of a poker game in Tkinter with Python 2.7, but I'm having trouble with redrawing the canvas at the moment. For some reason self.canvas.delete(ALL) won't ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.