How to remove single quotes in python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have
filename = src_filename
print filename
somehow it is printing filename = 'apple' instead of just apple. Is there a way I can remove these single quotes.

The single quotes denote a string literal
>>> 'apple'
'apple'
If you print the string, it will not use the single quotes
>>> print('apple')
apple
So in your case you can simply
print(filename)

>>> print(filename.strip('"\''))

Related

Problem in filter column in dataframe by contain function in str by "|" operator [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Why is not filter the df like in the screenshot
Show the function of str.contain("|") that's didn't work
You need to set the regex parameter of contains to False. In your code now, the string "|" is parsed as a regular expression, but you want to match the literal pipe character.
movies_with_more_dir = movies_df[movies_df['director'].str.contains("|", regex=False)]

How to keep the only characters I want [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I need to keep a certain character for my python project which and I don’t want to replace every unused character with ‘’ is there any way to do it?
You can use a function re.sub in re library
For example:
data = re.sub('0123456789abcdefghijklmnopqrstuvwxyzQWERTYUIOPASDFGHJKLZXCVBNNM+/-', '', data)
This will keep every character in the first parameter replace with the second parameter by using String from the third parameter
if you want to keep "d" in the string
origin = "abcdefghidx"
result = "".join([c for c in origin if c=="d"])
You can use str.replace('what to want to delete', 'what you want to add').
ex-
name = "stackoverflow"
newName = name.replace('o', '0')
newName becomes 'stack0verfl0w'

find the word before specific string in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Suppose I have string
exp='"\"OLS\".\"ORDER_ITEMS\".\"QUANTITY\" <50 and \"OLS\".\"PRODUCTS\".\"PRODUCT_NAME\" = 'Kingston''
How can I find word before string \"OLS\".\"PRODUCTS\".\"PRODUCT_NAME\" = 'Kingston' that is and in my case
result should be and
Here's one way (after fixing your string literal):
import re
exp='"OLS"."ORDER_ITEMS"."QUANTITY" <50 and "OLS"."PRODUCTS"."PRODUCT_NAME" = \'Kingston\''
search_for = '"OLS"."PRODUCTS"."PRODUCT_NAME"'
m = re.search(r'(\w+)\s+' + search_for, exp)
print(m.group(1))
Result:
and
Note that there's no reason to escape the double quote characters in your string, since you're defining the string with single quotes. For the same reason, you do have to escape the single quotes around Kingston.
This?
key = "\"OLS\".\"PRODUCTS\".\"PRODUCT_NAME\" = 'Kingston'"
exp[:exp.index(key)].split()[-1]

Extract Data Enclosed between three asterisks in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want the data enclosed between three asterisks.And the Word should start with description.
For eg:I have data like
description ***tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568***;
I want only
tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568
You may use re.findall here:
inp = "description ***tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568***;"
matches = re.findall(r'\bdescription\s+\*{3}(.*?)\*{3}', inp, flags=re.DOTALL)
print(matches)
This prints:
['tCore-DFON_P.17-18>dPLUC80115_S19P1>>><<<dPDCL80121_S17P1<100G.IPT.NTTA.SEA.ASE+PC1.LUC/PLD-SEA/PLD_100GEL064.263568']
Note that I use dot all mode in the regex, in case your expected matches might span across more than one line.

Concatenating select part of split string in python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
If I have a string:
str = "This is my string"
And I split it:
str.split (" ")
to obtain:
["This","is","my","string"]
I wonder how to concatenate it in order to obtain:
["This","is","my string"]
This isn't my exact problem. I'm parsing log files and need to figure out how to concatenate a certain part of each line after splitting it.
You can do something like below.
>>> "This is my string".split(" ", 2)
['This', 'is', 'my string']
>>>

Categories

Resources