I'm trying to read all lines of a file that are either all upper or all lower case.
If file.txt
contains:
Rememberr 8? when you
. Tellingmee THAT one
didntrememberthat
onethingtoday
I would want it to read:
didntrememberthat
ONETHINGTODAY
So far I have:
def function(file_name):
data=[]
f=open(file_name,'r')
lines=f.readlines()
for k in lines:
single=tuple(k)
for j in single:
j=str(j)
if j.isupper() or j.islower() == False:
lines=f.readlines()[j:]
Then i get this:
lines=f.readlines()[j:]
TypeError: slice indices must be integers or None or have an __index__ method
which makes sense because j
isn't an integer. But how would i find the position of j
when it encounters the if
-statement?
If there is an easier way to do it that would be awesome
def homogeneous_lines (file_name):
with open(file_name) as fd:
for line in fd:
if line.islower() or line.isupper():
yield line
This function reads through every line in the file. Then for every line checks to see if the line is all upper or lower case.
Finally we yield the line.
EDIT - Changed to incorporate using with statement, use builtin islower() and isupper() for strings, and made into a generator.