-
-
Save tombrad/4697060 to your computer and use it in GitHub Desktop.
fname = raw_input("Enter file name: ") | |
fh = open(fname) | |
lst = list() # list for the desired output | |
for line in fh: # to read every line of file romeo.txt | |
word= line.rstrip().split() # to eliminate the unwanted blanks and turn the line into a list of words | |
for element in word: # check every element in word | |
if element in lst: # if element is repeated | |
continue # do nothing | |
else : # else if element is not in the list | |
lst.append(element) # append | |
lst.sort() # sort the list (de-indent indicates that you sort when the loop ends) | |
print lst # print the list |
fname = input("Enter file name: ")
fname='romeo.txt'
fh = open(fname)
lst = list()
for line in fh:
word_split=line.split()
for w in word_split:
if w not in lst:
lst.append(w)
else:
continue
lst.sort()
print(lst)
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
temp=list()
count=0
for line in fh:
if line.startswith("From "):
words=line.split()
lst.append(words[1:2])
for element in lst:
for x in element:
print(x)
count+=1
print("There were", count, "lines in the file with From as the first word")
fname = input("Enter file name: ")
fh = open(fname)
lst = list()
lst2 = []
for line in fh:
q=line.rstrip()
w=q.split()
for word in w:
if word not in lst:
lst.append(word)
else:
continue
#print(q)
#print(lst2)
lst.sort()
print(lst)
#print()
I don't know why when I run the code I get none, help me.
fname = "8.4.txt"
fh = open(fname)
fh = fh.read().split()
lst = []
indice = len(fh)
for i in range(indice):
if fh[i] not in lst:
lst.append(fh[i])
lst = lst.sort()
print(lst)
fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
word= line.rstrip().split()
for element in word:
if element in lst:
continue
else :
lst.append(element)
lst.sort()
print(lst)
fname = input("Enter file name: ")
try :
fh = open(fname)
except:
print("File Does not Exist!")
lst = list()
for line in fh:
words = line.split()
for word in words:
if word in lst: continue
lst.append(word)
lst.sort()
print(lst)
fname = input("Enter the file name:")
fhandle = open(fname)
words = list()
for line in fhandle:
line = line.split()
[words.append(i) for i in line if i not in words] // without the need of 'for' loop
words.sort()
print(words)