Concatenating select part of split string 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 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']
>>>

Related

Only print the date in a string [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 5 months ago.
Improve this question
This is my string
word = " Saturday Fortune 08-09-2022 (4872) Draw Numbers "
But I only want to print the date in the string like this
'08-09-2022'
So how can I achieve this?
Should use a regular expression to find that part in the string. Something like:
import re
match = re.search(r'\d{2}-\d{2}-\d{4}', word)
This already seems to be answered as a subproblem in this thread.

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]

Regex to match up to the last occurrence of "$" and replace everything to the right [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Context: I want to use Python to iterate over a bunch of json documents (cosmodb) and remove the last portion of a string in the value of a key
I want to turn this:
"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02"
into this:
"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"
Any help would be greatly appreciated.
You could easily implement this with this piece of code:
result = my_string.rsplit('$', 1)[0] + "$"
Which behaves like this:
>>> my_string = "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02"
>>> print(my_string.rsplit('_', 1)[0])
"randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"

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.

How to remove single quotes 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 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('"\''))

Categories

Resources