Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think your question is good, and when you're done, you should head over to <a href="https://codereview.stackexchange.com/">code review</a> and get your code looked at for other things I've noticed:</p> <pre><code># the txt file I'm trying to extract last words from and write strings into a file #Hello:there:buddy #How:areyou:doing #I:amFine:thanks #thats:good:I:guess </code></pre> <p>First off, thanks for putting example file contents at the top of your question. </p> <pre><code>x = raw_input("Enter the full path + file name + file extension you wish to use: ") </code></pre> <p>I don't think this part is neccessary. You can just create a better parameter for <code>ripple</code> than <code>x</code>. I think <code>file_loc</code> is a pretty standard one.</p> <pre><code>def ripple(x): with open(x) as file: </code></pre> <p>With <code>open</code>, you are able to mark the operation happening to the file. I also like to name my file object according to its job. In other words, <code>with open(file_loc, 'r') as r:</code> reminds me that <code>r.foo</code> is going to be my file that is being read from.</p> <pre><code> for line in file: for word in line.split(): if ':' in word: </code></pre> <p>First off, your <code>for word in line.split()</code> statement does nothing but put the "Hello:there:buddy" string into a list: <code>["Hello:there:buddy"]</code>. A better idea would be to pass <code>split</code> an argument, which does more or less what you're trying to do here. For example, <code>"Hello:there:buddy".split(":")</code> would output <code>['Hello', 'there', 'buddy']</code>, making your search for colons an accomplished task.</p> <pre><code> try: print word.split(':')[-1] except (IndexError): pass </code></pre> <p>Another advantage is that you won't need to check for an <code>IndexError</code>, since you'll have, at least, an empty string, which when split, comes back as an empty string. In other words, it'll write nothing for that line.</p> <pre><code>ripple(x) </code></pre> <p>For <code>ripple(x)</code>, you would instead call <code>ripple('/home/user/sometext.txt')</code>.</p> <p>So, try looking over this, and explore code review. There's a guy named <a href="https://codereview.stackexchange.com/users/1659/winston-ewert">Winston</a> who does really awesome work with Python and self-described newbies. I always pick up new tricks from that guy.</p> <p>Here is my take on it, re-written out:</p> <pre><code>import os #for renaming the output file def ripple(file_loc='/typical/location/while/developing.txt'): outfile = "output.".join(os.path.basename(file_loc).split('.')) with open(outfile, 'w') as w: lines = open(file_loc, 'r').readlines() #everything is one giant list w.write('\n'.join([line.split(':')[-1] for line in lines])) ripple() </code></pre> <p>Try breaking this down, line by line, and changing things around. It's pretty condensed, but once you pick up comprehensions and using lists, it'll be more natural to read code this way.</p>
 

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