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']
Related
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 7 months ago.
Improve this question
How can we reduce a string like haaaaaaapppppyyyyyy to haappyy
Such that repetition is allowed to a maximum of twice in a row for a character in a string?
including any character ( special characters also )
converting --------------------- to --
We can use a regex replacement:
inp = "haaaaaaapppppyyyyyy"
output = re.sub(r'(\w)\1{2,}', r'\1\1', inp)
print(output) # haappyy
The above logic matches any one character which is followed by itself two or more times. It then replaces with just two of the character.
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=")
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 need to split a really complex line for me. The line I want to split is as follows
2019.10.20-22.01.33: '10.11.111.25 9999995555884411:TechnoBeceT(69)' logged in
how can i split this like this
['2019.10.20-22.01.33', '10.11.111.25', '9999995555884411', 'logged in']
i don't need
TechnoBeceT(69) this area.
Using Regular Expression
import re
p = re.compile(r'(([\d\.-]+)(?::|\s)|(logged in))')
s = "2019.10.20-22.01.33: '10.11.111.25 9999995555884411:TechnoBeceT(69)' logged in"
q = [x[1] or x[2] for x in p.findall(s)]
print(q)
Output
['2019.10.20-22.01.33', '10.11.111.25', '9999995555884411', 'logged in']
Looks like you just need to split by ' ', ':' and 'TechnoBeceT(69)' as an appropriate regex. This existing question is probably what you need: Split string with multiple delimiters in 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 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']
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