How do I prevent line.replace from removing extra lines?How can I represent an 'Enum' in Python?How to flush output of print function?python3 stdin/file object with For LoopReplace a text in a text file in pythonHow to remove a line break from CSV outputWrite RegEx results from multiple html files to .txt outfileHow to remove extra semicolons from the end of some lines in a txtRemove grid lines(vertical and horizontal) from a handwritten scanned imageHow to remove '' and '' characters from string after reading from txt file?remove a line from file which only contains new line

Where did the extra Pym particles come from in Endgame?

How to set the font color of quantity objects (Version 11.3 vs version 12)?

Do I have to worry about players making “bad” choices on level up?

What are the spoon bit of a spoon and fork bit of a fork called?

Phrase for the opposite of "foolproof"

Is GOCE a satellite or aircraft?

Lock in SQL Server and Oracle

Any examples of headwear for races with animal ears?

Do I have an "anti-research" personality?

Upright [...] in italics quotation

Asahi Dry Black beer can

How to figure out whether the data is sample data or population data apart from the client's information?

Why do Ichisongas hate elephants and hippos?

Toggle Overlays shortcut?

In Proverbs 14:34, is sin a disgrace to a people, or is mercy a sin-offering?

You look catfish vs You look like a catfish

Why is the origin of “threshold” uncertain?

Were there two appearances of Stan Lee?

Feels like I am getting dragged in office politics

Can a creature tell when it has been affected by a Divination wizard's Portent?

Electric guitar: why such heavy pots?

Packing rectangles: Does rotation ever help?

Why does nature favour the Laplacian?

Help, my Death Star suffers from Kessler syndrome!



How do I prevent line.replace from removing extra lines?


How can I represent an 'Enum' in Python?How to flush output of print function?python3 stdin/file object with For LoopReplace a text in a text file in pythonHow to remove a line break from CSV outputWrite RegEx results from multiple html files to .txt outfileHow to remove extra semicolons from the end of some lines in a txtRemove grid lines(vertical and horizontal) from a handwritten scanned imageHow to remove '' and '' characters from string after reading from txt file?remove a line from file which only contains new line






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I want to eliminate a line in a file, but when I do a line.replace function, it replaces things that shouldn't be replaced.



I tried line.replace to remove the "SCOPE1EXPRESSION" line, but it removed additional lines that I want to keep.



The input file looks like this:



SCOPE1EXPRESSION
InDifferentialPairClass'DIFF100'
TOPLAYER_PREFGAP
6.2992mil
BOTTOMLAYER_PREFGAP
6.2992mil
SCOPE1EXPRESSION
InDifferentialPairClass'DIFF90'
TOPLAYER_PREFGAP
4.4882mil
BOTTOMLAYER_PREFGAP
4.4882mil


Then I try line.replace("SCOPE1EXPRESSION","") to remove it:



'''
infile = open(('All_Rules1.txt'),'r')
outfile = open('All_Rules2.txt','w+')
for line in infile:
line = line.replace("SCOPE1EXPRESSION","")
if pattern.search(line):
outfile.write(line)
infile.close()
outfile.close()
sys.stdout.flush()
'''


and the result is:



InDifferentialPairClass'DIFF100'
TOPLAYER_PREFGAP
BOTTOMLAYER_PREFGAP
InDifferentialPairClass'DIFF90'
TOPLAYER_PREFGAP
BOTTOMLAYER_PREFGAP


How can I prevent it from removing extra lines?



I expected only the "SCOPE1EXPRESSION" to be removed.










share|improve this question

















  • 1





    I don't think it's replacing it, I think that the extra lines being removed don't pass the if pattern.search(line) test and aren't written to the outfile.

    – Carter
    Mar 22 at 19:23






  • 1





    You are absolutely correct. I used the pattern.search(line) feature to create the first file (from a very large file) but didn't remove it when doing the fine adjustments. Thanks!

    – CPMAN
    Mar 22 at 19:29











  • Carter, also when I have multiple lines that have the same word but an incremented number, such as test1, test2, test3... is there a way to replace all lines (entirely) that have "test" in it? I tried test* but that didn't work.

    – CPMAN
    Mar 22 at 19:48











  • Check out re.sub

    – Carter
    Mar 22 at 20:04

















