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. -
import re
-
-
with open(r'C:\Python27\log\master_input.txt', 'r') as infile, open(r'C:\Python27\log\output', 'w') as outfile:
-
copy = False
-
for line in infile:
-
if re.match("Jun 6 17:58:16(.*)", line):
-
copy = True
-
elif re.match("Jun 6 17:58:31(.*)", line):
-
copy = False
-
elif copy:
-
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
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: - import re
-
-
data = """Jun 6 17:58:13 other strings
-
Jun 6 17:58:13 other strings
-
Jun 6 17:58:14 other strings
-
Jun 6 17:58:14 other strings
-
Jun 6 17:58:15 other strings
-
Jun 6 17:58:15 other strings
-
Jun 6 17:58:15 other strings
-
Jun 6 17:58:15 other strings
-
Jun 6 17:58:16 other strings
-
Jun 6 17:58:16 other strings
-
Jun 6 17:58:16 other strings
-
Jun 6 17:58:16 other strings
-
Jun 6 17:58:16 other strings
-
Jun 6 17:58:16 other strings
-
Jun 6 17:58:17 other strings
-
Jun 6 17:58:17 other strings
-
Jun 6 17:58:17 other strings
-
Jun 6 17:58:17 other strings
-
Jun 6 17:58:18 other strings
-
Jun 6 17:58:18 other strings
-
Jun 6 17:58:18 other strings
-
Jun 6 17:58:18 other strings
-
Jun 6 17:58:18 other strings
-
Jun 6 17:58:19 other strings
-
Jun 6 17:58:19 other strings
-
Jun 6 17:58:20 other strings
-
Jun 6 17:58:20 other strings
-
Jun 6 17:58:21 other strings
-
Jun 6 17:58:21 other strings
-
Jun 6 17:58:21 other strings
-
Jun 6 17:58:21 other strings
-
Jun 6 17:58:22 other strings
-
Jun 6 17:58:23 other strings
-
Jun 6 17:58:24 other strings
-
Jun 6 17:58:27 other strings
-
Jun 6 17:58:28 other strings
-
Jun 6 17:58:28 other strings
-
Jun 6 17:58:29 other strings
-
Jun 6 17:58:29 other strings
-
Jun 6 17:58:29 other strings
-
Jun 6 17:58:29 other strings
-
Jun 6 17:58:30 other strings
-
Jun 6 17:58:31 other strings
-
Jun 6 17:58:31 other strings
-
Jun 6 17:58:32 other strings
-
Jun 6 17:58:33 other strings
-
Jun 6 17:58:33 other strings
-
Jun 6 17:58:33 other strings
-
Jun 6 17:58:33 other strings"""
-
-
patt = re.compile("Jun 6 17:58:(\d+?) (.*)")
-
upper = 31
-
lower = 16
-
-
for line in data.split("\n"):
-
m = patt.match(line)
-
if m:
-
i = int(m.group(1))
-
if i >= lower and i <= upper:
-
print line
@bvdet: Thanks for the solution. Here i do not know the upper and lower value... How did you get those values...
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: - >>> datetime.datetime.strptime("Jun 6 17:58:13", "%b %d %H:%M:%S")
-
datetime.datetime(1900, 6, 6, 17, 58, 13)
-
>>>
From there you can create timedelta objects: - >>> d1 = datetime.datetime.strptime("Jun 6 17:58:13", "%b %d %H:%M:%S")
-
>>> d2 = datetime.datetime.strptime("Jun 7 12:55:48", "%b %d %H:%M:%S")
-
>>> d1-d2
-
datetime.timedelta(-1, 18145)
-
>>> d2-d1
-
datetime.timedelta(0, 68255)
-
>>> dt1 = d1-d2
-
>>> dt1.days
-
-1
-
>>> dt1.total_seconds()
-
-68255.0
-
>>> dt2 = d2-d1
-
>>> dt2.days
-
-
>>> dt2.total_seconds()
-
68255.0
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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()...
|
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:
...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |