Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Both TkTech and abarnet have some good advice in terms of how to open the file and save the return values. Automatic clean up is nice.</p> <p>There is a logical issue afoot though. In the example sentence you gave, "0 is less than 1" that single line has both a "0" and a "1" in it. If you were to send that line through either function, it would modify the line to be "ZERO is less than 1" not your intended "ZERO is less than ONE." The reason for this is because the first if statement would catch the "0" but the use of elif or even else would indicate to the program "you already found the first condition, don't bother checking the rest." Since you want to catch any 1's regardless of whether or not any 0's exist, you shouldn't use any sort of else statement.</p> <p>Here is how I would write it:</p> <pre><code>def convert(file): in_file = open(file, "r") out_file = open("/home/user/temp/converted.txt", "w+") # Read up on what r and w+ mean; docs.python.org for line in in_file: # Catch 0 in line if "0" in line: line = line.replace("0", "ZERO") # Catch 1 in line if "1" in line: line = line.replace("1", "ONE") # Write line to file out_file.write(line) #end for # Close open files; this is common practice in C # Not needed in Python when using "with open(file, 'r') as filename:" in_file.close() # Force it to write out anything left in buffer; not usually needed as Python usually does this when calling close() out_file.flush() out_file.close() </code></pre> <p>There are a number of improvements that could be made to my code, in addition to improvements that could be made depending on the format of the intended input file.</p> <ol> <li>I could use TkTech's suggestion and then I wouldn't need to close anything at the end.</li> <li>If I wanted to look for more than just a couple things I could use a dictionary or other data structure to store all the intended changes.</li> </ol> <p>It does, however work in it's current state taking this:</p> <pre><code>0 is less than 1 1 is greater than 0 0 1 1 0 </code></pre> <p>and giving me this:</p> <pre><code>ZERO is less than ONE ONE is greater than ZERO ZERO ONE ONE ZERO </code></pre> <p>A file more complex than this may give you unintended results, such as:</p> <pre><code>0 is less than 1 but larger than -1 ==&gt; ZERO is less than ONE but larger than -ONE 0 is larger than -1 ==&gt; ZERO is larger than -ONE 2 is larger than 0 ==&gt; 2 is larger than ZERO 0 1 2 3 4 5 6 7 8 9 10 ==&gt; ZERO ONE 2 3 4 5 6 7 8 9 ZEROONE 0000001 ==&gt; ZEROZEROZEROZEROZEROZEROONE 1001101 ==&gt; ONEZEROZEROONEONEZEROONE </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload