473,396 Members | 2,070 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,396 software developers and data experts.

Extract Values between two strings in a text file using python

8
Lets say I have a Text file (input_file.txt, file size is ~10GB ).

Now I need to write a Python code which will read the text file and copy the contents between Start and end to another file.

I wrote the following code.

Expand|Select|Wrap|Line Numbers
  1. import re  
  2.  
  3. with open(r'C:\Python27\log\master_input.txt', 'r') as infile, open(r'C:\Python27\log\output', 'w') as outfile:  
  4.    copy = False  
  5.    for line in infile:  
  6.       if re.match("Jun  6 17:58:16(.*)", line):  
  7.          copy = True  
  8.       elif re.match("Jun  6 17:58:31(.*)", line):  
  9.          copy = False  
  10.       elif copy:  
  11.          outfile.write(line)  
I'm not getting the desired output as expected:

Output of the code ( output_of_my_code.txt ):

Expected output is ( Expected_output.txt ):

Pls help me here to do it in best way
Attached Files
File Type: txt input_file.txt (1.5 KB, 538 views)
File Type: txt output_of_my_code.txt (215 Bytes, 514 views)
File Type: txt Expected_output.txt (1.1 KB, 497 views)
Jun 10 '15 #1
3 5986
bvdet
2,851 Expert Mod 2GB
To achieve the output you need, use re to determine an integer representing the seconds and compare to the lower and upper boundaries. Here's an example:
Expand|Select|Wrap|Line Numbers
  1. import re
  2.  
  3. data = """Jun  6 17:58:13 other strings
  4. Jun  6 17:58:13 other strings
  5. Jun  6 17:58:14 other strings
  6. Jun  6 17:58:14 other strings
  7. Jun  6 17:58:15 other strings
  8. Jun  6 17:58:15 other strings
  9. Jun  6 17:58:15 other strings
  10. Jun  6 17:58:15 other strings
  11. Jun  6 17:58:16 other strings
  12. Jun  6 17:58:16 other strings
  13. Jun  6 17:58:16 other strings
  14. Jun  6 17:58:16 other strings
  15. Jun  6 17:58:16 other strings
  16. Jun  6 17:58:16 other strings
  17. Jun  6 17:58:17 other strings
  18. Jun  6 17:58:17 other strings
  19. Jun  6 17:58:17 other strings
  20. Jun  6 17:58:17 other strings
  21. Jun  6 17:58:18 other strings
  22. Jun  6 17:58:18 other strings
  23. Jun  6 17:58:18 other strings
  24. Jun  6 17:58:18 other strings
  25. Jun  6 17:58:18 other strings
  26. Jun  6 17:58:19 other strings
  27. Jun  6 17:58:19 other strings
  28. Jun  6 17:58:20 other strings
  29. Jun  6 17:58:20 other strings
  30. Jun  6 17:58:21 other strings
  31. Jun  6 17:58:21 other strings
  32. Jun  6 17:58:21 other strings
  33. Jun  6 17:58:21 other strings
  34. Jun  6 17:58:22 other strings
  35. Jun  6 17:58:23 other strings
  36. Jun  6 17:58:24 other strings
  37. Jun  6 17:58:27 other strings
  38. Jun  6 17:58:28 other strings
  39. Jun  6 17:58:28 other strings
  40. Jun  6 17:58:29 other strings
  41. Jun  6 17:58:29 other strings
  42. Jun  6 17:58:29 other strings
  43. Jun  6 17:58:29 other strings
  44. Jun  6 17:58:30 other strings
  45. Jun  6 17:58:31 other strings
  46. Jun  6 17:58:31 other strings
  47. Jun  6 17:58:32 other strings
  48. Jun  6 17:58:33 other strings
  49. Jun  6 17:58:33 other strings
  50. Jun  6 17:58:33 other strings
  51. Jun  6 17:58:33 other strings"""
  52.  
  53. patt = re.compile("Jun  6 17:58:(\d+?) (.*)")
  54. upper = 31
  55. lower = 16
  56.  
  57. for line in data.split("\n"):
  58.     m = patt.match(line)
  59.     if m:
  60.         i = int(m.group(1))
  61.         if i >= lower and i <= upper:
  62.             print line
Jun 10 '15 #2
helloR
8
@bvdet: Thanks for the solution. Here i do not know the upper and lower value... How did you get those values...
Jun 11 '15 #3
bvdet
2,851 Expert Mod 2GB
You knew the upper and lower values in your original post. How did you know them? If you are dealing with dates and times instead of strictly formatted data, look into using the time and datetime modules. Example of creating a datetime object from the date/time string:
Expand|Select|Wrap|Line Numbers
  1. >>> datetime.datetime.strptime("Jun  6 17:58:13", "%b  %d %H:%M:%S")
  2. datetime.datetime(1900, 6, 6, 17, 58, 13)
  3. >>>
From there you can create timedelta objects:
Expand|Select|Wrap|Line Numbers
  1. >>> d1 = datetime.datetime.strptime("Jun  6 17:58:13", "%b  %d %H:%M:%S")
  2. >>> d2 = datetime.datetime.strptime("Jun  7 12:55:48", "%b  %d %H:%M:%S")
  3. >>> d1-d2
  4. datetime.timedelta(-1, 18145)
  5. >>> d2-d1
  6. datetime.timedelta(0, 68255)
  7. >>> dt1 = d1-d2
  8. >>> dt1.days
  9. -1
  10. >>> dt1.total_seconds()
  11. -68255.0
  12. >>> dt2 = d2-d1
  13. >>> dt2.days
  14.  
  15. >>> dt2.total_seconds()
  16. 68255.0
  17. >>> 
Jun 11 '15 #4

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

Similar topics

1
by: Serge Guay | last post by:
I have been trying to print a text file to my printer but the most I have been able to do is print one line. I have been using the following commands. dc = win32ui.CreateDC()...
2
by: Simon Verona | last post by:
Not sure if this is the best group... it may be better off in one of the ADO groups, but I'm sure somebody here knows the answer: I'm trying to load up a text file using ADO.net, as follows: ...
11
by: muttu2244 | last post by:
hi everybody i want to write a set of values to a file from python. For ex:: the fields name will "comp name", "ip addr", "mac addr" etc. And below all these fields i ll have the values for...
16
by: Preben Randhol | last post by:
Hi A short newbie question. I would like to extract some values from a given text file directly into python variables. Can this be done simply by either standard library or other libraries? Some...
2
by: Praveen_db2 | last post by:
Hi all Db2 8.1.3 windows Is there any way to write data into a text file using a stored procedure? The way we return a cursor output to the calling application, can we return data in a text...
0
by: psbasha | last post by:
Hi, Is there any module available to write and read a XML file using Python? Links for the material will help me. Thanks in advance PSB
4
by: varsha desai | last post by:
Hello there, I want to change some data(which is in one line only) of text file using VB 6.0. Which is the best method for it? Another question is I want to delete last two, three lines...
2
by: sajidali | last post by:
i am trying to append a text file using c#. when i executes my application i writes to the text file using more then one functions. i want to append file only during execution of programe. next time...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.