0

I have a file that looks like this, if docket ="3ghi" (i could be "3ghi" or 5"ghi" )in the file then condition is greater than 4 should be changed to "new" and condition <=2 should be changed to "fair" . I added my code below, the replace command works but my if loop is bad. Please help.

Input:
<code=report docket="3ghi" parse=20>
    <items="20" product="abc" condition="9">
    <items="50" product="xyz" condition="8">
    <items="" product="mno" condition="2">

Output:
<code=report docket="3ghi" parse=20>
    <items="20" product="abc" condition="new">
    <items="50" product="xyz" condition="new">
    <items="" product="mno" condition="fair">

with open(("test.txt",'r') as new:
   readin = new.read
   if "docket =3ghi" == True:
        readin.replace('condition="4-100"', 'condition="new"')
        readin.replace('condition="1-2"', 'condition="fair"')
        x.write(readin)
2
  • All non empty string will default to True. Use something like "3ghi" in block Commented Apr 4, 2019 at 16:10
  • All told, if you are parsing xml, it's best to use an xml parser like the xml which is built-in, or a third party package like lxml. Parsers make structured languages like xml and html much easier to process Commented Apr 4, 2019 at 17:32

4 Answers 4

2

First problem:

readin = new.read

You're not calling the method, you won't get the file contents in readin

Second problem:

if "docket =3ghi" == True:

You're comparing if a string is True - it's never True.

Sign up to request clarification or add additional context in comments.

5 Comments

A non-empty string is always True
@Javier str == bool is never True
@Javier That only works if you're trying to use the string itself as the condition. if string will be true is string is non-empty
Good answer, though it may be worth noting that bool can be used to convert "docket =3ghi" to a boolean so it can be evaluated as expected in the if statement.
It is worth noting all these considerations.
2

Let's break down your current statement:

if "docket = 3ghi" == True:

Non-empty strings evaluate like True, but are not exactly True. True is a boolean, so you are asking "is this string a boolean?" That is always False:

"somestr" == True
# False

Fix it to check if the string is in a part of a file. For example:

with open(("test.txt",'r') as new:
    for line in new.read(): # read in the file and iterate
         if "somestr" in line:
             # do something

Note I've also added the parens to new.read() so that you don't get exceptions like function doesn't support iteration

Comments

0

Your first problem is reading the file using the read, its a method.

new.read()

Secondly, you're checking if

"docket=3ghi" 

exist in the file, thats not how its done. You ought to have iterated first e.g

for i in reading:
    if "your string" in i:
        print("Put your action here")

Thats it.

Comments

0

You need to correctly call the method to have the whole content of the file in readin. Now you can replace twice checking also correctly the condition

with open('test.txt', 'r') as new:
    readin = new.read()
    if 'docket="3ghi"' in readin:
        readin = readin.replace('"I"', '"new"').replace('"II"', '"fair"')
        # save or print or do whatever you want with readin

4 Comments

How can i edit this to replace anything above 2 to "fair" . for example <items="20" product="abc" condition="2">
What above 2? items?
If the condition is above 2, im sorry for not being clear, I updated the question to reflect my explanation
You can still concatenate more .replace(). Not a very elegant or eficient solution if you have too many more numbers, though. In that case, you should explore doing a substitution using regular expressions rather than replace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.