Python - deleting variables from text-data using regex












-1















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.









share|improve this question


















  • 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













  • 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))) 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
















-1















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.









share|improve this question


















  • 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













  • 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))) 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














-1












-1








-1








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.









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 11:24









MadyMady

1389




1389








  • 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













  • 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))) 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














  • 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













  • 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))) 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








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












1 Answer
1






active

oldest

votes


















0














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()):.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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()):.






    share|improve this answer




























      0














      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()):.






      share|improve this answer


























        0












        0








        0







        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()):.






        share|improve this answer













        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()):.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 12:59









        Wiktor StribiżewWiktor Stribiżew

        310k16131206




        310k16131206






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings