By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,385 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,385 IT Pros & Developers. It's quick & easy.

Read between tags in txt file.

P: 2
Hi,

I've been strugling with a problem I hope some of you can help me with. It's probably fairly easy but i haven't been able to crack the nut. I have a file with tags like shown below (or attached). I'm interested in the information between the tags.

The information is dynamic so the number of lines between the tags can change but the schema is always the same. Any ideas on how to solve this?
I have been experiencing with readline and readlines() but either i get to much or to little information out.

<--table GenerelStatistics starts-->
Name Count Maximum Minimum Mean
ContextCreated 3521 23.9259986877441 0 0.341778182875057 1.75451453132994 1203.40098190308
ContextCreationFailed 2 0.671998977661133 0.562999725341797 0.617499351501465 0.077074110459266 1.23499870300293
ContextCreationTimeout 0 0 0 0 0 0
<--table GenerelStatistics ends-->

<--table ContextCreated starts-->
Name Count Maximum Minimum Mean StandardDeviation Sum
AGS10SetupTest/KomColumbia 0 0 0 0 0 0
AGS10SetupTest/Kommuner 19 0.0159988403320313 0 0.00247363040321752 0.00587228232873751 0.0469989776611328
AGS10SetupTest/KomWebberville 0 0 0 0 0 0
<--table ContextCreated ends-->

<--table ContextCreationFailed starts-->
Name Count Maximum Minimum Mean
0 0
AppMan/elfaville 0 0 0 0 0 0
Dimension 0 0 0 0 0 0
Dimension1 0 0 0 0 0 0
<--table ContextCreationFailed ends-->

Cheers
Attached Files
File Type: txt Tags.txt (872 Bytes, 210 views)
Mar 26 '12 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 626
You can use groupby to do that. This is a test program where the data was changed to the sample you posted. For further help, post the code you are using as it is impossible to guess which parts of Python you want to use on this problem.
Expand|Select|Wrap|Line Numbers
  1. from itertools import groupby
  2.  
  3. def key_func(rec):
  4.     if rec.strip().startswith("<--"):
  5.         return True
  6.     return False
  7.  
  8. test_data="""<--table GenerelStatistics starts-->
  9.  Name Count Maximum Minimum Mean
  10.  ContextCreated 3521 23.9259986877441 0 0.341778182875057 1.75451453132994 1203.40098190308
  11.  ContextCreationFailed 2 0.671998977661133 0.562999725341797 0.617499351501465 0.077074110459266 1.23499870300293
  12.  ContextCreationTimeout 0 0 0 0 0 0
  13.  <--table GenerelStatistics ends-->
  14.  
  15.  <--table ContextCreated starts-->
  16.  Name Count Maximum Minimum Mean StandardDeviation Sum
  17.  AGS10SetupTest/KomColumbia 0 0 0 0 0 0
  18.  AGS10SetupTest/Kommuner 19 0.0159988403320313 0 0.00247363040321752 0.00587228232873751 0.0469989776611328
  19.  AGS10SetupTest/KomWebberville 0 0 0 0 0 0
  20.  <--table ContextCreated ends-->
  21.  
  22.  <--table ContextCreationFailed starts-->
  23.  Name Count Maximum Minimum Mean
  24.  0 0
  25.  AppMan/elfaville 0 0 0 0 0 0
  26.  Dimension 0 0 0 0 0 0
  27.  Dimension1 0 0 0 0 0 0
  28.  <--table ContextCreationFailed ends-->
  29. """
  30.  
  31. test_list=test_data.split("\n")
  32. groups=[]
  33. for k, g in groupby(test_list, key=key_func):
  34.     groups.append(list(g))      # Store group iterator as a list
  35. for group in groups:
  36.     print group, "\n"
  37.  
  38. ## or
  39. print "-"*50
  40. for ctr in range(0, len(groups), 2):
  41.     print groups[ctr]
  42.     print groups[ctr+1]
  43.     print "-"*50 
Mar 27 '12 #2

P: 2
Hi Dwblas,

Thanks for the answer. I made it work using the following to identify the tags.

#Input data
f = open(r'C:\Projekter\AGS_Statistics\output.txt')
line = f.readlines()

for position, item in enumerate(line):
if item == "<--table ArcGISConfiguration starts-->\n":
print position

However i would have to do that for every tag. Your way seems a lot smarter so I'll try to implement that instead. Thanks a lot
Mar 28 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.