read file block by block in python
I have file that has this format
MACRO L20_FDPQ_TV1T8_1
FIXEDMASK ;
CLASS CORE ;
...
...
END L20_FDPQ_TV1T8_1
MACRO INV_20
...
...
END INV_20
I want to read the file as blocks such that each MACRO to end of its name form a block in python. I tried to use this
with open(file_name, "r") as f_read:
lines = f_read.readlines()
num = 0
while num < len(lines):
line = lines[num]
if re.search(r"MACROs+", line, re.IGNORECASE):
macro_name = line.split()[1]
while re.search(r"s+ENDs+%s"%macro_name, line, re.IGNORECASE) is None:
line = lines[num + 1]
#...do something...
num = num+1
num +=1
how this can be done in an efficient way?
python file
add a comment |
I have file that has this format
MACRO L20_FDPQ_TV1T8_1
FIXEDMASK ;
CLASS CORE ;
...
...
END L20_FDPQ_TV1T8_1
MACRO INV_20
...
...
END INV_20
I want to read the file as blocks such that each MACRO to end of its name form a block in python. I tried to use this
with open(file_name, "r") as f_read:
lines = f_read.readlines()
num = 0
while num < len(lines):
line = lines[num]
if re.search(r"MACROs+", line, re.IGNORECASE):
macro_name = line.split()[1]
while re.search(r"s+ENDs+%s"%macro_name, line, re.IGNORECASE) is None:
line = lines[num + 1]
#...do something...
num = num+1
num +=1
how this can be done in an efficient way?
python file
What do you mean by to end of its name?
– toti08
Nov 19 '18 at 15:31
You should show whether you have tried anything or not.
– ahota
Nov 19 '18 at 15:47
why are you searching for "MACRO" and "END"? The beginning of a macro must exactly start with "MACRO" and you know it will end when the line starts exactly with "END". No need to search
– Sembei Norimaki
Nov 19 '18 at 16:04
is this is the efficient way? @ahota
– M.salameh
Nov 19 '18 at 16:10
add a comment |
I have file that has this format
MACRO L20_FDPQ_TV1T8_1
FIXEDMASK ;
CLASS CORE ;
...
...
END L20_FDPQ_TV1T8_1
MACRO INV_20
...
...
END INV_20
I want to read the file as blocks such that each MACRO to end of its name form a block in python. I tried to use this
with open(file_name, "r") as f_read:
lines = f_read.readlines()
num = 0
while num < len(lines):
line = lines[num]
if re.search(r"MACROs+", line, re.IGNORECASE):
macro_name = line.split()[1]
while re.search(r"s+ENDs+%s"%macro_name, line, re.IGNORECASE) is None:
line = lines[num + 1]
#...do something...
num = num+1
num +=1
how this can be done in an efficient way?
python file
I have file that has this format
MACRO L20_FDPQ_TV1T8_1
FIXEDMASK ;
CLASS CORE ;
...
...
END L20_FDPQ_TV1T8_1
MACRO INV_20
...
...
END INV_20
I want to read the file as blocks such that each MACRO to end of its name form a block in python. I tried to use this
with open(file_name, "r") as f_read:
lines = f_read.readlines()
num = 0
while num < len(lines):
line = lines[num]
if re.search(r"MACROs+", line, re.IGNORECASE):
macro_name = line.split()[1]
while re.search(r"s+ENDs+%s"%macro_name, line, re.IGNORECASE) is None:
line = lines[num + 1]
#...do something...
num = num+1
num +=1
how this can be done in an efficient way?
python file
python file
edited Nov 19 '18 at 16:00
asked Nov 19 '18 at 15:06
M.salameh
597
597
What do you mean by to end of its name?
– toti08
Nov 19 '18 at 15:31
You should show whether you have tried anything or not.
– ahota
Nov 19 '18 at 15:47
why are you searching for "MACRO" and "END"? The beginning of a macro must exactly start with "MACRO" and you know it will end when the line starts exactly with "END". No need to search
– Sembei Norimaki
Nov 19 '18 at 16:04
is this is the efficient way? @ahota
– M.salameh
Nov 19 '18 at 16:10
add a comment |
What do you mean by to end of its name?
– toti08
Nov 19 '18 at 15:31
You should show whether you have tried anything or not.
– ahota
Nov 19 '18 at 15:47
why are you searching for "MACRO" and "END"? The beginning of a macro must exactly start with "MACRO" and you know it will end when the line starts exactly with "END". No need to search
– Sembei Norimaki
Nov 19 '18 at 16:04
is this is the efficient way? @ahota
– M.salameh
Nov 19 '18 at 16:10
What do you mean by to end of its name?
– toti08
Nov 19 '18 at 15:31
What do you mean by to end of its name?
– toti08
Nov 19 '18 at 15:31
You should show whether you have tried anything or not.
– ahota
Nov 19 '18 at 15:47
You should show whether you have tried anything or not.
– ahota
Nov 19 '18 at 15:47
why are you searching for "MACRO" and "END"? The beginning of a macro must exactly start with "MACRO" and you know it will end when the line starts exactly with "END". No need to search
– Sembei Norimaki
Nov 19 '18 at 16:04
why are you searching for "MACRO" and "END"? The beginning of a macro must exactly start with "MACRO" and you know it will end when the line starts exactly with "END". No need to search
– Sembei Norimaki
Nov 19 '18 at 16:04
is this is the efficient way? @ahota
– M.salameh
Nov 19 '18 at 16:10
is this is the efficient way? @ahota
– M.salameh
Nov 19 '18 at 16:10
add a comment |
1 Answer
1
active
oldest
votes
Assumming that you cannot nest Macros, that Macros always start with "MACRO [name]" and end with "END [name]":
# read the contents of the file
with open(file_name, "r") as f_read:
lines = f_read.readlines()
current_macro_lines =
for line in lines:
if line.startswith("MACRO"):
macro_name = line.split()[1]
# if line starts with END and refers to the current macro name
elif line.startswith("END") and line.split()[1] == macro_name:
# here the macro is complete,
#put the code you want to execute when you find the end of the macro
# then when you finish processing the lines,
# empty them so you can accumulate the next macro
current_macro_lines =
else:
# here you can accumulate the macro lines
current_macro_lines.append(line)
there is "END" line before the "END macro_name" and how can I accumulate macro lines?
– M.salameh
Nov 19 '18 at 16:13
I edited my post to handle this cases
– Sembei Norimaki
Nov 19 '18 at 16:16
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377457%2fread-file-block-by-block-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Assumming that you cannot nest Macros, that Macros always start with "MACRO [name]" and end with "END [name]":
# read the contents of the file
with open(file_name, "r") as f_read:
lines = f_read.readlines()
current_macro_lines =
for line in lines:
if line.startswith("MACRO"):
macro_name = line.split()[1]
# if line starts with END and refers to the current macro name
elif line.startswith("END") and line.split()[1] == macro_name:
# here the macro is complete,
#put the code you want to execute when you find the end of the macro
# then when you finish processing the lines,
# empty them so you can accumulate the next macro
current_macro_lines =
else:
# here you can accumulate the macro lines
current_macro_lines.append(line)
there is "END" line before the "END macro_name" and how can I accumulate macro lines?
– M.salameh
Nov 19 '18 at 16:13
I edited my post to handle this cases
– Sembei Norimaki
Nov 19 '18 at 16:16
add a comment |
Assumming that you cannot nest Macros, that Macros always start with "MACRO [name]" and end with "END [name]":
# read the contents of the file
with open(file_name, "r") as f_read:
lines = f_read.readlines()
current_macro_lines =
for line in lines:
if line.startswith("MACRO"):
macro_name = line.split()[1]
# if line starts with END and refers to the current macro name
elif line.startswith("END") and line.split()[1] == macro_name:
# here the macro is complete,
#put the code you want to execute when you find the end of the macro
# then when you finish processing the lines,
# empty them so you can accumulate the next macro
current_macro_lines =
else:
# here you can accumulate the macro lines
current_macro_lines.append(line)
there is "END" line before the "END macro_name" and how can I accumulate macro lines?
– M.salameh
Nov 19 '18 at 16:13
I edited my post to handle this cases
– Sembei Norimaki
Nov 19 '18 at 16:16
add a comment |
Assumming that you cannot nest Macros, that Macros always start with "MACRO [name]" and end with "END [name]":
# read the contents of the file
with open(file_name, "r") as f_read:
lines = f_read.readlines()
current_macro_lines =
for line in lines:
if line.startswith("MACRO"):
macro_name = line.split()[1]
# if line starts with END and refers to the current macro name
elif line.startswith("END") and line.split()[1] == macro_name:
# here the macro is complete,
#put the code you want to execute when you find the end of the macro
# then when you finish processing the lines,
# empty them so you can accumulate the next macro
current_macro_lines =
else:
# here you can accumulate the macro lines
current_macro_lines.append(line)
Assumming that you cannot nest Macros, that Macros always start with "MACRO [name]" and end with "END [name]":
# read the contents of the file
with open(file_name, "r") as f_read:
lines = f_read.readlines()
current_macro_lines =
for line in lines:
if line.startswith("MACRO"):
macro_name = line.split()[1]
# if line starts with END and refers to the current macro name
elif line.startswith("END") and line.split()[1] == macro_name:
# here the macro is complete,
#put the code you want to execute when you find the end of the macro
# then when you finish processing the lines,
# empty them so you can accumulate the next macro
current_macro_lines =
else:
# here you can accumulate the macro lines
current_macro_lines.append(line)
edited Nov 19 '18 at 16:15
answered Nov 19 '18 at 16:10
Sembei Norimaki
2,5171028
2,5171028
there is "END" line before the "END macro_name" and how can I accumulate macro lines?
– M.salameh
Nov 19 '18 at 16:13
I edited my post to handle this cases
– Sembei Norimaki
Nov 19 '18 at 16:16
add a comment |
there is "END" line before the "END macro_name" and how can I accumulate macro lines?
– M.salameh
Nov 19 '18 at 16:13
I edited my post to handle this cases
– Sembei Norimaki
Nov 19 '18 at 16:16
there is "END" line before the "END macro_name" and how can I accumulate macro lines?
– M.salameh
Nov 19 '18 at 16:13
there is "END" line before the "END macro_name" and how can I accumulate macro lines?
– M.salameh
Nov 19 '18 at 16:13
I edited my post to handle this cases
– Sembei Norimaki
Nov 19 '18 at 16:16
I edited my post to handle this cases
– Sembei Norimaki
Nov 19 '18 at 16:16
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377457%2fread-file-block-by-block-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean by to end of its name?
– toti08
Nov 19 '18 at 15:31
You should show whether you have tried anything or not.
– ahota
Nov 19 '18 at 15:47
why are you searching for "MACRO" and "END"? The beginning of a macro must exactly start with "MACRO" and you know it will end when the line starts exactly with "END". No need to search
– Sembei Norimaki
Nov 19 '18 at 16:04
is this is the efficient way? @ahota
– M.salameh
Nov 19 '18 at 16:10