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))
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 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 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())
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
How do I tell a script to compare a string with the names of all materials? This following code does not work:
for i in len(bpy.data.materials):
if str(color) == bpy.data.materials[i].name:
mat = bpy.data.materials[i]
mesh.materials.append(mat)
break
Error:
TypeError: 'int' object is not iterable (line 1)
Thanks.
That first line needs to be changed to for i in range(len(bpy.data.materials)):.
Alternatively, you could write the following instead:
for mat in bpy.data.materials:
if str(color) == mat.name:
mesh.materials.append(mat)
break
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.