SyntaxError: invalid syntax file.close() [closed] - python

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 4 years ago.
Improve this question
I keep getting an error here, any help?
file = open(FILENAME,'r') is on the same line as file.close with no indentations. Why do I keep getting this error?

You forgot to close a parenthesis on this line:
ballpark = line.strip*(
it should probably be:
ballpark = line.strip()

Related

python string comparison failed [closed]

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
In python, I wrote a program to compare two strings.
The coedes are below
d = data.iloc[0]
cmpdate = d['quant_date']
print(cmpdate)
if (cmpdate=='2010-03-18'):
print("=================", dt)
else:
print("xxxxxxxxxxxxx", cmpdate)
the results are
2010-03-18
xxxxxxxxxxxxx 2010-03-18
tow strings are exactly same.
what is the problem?
TIA
Make sure you convert both of the dates to datetime format
and check the result
use to_datetime() function
This works fine

is there an python method for solve this list index error? [closed]

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 have error that say: the list index out of range ?
equal_score = []
for i, j in enumerate(new_gd):
if i > len(new_gd):
break
if new_gd[i]['score'] == new_gd[i+1]['score']:
equal_score.append(new_gd[i])
equal_score.append(new_gd[i+1])'
Since you refer to the index i+1 you should do if i+1 >= len(new_gd): break so you make sure i+1 exists.

No "\n" after line.rsplit in python [closed]

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 want to create a new text file and cut the string in each line.
for line in rescut:
rescutfinal.write("pretext_" + line.rsplit('delimiter', 1)[-1] + "1\t0\t10\tLinear\t0\t0")
But my code doesn't work as expected. My Outpout contains a new line after the "line.rsplit"-string
pretext_linesplitstring
string after linesplitstring pretext_linesplitstring
string after linesplitstring pretext_linesplitstring
string after linesplitstring pretext_linesplitstring"
How do I get rid of the "\n" after the linesplitstring?
you can use str.rstrip() or str.strip():
line.rstrip().rsplit('delimiter', 1)[-1]

Python SyntaxError when formatting [closed]

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 4 years ago.
Improve this question
print "Detections before NMS = {}".format(detections)
Produces:
SyntaxError: invalid syntax
detections = 10
print ("Detections before NMS = {}".format(detections))
Output:
Detections before NMS = 10
If you are using Python 3, print is a function and therefore needs brackets.
I suspect you are on Python 3, hence the syntax error.

How to Empty Lines? [closed]

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()

Categories

Resources