data=data={...} How to delete the first data= from such strings? [closed] - python

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
I got data=.... where I've done splitting so text.split("=") and chose the second list element.
But with my new data in this way it deletes even the data= and the "{" "}" Elements
Any suggestions?

You should choose the second element through the end of the list, then rejoin them.
text = "=".join(text.split("=")[1:])
Or you could do a string replacement:
text = text.replace("data=data=", "data=")

Related

How can I sort the a list of strings which contain date as substring [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 1 year ago.
Improve this question
Below is my list,
['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
And I need to sort the list and filter it to a below format. Please suggest a quicker way to achieve it
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']
When your format is fixed and always starts with "pending" you can use the normal sorted function and count the / in a list comprehension.
>>> values = ['pending/', 'pending/2021-08-01/', 'pending/2021-06-01/', 'pending/2021-06-18/']
>>> sorted(x for x in values if x.count('/') == 2)
['pending/2021-06-01/', 'pending/2021-06-18/', 'pending/2021-08-01/']

python regex with multiple separators [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 3 years ago.
Improve this question
I am trying to separate all the images from the following string.
how can I get a list of images that start with "comp1/img_" and are either split by a "," or a ";"
/*jsonp*/jsonresp({"img_set":"comp1/img_23434;comp1/img_3243r43r,comp1/img_o43nfjr;comp1/img_wjfno43,comp1/img_nrejfner;comp1/img_jrenckerjv,comp1/img_23434k;comp1/img_rkfnk4n"},"fknreff\",");
so I would end up with a list like...
comp1/img_23434
comp1/img_3243r43r
comp1/img_o43nfjr
comp1/img_wjfno43
comp1/img_nrejfner
comp1/img_jrenckerjv
comp1/img_23434k
comp1/img_rkfnk4n
any help would be appreciated.
thanks
You can do this:
>>> data = '/*jsonp*/jsonresp({"img_set":"comp1/img_23434;comp1/img_3243r43r,comp1/img_o43nfjr;comp1/img_wjfno43,comp1/img_nrejfner;comp1/img_jrenckerjv,comp1/img_23434k;comp1/img_rkfnk4n"},"fknreff\",");'
>>> import re
>>> re.findall(r'comp1/img_[^;,"]+', data)
['comp1/img_23434', 'comp1/img_3243r43r', 'comp1/img_o43nfjr', 'comp1/img_wjfno43', 'comp1/img_nrejfner', 'comp1/img_jrenckerjv', 'comp1/img_23434k', 'comp1/img_rkfnk4n']

Easy way to extract multiple dates from string without spaces? [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 3 years ago.
Improve this question
I am looking for a way to automatically extract dates from a string, but following each other without a delimiter
For example my string is: \n-\n24-04-201923-04-201922-04-201921-04-201920-04-201919-04-201918-04-2019
How can I get this output:
24-04-2019
23-04-2019
22-04-2019
21-04-2019
20-04-2019
19-04-2019
18-04-2019
Any help would be appreciated!
Given that they're all of equal length, you can just clear the \n's then use textwrap:
import textwrap
print(textwrap.wrap(my_string, 10))
You can remove \n's using strip():
my_string = my_string.strip()
You can use this code also.
string='\n-\n24-04-201923-04-201922-04-201921-04-201920-04-201919-04-201918-04-2019'
newStr=string[3:]
for char in range(0,len(newStr),10):
print newStr[char:char+10]
Here's the output
24-04-2019
23-04-2019
22-04-2019
21-04-2019
20-04-2019
19-04-2019
18-04-2019

How to remove characters from a list. [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 4 years ago.
Improve this question
I have a list output:
['Go497f9te(40RAAC34)\n','G0THDU433(40RAAC33)\n']
and I want to clean it up in order to output:
[40RAAC34,40RAAC33]
If you have a string:
'hello (world)'
and want the text between the brackets, you can either use a regex:
import re
re.findall('\((.*?)\)', s)[0]
#'world'
or, if you are sure that there is only one set of brackets (i.e. no leading ) chars) then you can just use slicing:
s[s.index('(')+1:s.index(')')]
#'world'
So then you just need to throw this into a list-comprehension or similar.
l = ['Go497f9te(40RAAC34)\n','G0THDU433(40RAAC33)\n']
[s[s.index('(')+1:s.index(')')] for s in l]
#['40RAAC34', '40RAAC33']

How do you convert a single entity list into a string 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 4 years ago.
Improve this question
I am trying to convert a tuple:
('Cobra',)
to a string, which when printed yields:
Cobra
#Assuming you have a list of tuples
sample = [('cobra',),('Cat',),('Dog',),('hello',),('Cobra',)]
#For each tuple in the list, Get the first element of each tuple
x = [i[0] for i in sample]

Categories