I'm a beginner with python, and I'm trying to learn through google and some books... I'm working on a specific project and doing ok with it so far...
The first part of my program takes an input text file, and scans it for certain data within the lines, it then writes the line back out to a new file if it doesn't satisfy the search criteria...
What I've done is ugly as hell, but it's also very slow... When I run it on a Raspberry Pi, this part takes 4 seconds alone (input file is just over 1700 lines of text)
Here's my effort:
with open('mirror2.txt', mode='r') as fo:
lines = fo.readlines()
with open('temp/data.txt', mode='w') as of:
for line in lines:
date = 0
page = 0
dash = 0
empty = 0
if "Date" in line: date += 1
if "Page" in line: page += 1
if "----" in line: dash += 1
if line == "\n": empty += 1
sum = date + page + dash + empty
if sum == 0:
of.write(line)
else:()
I'm embarrassed to show that in public, but I'd love to see a 'pythonic' way to do it more elegantly (and quicker!)
Anyone help?
sum, Python already does as built-in. How abouttotalinstead?elif, that way not all of theifs will be tested after one of them is true. Also, What's with theelse:()part?