the text file is like:
shfhsgfkshkjg
gkjsfkgkjfgkfg
model1
lkgjhllghfjgh
kjfgkjjfghg
endmodel
model2
jfhkjhcgbkcjg
xhbgkxfkgh
endmodel
i want between each model and endmodel ,what is the text should be in the new file.and file name should be like model1,model2....model may be 100 or more.please help me.
13 1785 sicarie 4,677
Recognized Expert Moderator Specialist
Between the two, it looks like "model" is the same between. You could either write a regex or parse the file for matching characters.
but how to write for making the new text file each time.
bvdet 2,851
Recognized Expert Moderator Specialist
The file could be written something like: - fn = "model1"
-
f = open(fn, "w")
-
f.write("\n".join(content))
-
f.close()
"content" would be a list of the text items between "modelX" and "endmodel".
A possible regex pattern could be: - import re
-
patt = re.compile(r"(model\d+)\n([0-9a-zA-Z\n]+?)endmodel", re.MULTILINE)
Then the file names and content could be extracted to a list of lists: - contents = patt.findall(s)
bro does it give 100 FILE if 100 models are there...
bvdet 2,851
Recognized Expert Moderator Specialist
rokstar24,
Yes, if the models are uniquely identified.
can anyone give me proper code which will run directly..because i m trying but i am not getting it.
- f=open("E:\pra.txt","r+")
-
-
-
import re,sys
-
k= re.findall(r"(model\d+)\n([0-9a-zA-Z\n]+?)endmodel",f.read())
-
-
count = 1
-
fwrite = open("filename%s" %(count), 'w')
-
for line in f:
-
if k in line:
-
# close open file object, increment count, open new file object
-
-
count += 1
-
fwrite = open("filename%s" %(count), 'w')
-
fwrite.write(k)
-
fwrite.close()
-
f.close()
i have written this..tell me where i m wrong??
how to make this k to be written in new file..because it is showing k is not string.
bvdet 2,851
Recognized Expert Moderator Specialist
You are right - 'k' is not a string. It is a list of lists, and you have to get the appropriate individual elements, which are strings, and write them to disk. 'k' may look something like this: - [('model1', 'lkgjhllghfjgh\nkjfgkjjfghg\n'), ('model2', 'jfhkjhcgbkcjg\nxhbgkxfkgh\n')]
dwblas 626
Recognized Expert Contributor
k= re.findall(r"(model\d+)\n([0-9a-zA-Z\n]+?)endmodel",f.read())
and
if k in line:
are redundant. Forget the findall and just use if "model" in line, or since the line starts with "model" and we don't want "endmodel"
if line.startswith(model) - test_data = """the text file is like:
-
shfhsgfkshkjg
-
gkjsfkgkjfgkfg
-
model1
-
lkgjhllghfjgh
-
kjfgkjjfghg
-
endmodel
-
model2
-
jfhkjhcgbkcjg
-
xhbgkxfkgh
-
endmodel"""
-
-
file_input = test_data.split("\n")
-
model_list = []
-
ctr = 0
-
for line in file_input:
-
line = line.strip()
-
if line.startswith("model"):
-
if ctr: ## first group is junk & not copied
-
with open(model_list[0], "w") as fp_out:
-
for rec in model_list:
-
fp_out.write("%s \n" % (rec))
-
ctr += 1
-
model_list = []
-
model_list.append(line)
-
-
## final list
-
with open(model_list[0], "w") as fp_out:
-
for rec in model_list:
-
fp_out.write("%s \n" % (rec))
can anyone tell
if i have to give if condition for
means 8 spaces exactly are between model and 1.then what should i do exactly to give condition for finding it.
bvdet 2,851
Recognized Expert Moderator Specialist
Assuming you want to use the regex solution: - patt = re.compile(r"(model *\d+)\n([0-9a-zA-Z\n]+?)endmodel", re.MULTILINE)
dwblas 626
Recognized Expert Contributor
Or use split and join which is also a "standard" way. - test_string="model 1"
-
print test_string
-
print "becomes", "".join(test_string.split())
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Subodh |
last post by:
Hi,
Currently we get data from more then 200 different sources and all of
our vendors provide data in different file formats. The problem is we
have more then 100 DTS packages now and the...
|
by: Tony Liu |
last post by:
Hi, how can I create multiple new file handles of a file without having to
share to file to the other processes?
I have a file that will be accessed by multiple threads in my application,
each...
|
by: Rich Denis |
last post by:
Hello,
I am in need of assistance trying to figure out how to 'Unit Test' my Event
Based Async Pattern (http://msdn2.microsoft.com/e7a34yad.aspx) web service
calls. Specifically how to test the...
|
by: tolcis |
last post by:
Hi!
I need to know the proper way to split existing databases into multiple
file groups. How do I move existing tables into different file groups
(keeping all constrains intact) and move indexes...
|
by: Query Builder |
last post by:
I have one of our production Accounting Databases starting from 2 GB
now grown into a 20 GB Database over the period of a few years...
I have been getting timeouts when transactions are trying to...
| |
by: =?Utf-8?B?aGVyYmVydA==?= |
last post by:
I need to build an event-based asynchronous pattern (around a send/receive
messaging API).
Is there a step-by-step guidance about how to write code for the EBAP ?
Does any book cover this theme...
|
by: Gaijinco |
last post by:
I'm having a weird error compiling a multiple file project:
I have three files:
tortuga.h where I have declared 5 global variables and prototypes for
some functions.
tortuga.cpp where I...
|
by: Prashant |
last post by:
Hi All,
we are using <input id="testFile" runat="server" type="file" />
control to select file from local machine. A problem with this control
is at a time we can select only single file from...
|
by: Vinda |
last post by:
Hi Bytes,
Using a previous question as a base Access 2000 Inserting multiple rows based on a date range.
I also wanted to insert multiple rows into a table according to a date range supplied by a...
|
by: kffacs |
last post by:
Multiple rows based on a date range
I have an MSAccess 2007 DB to record our employees Personal Days Off (PDO). Until now I have only had a form to record each single day taken. This results in...
|
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: 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,...
|
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: 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,...
|
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |