Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
im new to python (I started yesterday) and wanted to make a program, in which you can enter a name, and then searches for the name in a text file and displays if the name exists or not. If I run the program and enter a existing name, it still shows "no" and I have no idea why. Can anyone help me out?
Here's a small code snippet which performs this logic:
def find_name_in_file(name, filename):
with open(filename, "r") as fd:
for line in fd.readlines():
if name in line:
return True # or print "YES"
return False # or print "NO"
Two things you need to keep in mind:
This code does not take into account case sensitivity, if name equals "Mark" and in the file the name is "mark" you will not find it (this can easily be resolved by using the "lower" function).
The function returns true also in cases where the file contains the word "SHMark" or "MarkSH". If you can't rely on the file having the exact name you are looking for you can add additional logic to check the word is surrounded by whitespace. I'll leave it to you to find out how to do it.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 9 months ago.
Improve this question
My for loop working when I have only one item in my list. see the code below:
remove_month_year = ["apple","banana"]
for remove_m_y in remove_month_year:
if remove_m_y not in time_posted: #I want to exceute this line of code if "apple" and "banna" not found in time_posted
....my others code
#time_posted is list of string like time_posted = "he eat apple"
But when I added more then one item I am facing the problems. Let you explain:
Assume I have two item in my list ["apple","banana"] so when I running the for loop first it will check for "apple" if apple not found then it will run my others block of code which is okay. The problems occurs when it's checking banana and found apple and my others block code running where I added logic not to run code if found apple.
It’s iterating over the array with one variable at a time, so imo you could either add a Boolean value which is changed upon one being present, followed by a conditional after the loop to check that Boolean value, or a more efficient way would be
remove_month_year = ["apple","banana"]
if(not any(remove_m_y in time_posted for remove_m_y in remove_month_year)):
....my others code
#time_posted is list of string like time_posted = "he eat apple"
Not sure if this exactly will work, but I think it should…
any returns true if any element is true, and false if all are, so this is probably perfect
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am using the print function with output to file argument. The print function is under an if statement. Below is the code
log_file = open("Src_files.log", 'w')
if count_1.equals(count_2) == False:
print('Error: Discrepancy with processed file. Count of records does not match with sources file', file=log_file)
Count_1 and count_2 are unequal dataframes
The code gets executed without throwing any error but when I check the log file, it does not contain the printed statement.
How do I correct the code?
print does not flush by default. check the python manual to find that there is a flush keyword arg, or simply close the file. log_file.close()
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Sorry if this is basic question I'm new to python and i couldn't find the proper way to do this within the api docs.
I have a list that contains stack arguments in the fashion
[push,3][push,2][push,1][pop][add]... so on
I also have a StackMachine.py file that created a class Stack:with the member functions push pop add sub mul div mod
I imported it into my main.py file and i want to go through my list and execute the arguments using the functions i made in my StackMachine.py file.
My question is how to analyze the list and use my member functions from the Stack class.
my main.py looks like
from StackMachine import Stack
import sys
toks = []
f = open(sys.argv[1])
for line in f.readlines():
tokens = line.split()
toks.append(tokens)
f.close()
It may not work because one you forgot the colon after the if statement, if i == "push": and if i == "pop":. For the push, 1 and push, 2 part you would be checking a tuple which is a data structure that is similar to a list but is immutable. So to check what the number is or what the first argument is you would do if i[0] == "push" and i[1] == 1: but before this if statement you would have to check if that object is a tuple so you would do if type = tuple:
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a .txt file that has a really long RNAm sequence. I don´t know the exact length of the sequence.
What I need to do is extract the part of the sequence that is valid, meaning it starts with "AUG" and ends in "UAA" "UAG" or "UGA". Since the sequence is too long I don´t know the index of any of the letters or where the valid sequence is.
I need to save the new sequence in another variable.
Essentially, what you need to do, without coding the whole thing for you, is:
Example string:
rnaSequence = 'ACGUAFBHUAUAUAGAAAAUGGAGAGAGAAAAUUUGGGGGGGAAAAAAUAAAAAGGGUAUAUAGAUGAGAGAGA'
You will want to find the index of the 'AUG' and the index of 'UAA', 'UAG', or 'UGA' .. Something like this
rnaStart = rnaSequence.index(begin)
Then you'll need to set the slice of the string to a new variable
rnaSubstring = rnaSequence[rnaStart:rnaEnd+3]
Which in my string above, returns:
AUGGAGAGAGAAAAUUUGGGGGGGAAAAAAUAA
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I ran into a problem while trying to put lines from a .txt file into a list. I know you get extra lines when you do this, so I used line.split() to take out the trailing lines.
When I did this, the words I was trying to read became weirdly formatted. This is what it looked like...
['word']
Do any of you know how to take out the trailing lines without having this happen?
Just read all of lines with readlines() function and then you can get the n last line with reverse indexing : lines[-n:] and as says in comment its better to call the built-in list of open file handle !
with open('test_file.txt','r') as f :
lines =list(f)
or
with open('test_file.txt','r') as f :
lines =f.readlines()