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]
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
in 'id_path' in CSV file i want remove subpath from it such as
dataframe of csv file
i want remove all path before the image file name
./input/skin-cancer-malignant-vs-benign/data/test/benign/454.jpg
./input/skin-cancer-malignant-vs-benign/data/test/benign/90.jpg
./input/skin-cancer-malignant-vs-benign/data/test/benign/147.jpg
./input/skin-cancer-malignant-vs-benign/data/test/malignant/771.jpg
./input/skin-cancer-malignant-vs-benign/data/test/malignant/208.jpg
./input/skin-cancer-malignant-vs-benign/data/test/malignant/1383.jpg
./input/skin-cancer-malignant-vs-benign/data/test/malignant/1354.jpg
the output should be
454.jpg
90.jpg
147.jpg
771.jpg
208.jpg
1383.jpg
1354.jpg
rsplit() splits the data from the right side of the string and 1 is way of saying python to stop after first split.
txt = "./input/skin-cancer-malignant-vs-benign/data/test/benign/454.jpg"
x = txt.rsplit("/",1)
#your answer
print(x[1])
on your dataframe you could do something like:
train_df['id_path'] = train_df['id_path'].apply(lambda x: x.rsplit('/',1)[1])
Using str.replace:
df["filename"] = df["path"].str.replace(r'^.*/', '')
We could also use str.extract here:
df["filename"] = df["path"].str.extract(r'([^/]+\.\S+$)')
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
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()
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
Say I have an arbitrary string like
"abc 123 def 456"
How would I take out the integers so that it can print "123456" without using regex?
for your specific question, you can just use .isdigit()
s = "abc 123 def 456"
for ch in s:
if ch.isdigit():
print(ch, end='')
print()
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()