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()
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 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.
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 2 years ago.
Improve this question
I need to read config.conf file in Python using ConfigParser. The thing I'm trying to do is, club $dataDir variable with a fileName and can't get expected output as given below.
config.conf
[dir]
dataDir = /home/srujan/Documents/r2d2/data-cut/SLIM/postcut
[files]
current_materiel = ${{dataDir}} + /20201209-rtvm_api_current_materiel_not_filtered_OS.csv
Expected output:
/home/srujan/Documents/r2d2/data-cut/SLIM/postcut/20201209-rtvm_api_current_materiel_not_filtered_OS.csv
Debug results - I get as a text instead of dataDir path.
${{dataDir}} + /20201209-rtvm_api_current_materiel_not_filtered_OS.csv
You are looking for https://docs.python.org/3/library/configparser.html#interpolation-of-values.
The default interpolation only allows use of variables from the same section with %(varname) syntax. If you want variables to persist across multiple sections, you need to use the extended interpolation
parser = configparser.ConfigParser(..., interpolation=configparser.ExtendedInterpolation())
Extended interpolation uses ${section:option} to denote a value from a foreign section.
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
I am new to python and OOPS.I am expecting my module add_book to increment if book is already present in dictionary. Please help me .Not sure why for loop is not working as expected.
https://github.com/amitsuneja/Bookstore/commit/4aefb378171ac326aacb35f355051bc0b057d3be
You should not append to the list while you are still iterating it. Also, your code will append the new item for each item already in the list that has a different name. Instead, you should use a for/else loop. Here, the else case will only be triggered if you do not break from the loop.
for recordlist in self.mybooksinventory:
if self.name == recordlist['name']:
recordlist['quantity'] += 1
break # break from the loop
else: # for/else, not if/else !
self.mybooksinventory.append({'name':self.name,'stuclass':self.stuclass,'subject':self.subject,'quantity':1})
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've been writing this program for encryption of messages by ceasar's method, but i have a major problem with this error. it is supposed to change the string b into c and c has to be encrypted. however nothing shows up in tkinter.i have seen several similar question regarding this error but none seemed to have any connection to this case.could someone help??here is the code.
def encrypt(event):
global top,c,root,e
a=e.get()
b=l.get()
top.destroy()
c=''
mystring=StringVar()
mystring.set(c)
for i in b:
if ord(i)in range(65,91) or ord(i) in range(97,123):
if ((ord(i)+a%26)>90 and ord(i)<=90) or (ord(i)+a%26)>122:
c=c+chr(ord(i)+a%26-26)
else:
c=c+chr(ord(i)+a%26)
else:
c=c+i
mystring.set(c)
Label(root,textvariable=mystring,bg='blue',fg='white',font=("Helvetica", 30)).pack()
root.update()
e.get() probably is returning a string. You have set a = e.get then, later on, you do a % 26. The % is modulo for an int, but is string formatting for a string. If a isn't a proper string for formatting (e.g. "There were %s cows!"), it will throw that TypeError. Test this by using IDLE's standard debugger or putting prints to print the value of each variable for testing.
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()