So, I'm new to python and still learning. I am writing a program that would allow a user to input keywords and search a input file for these keywords, then save the results to an output file.
So far, I can get it to do the search, but it is only returning certain lines from positive hits instead of all the lines. I originally thought this was because the of the new line attached to each item in the file, but I managed to strip that away and it is still not providing the correct information in the output file.
So the input file says: Temp Chat Log age sex location ? sex age phone number words bob Things things thing 09/03/2018
The keyword search is: yes, bob, thing, sara, more, location, ?
The output file is only getting this return: Things things thing
I have tried removing the '\n', I have tried saving to a new file just in case there was something wrong with the original one I'm overwriting, I have tried re.search and re.replace, I've tried making a function but this is the closest I've gotten to getting the program to do this piece. The problem, I think, is with the last section of code, but I'm providing code for all pieces used in that last section. In the final section there are extra print lines because I was trying to see what was happening with the code as it progressed through this section of the program.
# Ask user for the path to the text file they would like to use
print ("Please provide a valid path to the chat log text (.txt) file. Example: C:\ Users \ USERNAME \ Documents")
# Save the path from the user input
path1 = input ()
# Checking the user input for the .txt file extension
if os.path.splitext(path1)[1] != ".txt":
print (path1 + " is an unknown file format. Please provide a .txt file.")
path1 = input()
else:
print ("Thank you for providing a text file.")
# Using a while loop to ensure this is a valid path
while not os.path.isfile(path1):
print ("Please provide a valid path to the chat log text (.txt) file.")
path1 = input ()
print ("File was successfully retrieved.")
# Ask user for needed keywords or symbols
user_keywords = input("What keywords or special symbols would you like to search the provided file for?\n"
"Please separate each entry with a comma.\nIf you would like to just search for question marks,"
" please just type n.\n")
# Holding list, using comma as a way to separate the given words and symbols
list1 = [s.lower() for s in user_keywords.split(',')]
# Print list for user to see
print("You have entered the following keywords and/or special symbols: ", list1)
# Opens a new file for saving the results to.
print("Please list the path you would like the new file to save to. Example: C:\ Users \ NAME \Desktop\File name.")
outFileName = input()
# Opens the file under review.
with open(path1,'r') as f, open(outFileName, 'w') as w:
f = f.readlines()
f[:] = [f.rstrip('\n') for f in f]
for line in f:
if any(s in line.lower() for s in list1):
print(f)
print(list1)
print(line)
w.write(line)
w.close()
No error messages. Expected Output New file at given output file path, with the positive result lines listed. So in this case, the file should have these lines on it. bob Things things thing location ?
Current Output The only positive return that is happening is: Things thing thing
Aucun commentaire:
Enregistrer un commentaire