Python - deleting variables from text-data using regex
I am using python. I want to delete the line of a file which contains varaibles (input_numb ) the user can put in.
input_numb = ['13', '04'] ## These are variable (depending on what the user puts in)
The variables are only supposed to be deleted if they a written with no other text in a line. If there is some text before or after it they are not to be deleted. I tried it with the regular expression below (line ) but it still returns all 13 and 04.
line = line.replace('^'+input_numb+'$', '')
Here is an example of the data:
13
Some text is good.
The text has 13 lines
13 is a nice number.
13
Some text.
Some more text 04.
04.
Some text.
The output data is which:
Some text is good.
The text has 13 lines
13 is a nice number.
Some text.
Some more text 04.
Some text.
python regex list variables
add a comment |
I am using python. I want to delete the line of a file which contains varaibles (input_numb ) the user can put in.
input_numb = ['13', '04'] ## These are variable (depending on what the user puts in)
The variables are only supposed to be deleted if they a written with no other text in a line. If there is some text before or after it they are not to be deleted. I tried it with the regular expression below (line ) but it still returns all 13 and 04.
line = line.replace('^'+input_numb+'$', '')
Here is an example of the data:
13
Some text is good.
The text has 13 lines
13 is a nice number.
13
Some text.
Some more text 04.
04.
Some text.
The output data is which:
Some text is good.
The text has 13 lines
13 is a nice number.
Some text.
Some more text 04.
Some text.
python regex list variables
1
.replaceonly supports plain string substitutions. Usere.subto use a regex one. Something likeline = re.sub('(?m)^(?:{})$'.format("|".join([re.escape(x) for x in input_numb])), '', line).
– Wiktor Stribiżew
Nov 13 '18 at 11:25
Note that04.is not alone, there is a.. What are the real requirements? Please show the whole relevant code snippet (how are you reading in the data?). If you read a file line by line and write only valid lines, and if you allow any chars other than digits after the number, try ideone.com/bCM6F7.
– Wiktor Stribiżew
Nov 13 '18 at 11:35
the 04. was a typing error, sorry.
– Mady
Nov 13 '18 at 11:44
Ok, so, are you reading line by line?rx = re.compile(r'^(?:{})$'.format("|".join(input_numb)))withif not rx.search(line)should work then, see ideone.com/ranwjT
– Wiktor Stribiżew
Nov 13 '18 at 11:47
Does that help?
– Wiktor Stribiżew
Nov 13 '18 at 12:46
add a comment |
I am using python. I want to delete the line of a file which contains varaibles (input_numb ) the user can put in.
input_numb = ['13', '04'] ## These are variable (depending on what the user puts in)
The variables are only supposed to be deleted if they a written with no other text in a line. If there is some text before or after it they are not to be deleted. I tried it with the regular expression below (line ) but it still returns all 13 and 04.
line = line.replace('^'+input_numb+'$', '')
Here is an example of the data:
13
Some text is good.
The text has 13 lines
13 is a nice number.
13
Some text.
Some more text 04.
04.
Some text.
The output data is which:
Some text is good.
The text has 13 lines
13 is a nice number.
Some text.
Some more text 04.
Some text.
python regex list variables
I am using python. I want to delete the line of a file which contains varaibles (input_numb ) the user can put in.
input_numb = ['13', '04'] ## These are variable (depending on what the user puts in)
The variables are only supposed to be deleted if they a written with no other text in a line. If there is some text before or after it they are not to be deleted. I tried it with the regular expression below (line ) but it still returns all 13 and 04.
line = line.replace('^'+input_numb+'$', '')
Here is an example of the data:
13
Some text is good.
The text has 13 lines
13 is a nice number.
13
Some text.
Some more text 04.
04.
Some text.
The output data is which:
Some text is good.
The text has 13 lines
13 is a nice number.
Some text.
Some more text 04.
Some text.
python regex list variables
python regex list variables
asked Nov 13 '18 at 11:24
MadyMady
1389
1389
1
.replaceonly supports plain string substitutions. Usere.subto use a regex one. Something likeline = re.sub('(?m)^(?:{})$'.format("|".join([re.escape(x) for x in input_numb])), '', line).
– Wiktor Stribiżew
Nov 13 '18 at 11:25
Note that04.is not alone, there is a.. What are the real requirements? Please show the whole relevant code snippet (how are you reading in the data?). If you read a file line by line and write only valid lines, and if you allow any chars other than digits after the number, try ideone.com/bCM6F7.
– Wiktor Stribiżew
Nov 13 '18 at 11:35
the 04. was a typing error, sorry.
– Mady
Nov 13 '18 at 11:44
Ok, so, are you reading line by line?rx = re.compile(r'^(?:{})$'.format("|".join(input_numb)))withif not rx.search(line)should work then, see ideone.com/ranwjT
– Wiktor Stribiżew
Nov 13 '18 at 11:47
Does that help?
– Wiktor Stribiżew
Nov 13 '18 at 12:46
add a comment |
1
.replaceonly supports plain string substitutions. Usere.subto use a regex one. Something likeline = re.sub('(?m)^(?:{})$'.format("|".join([re.escape(x) for x in input_numb])), '', line).
– Wiktor Stribiżew
Nov 13 '18 at 11:25
Note that04.is not alone, there is a.. What are the real requirements? Please show the whole relevant code snippet (how are you reading in the data?). If you read a file line by line and write only valid lines, and if you allow any chars other than digits after the number, try ideone.com/bCM6F7.
– Wiktor Stribiżew
Nov 13 '18 at 11:35
the 04. was a typing error, sorry.
– Mady
Nov 13 '18 at 11:44
Ok, so, are you reading line by line?rx = re.compile(r'^(?:{})$'.format("|".join(input_numb)))withif not rx.search(line)should work then, see ideone.com/ranwjT
– Wiktor Stribiżew
Nov 13 '18 at 11:47
Does that help?
– Wiktor Stribiżew
Nov 13 '18 at 12:46
1
1
.replace only supports plain string substitutions. Use re.sub to use a regex one. Something like line = re.sub('(?m)^(?:{})$'.format("|".join([re.escape(x) for x in input_numb])), '', line).– Wiktor Stribiżew
Nov 13 '18 at 11:25
.replace only supports plain string substitutions. Use re.sub to use a regex one. Something like line = re.sub('(?m)^(?:{})$'.format("|".join([re.escape(x) for x in input_numb])), '', line).– Wiktor Stribiżew
Nov 13 '18 at 11:25
Note that
04. is not alone, there is a .. What are the real requirements? Please show the whole relevant code snippet (how are you reading in the data?). If you read a file line by line and write only valid lines, and if you allow any chars other than digits after the number, try ideone.com/bCM6F7.– Wiktor Stribiżew
Nov 13 '18 at 11:35
Note that
04. is not alone, there is a .. What are the real requirements? Please show the whole relevant code snippet (how are you reading in the data?). If you read a file line by line and write only valid lines, and if you allow any chars other than digits after the number, try ideone.com/bCM6F7.– Wiktor Stribiżew
Nov 13 '18 at 11:35
the 04. was a typing error, sorry.
– Mady
Nov 13 '18 at 11:44
the 04. was a typing error, sorry.
– Mady
Nov 13 '18 at 11:44
Ok, so, are you reading line by line?
rx = re.compile(r'^(?:{})$'.format("|".join(input_numb))) with if not rx.search(line) should work then, see ideone.com/ranwjT– Wiktor Stribiżew
Nov 13 '18 at 11:47
Ok, so, are you reading line by line?
rx = re.compile(r'^(?:{})$'.format("|".join(input_numb))) with if not rx.search(line) should work then, see ideone.com/ranwjT– Wiktor Stribiżew
Nov 13 '18 at 11:47
Does that help?
– Wiktor Stribiżew
Nov 13 '18 at 12:46
Does that help?
– Wiktor Stribiżew
Nov 13 '18 at 12:46
add a comment |
1 Answer
1
active
oldest
votes
I suggest omitting all lines that fully match your input_numb list items.
Compile the following regex:
rx = re.compile(r'^(?:{})$'.format("|".join(input_numb)))
And then test each line for the pattern match, and if there is no match, write the line to the new file, else, do nothing:
if not rx.search(line):
fw.write(line) # where fw is the file handle where output is written
See the Python demo online.
In some cases, you might probably want to strip the line from the trailing whitespace, if not rx.search(line.rstrip()):.
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%2f53279980%2fpython-deleting-variables-from-text-data-using-regex%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
I suggest omitting all lines that fully match your input_numb list items.
Compile the following regex:
rx = re.compile(r'^(?:{})$'.format("|".join(input_numb)))
And then test each line for the pattern match, and if there is no match, write the line to the new file, else, do nothing:
if not rx.search(line):
fw.write(line) # where fw is the file handle where output is written
See the Python demo online.
In some cases, you might probably want to strip the line from the trailing whitespace, if not rx.search(line.rstrip()):.
add a comment |
I suggest omitting all lines that fully match your input_numb list items.
Compile the following regex:
rx = re.compile(r'^(?:{})$'.format("|".join(input_numb)))
And then test each line for the pattern match, and if there is no match, write the line to the new file, else, do nothing:
if not rx.search(line):
fw.write(line) # where fw is the file handle where output is written
See the Python demo online.
In some cases, you might probably want to strip the line from the trailing whitespace, if not rx.search(line.rstrip()):.
add a comment |
I suggest omitting all lines that fully match your input_numb list items.
Compile the following regex:
rx = re.compile(r'^(?:{})$'.format("|".join(input_numb)))
And then test each line for the pattern match, and if there is no match, write the line to the new file, else, do nothing:
if not rx.search(line):
fw.write(line) # where fw is the file handle where output is written
See the Python demo online.
In some cases, you might probably want to strip the line from the trailing whitespace, if not rx.search(line.rstrip()):.
I suggest omitting all lines that fully match your input_numb list items.
Compile the following regex:
rx = re.compile(r'^(?:{})$'.format("|".join(input_numb)))
And then test each line for the pattern match, and if there is no match, write the line to the new file, else, do nothing:
if not rx.search(line):
fw.write(line) # where fw is the file handle where output is written
See the Python demo online.
In some cases, you might probably want to strip the line from the trailing whitespace, if not rx.search(line.rstrip()):.
answered Nov 13 '18 at 12:59
Wiktor StribiżewWiktor Stribiżew
310k16131206
310k16131206
add a comment |
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.
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%2f53279980%2fpython-deleting-variables-from-text-data-using-regex%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
1
.replaceonly supports plain string substitutions. Usere.subto use a regex one. Something likeline = re.sub('(?m)^(?:{})$'.format("|".join([re.escape(x) for x in input_numb])), '', line).– Wiktor Stribiżew
Nov 13 '18 at 11:25
Note that
04.is not alone, there is a.. What are the real requirements? Please show the whole relevant code snippet (how are you reading in the data?). If you read a file line by line and write only valid lines, and if you allow any chars other than digits after the number, try ideone.com/bCM6F7.– Wiktor Stribiżew
Nov 13 '18 at 11:35
the 04. was a typing error, sorry.
– Mady
Nov 13 '18 at 11:44
Ok, so, are you reading line by line?
rx = re.compile(r'^(?:{})$'.format("|".join(input_numb)))withif not rx.search(line)should work then, see ideone.com/ranwjT– Wiktor Stribiżew
Nov 13 '18 at 11:47
Does that help?
– Wiktor Stribiżew
Nov 13 '18 at 12:46