0















I want to eliminate a line in a file, but when I do a line.replace function, it replaces things that shouldn't be replaced.



I tried line.replace to remove the "SCOPE1EXPRESSION" line, but it removed additional lines that I want to keep.



The input file looks like this:



SCOPE1EXPRESSION
InDifferentialPairClass'DIFF100'
TOPLAYER_PREFGAP
6.2992mil
BOTTOMLAYER_PREFGAP
6.2992mil
SCOPE1EXPRESSION
InDifferentialPairClass'DIFF90'
TOPLAYER_PREFGAP
4.4882mil
BOTTOMLAYER_PREFGAP
4.4882mil


Then I try line.replace("SCOPE1EXPRESSION","") to remove it:



'''
infile = open(('All_Rules1.txt'),'r')
outfile = open('All_Rules2.txt','w+')
for line in infile:
line = line.replace("SCOPE1EXPRESSION","")
if pattern.search(line):
outfile.write(line)
infile.close()
outfile.close()
sys.stdout.flush()
'''


and the result is:



InDifferentialPairClass'DIFF100'
TOPLAYER_PREFGAP
BOTTOMLAYER_PREFGAP
InDifferentialPairClass'DIFF90'
TOPLAYER_PREFGAP
BOTTOMLAYER_PREFGAP


How can I prevent it from removing extra lines?



I expected only the "SCOPE1EXPRESSION" to be removed.










share|improve this question

















  • 1





    I don't think it's replacing it, I think that the extra lines being removed don't pass the if pattern.search(line) test and aren't written to the outfile.

    – Carter
    Mar 22 at 19:23






  • 1





    You are absolutely correct. I used the pattern.search(line) feature to create the first file (from a very large file) but didn't remove it when doing the fine adjustments. Thanks!

    – CPMAN
    Mar 22 at 19:29











  • Carter, also when I have multiple lines that have the same word but an incremented number, such as test1, test2, test3... is there a way to replace all lines (entirely) that have "test" in it? I tried test* but that didn't work.

    – CPMAN
    Mar 22 at 19:48











  • Check out re.sub

    – Carter
    Mar 22 at 20:04













0












0








0








I want to eliminate a line in a file, but when I do a line.replace function, it replaces things that shouldn't be replaced.



I tried line.replace to remove the "SCOPE1EXPRESSION" line, but it removed additional lines that I want to keep.



The input file looks like this:



SCOPE1EXPRESSION
InDifferentialPairClass'DIFF100'
TOPLAYER_PREFGAP
6.2992mil
BOTTOMLAYER_PREFGAP
6.2992mil
SCOPE1EXPRESSION
InDifferentialPairClass'DIFF90'
TOPLAYER_PREFGAP
4.4882mil
BOTTOMLAYER_PREFGAP
4.4882mil


Then I try line.replace("SCOPE1EXPRESSION","") to remove it:



'''
infile = open(('All_Rules1.txt'),'r')
outfile = open('All_Rules2.txt','w+')
for line in infile:
line = line.replace("SCOPE1EXPRESSION","")
if pattern.search(line):
outfile.write(line)
infile.close()
outfile.close()
sys.stdout.flush()
'''


and the result is:



InDifferentialPairClass'DIFF100'
TOPLAYER_PREFGAP
BOTTOMLAYER_PREFGAP
InDifferentialPairClass'DIFF90'
TOPLAYER_PREFGAP
BOTTOMLAYER_PREFGAP


How can I prevent it from removing extra lines?



I expected only the "SCOPE1EXPRESSION" to be removed.










share|improve this question














I want to eliminate a line in a file, but when I do a line.replace function, it replaces things that shouldn't be replaced.



I tried line.replace to remove the "SCOPE1EXPRESSION" line, but it removed additional lines that I want to keep.



The input file looks like this:



