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 8 years ago.
Improve this question
I need to write a regex, that will traverse through my file and replace each :
href='#!/api/API.
with
href='http://www.domain.com/API. . I currently have this kind of code, that will be run from console :
def replace(file, pattern, substring):
my_file = open(file)
for line in my_file:
my_file.write(line.replace(pattern, substring))
Will sending the given strings work in this case ?
So you want to replace #!/api with http://www.domain.com?
You can just do:
line.replace('#!/api','http://www.domain.com')
No need for regex.
Related
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 9 years ago.
Improve this question
How can I find a string in a text file and then print which line that string was found on?
I am using Python 2.7.
In Python, enumerate is handy for this sort of thing:
with open(myfile) as f:
for index, line in enumerate(f, 1):
if s in line:
print("'{0}' found on line {1}".format(s, index))
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
Let's say I have a text file of links and garbage which look like this:
www.facebook.com
www.google.com
123
fewfwfqwfq
Let's do a for loop and compare each line and if there is not a ".com" in the specific line, the line gets deleted. In that case '123' and 'fewfwfqwfq' gets deleted.
How can I achieve that?
good_lines = [l for l in links if '.com' in l]
with open('fname.txt') as fout:
[fout.write(g) for g in goodlines]
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 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']
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 a text file where I wrote a list of symbolic equations. I need to set the value of a variable with this list.
In other words I'm looking for a command that automatically "copy" all I have written in the text file and "paste" it into an expression such this:
a = all_it's_written_in_the_text_file
Is this possible?
with open(filename, "rt") as f:
a = eval(f.read())