473,401 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Read between tags in txt file.

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, 246 views)
Mar 26 '12 #1
2 1873
dwblas
626 Expert 512MB
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
Andy E
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

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

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
3
by: Anne-Marte | last post by:
Hi I simply don't understand how to read a simple file using std::istream. How do I open a file for reading with istream?? Anne-Marte
7
by: Davy | last post by:
Hi all, A read-only data file is read in a C/C++ program. And now I use stdio function such as fopen() to fread() to operate the file. The content of the data file is constant. How to build...
1
by: TaeHo Yoo | last post by:
The file I would like to access in located in C drive and the application which reads the file is located in E drive. When it trys to read the file, it generates an error Error: Request for the...
2
by: Lam | last post by:
Hi the default readline method in StreamReader read the text from the top to bottom how can I read a text file from the bottom to the top? Thanks
4
by: funkmusha | last post by:
I am trying to read a log file using vb.net. I get an error stating "The process cannot access the file 'C:\test.log' because it is being used by another process." Below is a sample of what I am...
3
by: Yaniv | last post by:
Hi I'm new in VB.NET. I wrote an application which opens a text file and read it all lines untill the EOF this file is open for read only and for sharing asllowed. every 5 seconds another...
10
by: Arquitecto | last post by:
Hi , I have a question about a file operation i want to do . I have a data file lets say X bytes . I want to read the file and delete a byte every 2nd byte . I am a little comfused ,my approach...
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
4
by: bmerlover | last post by:
How can you read a file in a timer function? I am currently using a do while statement to read a file which works fine. The implementation is in C but I've added the code to a C++ .NET framework GUI...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.