SCOPE1EXPRESSION
InDifferentialPairClass'DIFF100'
TOPLAYER_PREFGAP
6.2992mil
BOTTOMLAYER_PREFGAP
6.2992mil
SCOPE1EXPRESSION
InDifferentialPairClass'DIFF90'
TOPLAYER_PREFGAP
4.4882mil
BOTTOMLAYER_PREFGAP
4.4882mil


Then I try line.replace("SCOPE1EXPRESSION","") to remove it:



'''
infile = open(('All_Rules1.txt'),'r')
outfile = open('All_Rules2.txt','w+')
for line in infile:
line = line.replace("SCOPE1EXPRESSION","")
if pattern.search(line):
outfile.write(line)
infile.close()
outfile.close()
sys.stdout.flush()
'''


and the result is:



InDifferentialPairClass'DIFF100'
TOPLAYER_PREFGAP
BOTTOMLAYER_PREFGAP
InDifferentialPairClass'DIFF90'
TOPLAYER_PREFGAP
BOTTOMLAYER_PREFGAP


How can I prevent it from removing extra lines?



I expected only the "SCOPE1EXPRESSION" to be removed.







python-3.x






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 22 at 19:19









CPMANCPMAN

376




376







  • 1





    I don't think it's replacing it, I think that the extra lines being removed don't pass the if pattern.search(line) test and aren't written to the outfile.

    – Carter
    Mar 22 at 19:23






  • 1





    You are absolutely correct. I used the pattern.search(line) feature to create the first file (from a very large file) but didn't remove it when doing the fine adjustments. Thanks!

    – CPMAN
    Mar 22 at 19:29











  • Carter, also when I have multiple lines that have the same word but an incremented number, such as test1, test2, test3... is there a way to replace all lines (entirely) that have "test" in it? I tried test* but that didn't work.

    – CPMAN
    Mar 22 at 19:48











  • Check out re.sub

    – Carter
    Mar 22 at 20:04












  • 1





    I don't think it's replacing it, I think that the extra lines being removed don't pass the if pattern.search(line) test and aren't written to the outfile.

    – Carter
    Mar 22 at 19:23






  • 1





    You are absolutely correct. I used the pattern.search(line) feature to create the first file (from a very large file) but didn't remove it when doing the fine adjustments. Thanks!

    – CPMAN
    Mar 22 at 19:29











  • Carter, also when I have multiple lines that have the same word but an incremented number, such as test1, test2, test3... is there a way to replace all lines (entirely) that have "test" in it? I tried test* but that didn't work.

    – CPMAN
    Mar 22 at 19:48











  • Check out re.sub

    – Carter
    Mar 22 at 20:04







1




1





I don't think it's replacing it, I think that the extra lines being removed don't pass the if pattern.search(line) test and aren't written to the outfile.

– Carter
Mar 22 at 19:23





I don't think it's replacing it, I think that the extra lines being removed don't pass the if pattern.search(line) test and aren't written to the outfile.

– Carter
Mar 22 at 19:23




1




1





You are absolutely correct. I used the pattern.search(line) feature to create the first file (from a very large file) but didn't remove it when doing the fine adjustments. Thanks!

– CPMAN
Mar 22 at 19:29





You are absolutely correct. I used the pattern.search(line) feature to create the first file (from a very large file) but didn't remove it when doing the fine adjustments. Thanks!

– CPMAN
Mar 22 at 19:29













Carter, also when I have multiple lines that have the same word but an incremented number, such as test1, test2, test3... is there a way to replace all lines (entirely) that have "test" in it? I tried test* but that didn't work.

– CPMAN
Mar 22 at 19:48





Carter, also when I have multiple lines that have the same word but an incremented number, such as test1, test2, test3... is there a way to replace all lines (entirely) that have "test" in it? I tried test* but that didn't work.

– CPMAN
Mar 22 at 19:48













Check out re.sub

– Carter
Mar 22 at 20:04





Check out re.sub

– Carter
Mar 22 at 20:04












0






active

oldest

votes












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%2f55306474%2fhow-do-i-prevent-line-replace-from-removing-extra-lines%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















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%2f55306474%2fhow-do-i-prevent-line-replace-from-removing-extra-lines%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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript