Re: [Tutor] delete strings from specificed words
On 11Jan2018 12:16, YU Bowrote: Hi, Hi, On Thu, Jan 11, 2018 at 11:40:35AM +1100, Cameron Simpson wrote: Do you have the text as above - a single string - or coming from a file? I'll presume a single string. En.., the text is multi-string from str(something) within python. That's a single string in my thinking. I would treat the text as lines, particularly since the diff markers etc are all line oriented. So you might write something like this: interesting = [] for line in the_text.splitlines(): if line.startswith('diff --git '): break interesting.append(line) Now the "interesting" list has the lines you want. Yes, i test your method and it will come to my intend, but maybe cast a few of other steps. I think that the method will offer a similar ways to solve others issues. I personally like peter's method: text.partition("diff --git")[0].strip() Yes, Peter's partition method is very short and direct. Personally I would include a leading newline in the string, thus: text.partition("\ndiff --git ") to make it more precise. Consider a patch which discusses your script; its comment area might well mention the string "diff --git", as this email does. What you end up with may depend on what further things you do with the text. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] delete strings from specificed words
Hi, On Thu, Jan 11, 2018 at 11:40:35AM +1100, Cameron Simpson wrote: On 09Jan2018 22:20, YU Bowrote: The text i will working as follow: ```text [...] diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index a789f952b3e9..443892dabedb 100644 [...] +++ b/tools/perf/util/util.c [...] ``` [...] I have tried split(), replace(), but i have no idea to deal with it. Do you have the text as above - a single string - or coming from a file? I'll presume a single string. En.., the text is multi-string from str(something) within python. I would treat the text as lines, particularly since the diff markers etc are all line oriented. So you might write something like this: interesting = [] for line in the_text.splitlines(): if line.startswith('diff --git '): break interesting.append(line) Now the "interesting" list has the lines you want. Yes, i test your method and it will come to my intend, but maybe cast a few of other steps. I think that the method will offer a similar ways to solve others issues. I personally like peter's method: text.partition("diff --git")[0].strip() Thanks, Bo There's any number of variations on that you might use, but that should get you going. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] delete strings from specificed words
On 09Jan2018 22:20, YU Bowrote: The text i will working as follow: ```text [...] diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index a789f952b3e9..443892dabedb 100644 [...] +++ b/tools/perf/util/util.c [...] ``` In fact, this is a patch from lkml,my goal is to design a kernel podcast for myself to focus on what happened in kernel. I have crawled the text with python and want to remove strings from *diff --git*, because reading the git commit above, i have a shape in head. I have tried split(), replace(), but i have no idea to deal with it. Do you have the text as above - a single string - or coming from a file? I'll presume a single string. I would treat the text as lines, particularly since the diff markers etc are all line oriented. So you might write something like this: interesting = [] for line in the_text.splitlines(): if line.startswith('diff --git '): break interesting.append(line) Now the "interesting" list has the lines you want. There's any number of variations on that you might use, but that should get you going. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] delete strings from specificed words
Hi, On Wed, Jan 10, 2018 at 10:37:09AM +0100, Peter Otten wrote: YU Bo wrote: index 45a63e0..3b9b238 100644 ... ``` I want to delete string from *diff --git* to end, because too many code is here Use str.split() or str.partition() and only keep the first part: text = """The registers rax, rcx and rdx are touched when controlling IBRS ... so they need to be saved when they can't be clobbered. ... ... diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h ... index 45a63e0..3b9b238 100644 ... """ cleaned_text = text.partition("diff --git")[0].strip() print(cleaned_text) The registers rax, rcx and rdx are touched when controlling IBRS so they need to be saved when they can't be clobbered. Cool,It is what i want. Thanks all! Bo ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] delete strings from specificed words
YU Bo wrote: > ```text > The registers rax, rcx and rdx are touched when controlling IBRS > so they need to be saved when they can't be clobbered. > > diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h > index 45a63e0..3b9b238 100644 > ... > ``` > I want to delete string from *diff --git* to end, because too many code is > here Use str.split() or str.partition() and only keep the first part: >>> text = """The registers rax, rcx and rdx are touched when controlling IBRS ... so they need to be saved when they can't be clobbered. ... ... diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h ... index 45a63e0..3b9b238 100644 ... """ >>> cleaned_text = text.partition("diff --git")[0].strip() >>> print(cleaned_text) The registers rax, rcx and rdx are touched when controlling IBRS so they need to be saved when they can't be clobbered. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] delete strings from specificed words
Hi, First, thank you very much for your reply. On Tue, Jan 09, 2018 at 10:25:11PM +, Alan Gauld via Tutor wrote: On 09/01/18 14:20, YU Bo wrote: But, i am facing an interesting question.I have no idea to deal with it. I don;t think you have given us enough context to be able to help much. WE would need some idea of the input and output data (both current and desired) It sounds like you are building some kind of pretty printer. Maybe you could use Pythons pretty-print module as a design template? Or maybe even use some of it directly. It just depends on your data formats etc. Yes. I think python can deal with it directly. In fact, this is a patch from lkml,my goal is to design a kernel podcast for myself to focus on what happened in kernel. Sorry, I've no idea what lkml is nor what kernel you are talking about. Can you show us what you are receiving, what you are currently producing and what you are trying to produce? Some actual code might be an idea too. And the python version and OS. Sorry, i don't to explain it.But, my code is terribly. lkml.py: ```code #!/usr/bin/python # -*- coding: UTF-8 -*- # File Name: lkml.py # Author: Bo Yu """ This is source code in page that i want to get """ import sys reload(sys) sys.setdefaultencoding('utf8') import urllib2 from bs4 import BeautifulSoup import requests import chardet import re # import myself print function from get_content import print_content if __name__ == '__main__': comment_url = [] target = 'https://www.spinics.net/lists/kernel/threads.html' req = requests.get(url=target) req.encoding = 'utf-8' content = req.text bf = BeautifulSoup(content ,'lxml') # There is no problem context = bf.find_all('strong') for ret in context[0:1]: for test in ret: print '\t' x = re.split(' ', str(test)) y = re.search('"(.+?)"', str(x)).group(1) comment_url.append(target.replace("threads.html", str(y))) for tmp_target in comment_url: print "===This is a new file ===" print_content(tmp_target, 'utf-8', 'title') ``` get_content.py: ```code #!/usr/bin/python # -*- coding: UTF-8 -*- # File Name: get_content.py import urllib2 from bs4 import BeautifulSoup import requests import chardet import re def print_content(url, charset, find_id): req = requests.get(url=url) req.encoding = charset content = req.text bf = BeautifulSoup(content ,'lxml') article_title = bf.find('h1') #author = bf.find_all('li') commit = bf.find('pre') print '\t' print article_title.get_text() print '\t' x = str(commit.get_text()) print x ``` python --version: Python 2.7.13 OS: debian 9 usage: python lkml.py output: oh... https://pastecode.xyz/view/04645424 Please ignore my print debug format. This is my code and i can get text like output above. So, simple my quzz: I dont know how to delete strings after special word, for example: ```text The registers rax, rcx and rdx are touched when controlling IBRS so they need to be saved when they can't be clobbered. diff --git a/arch/x86/entry/calling.h b/arch/x86/entry/calling.h index 45a63e0..3b9b238 100644 ... ``` I want to delete string from *diff --git* to end, because too many code is here Whatever, thanks! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] delete strings from specificed words
On 09/01/18 14:20, YU Bo wrote: > But, i am facing an interesting question.I have no idea to deal with it. I don;t think you have given us enough context to be able to help much. WE would need some idea of the input and output data (both current and desired) > [PATCH] perf tools: Fix copyfile_offset update of output offset > > We need to increase output offset in each iteration, > not decrease it as we currently do. > It sounds like you are building some kind of pretty printer. Maybe you could use Pythons pretty-print module as a design template? Or maybe even use some of it directly. It just depends on your data formats etc. > In fact, this is a patch from lkml,my goal is to design a kernel podcast > for myself to focus on what happened in kernel. Sorry, I've no idea what lkml is nor what kernel you are talking about. Can you show us what you are receiving, what you are currently producing and what you are trying to produce? Some actual code might be an idea too. And the python version and OS. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] delete strings from specificed words
Hi, I am learning python to reslove issue,which make happy:) But, i am facing an interesting question.I have no idea to deal with it. The text i will working as follow: ```text [PATCH] perf tools: Fix copyfile_offset update of output offset We need to increase output offset in each iteration, not decrease it as we currently do. I guess we were lucky to finish in most cases in first iteration, so the bug never showed. However it shows a lot when working with big (~4GB) size data. Link: http://lkml.kernel.org/n/tip-f4az7t2nxjbjz5tqrv83z64e@xx Signed-off-by: Jiri Olsa--- tools/perf/util/util.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index a789f952b3e9..443892dabedb 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c @@ -210,7 +210,7 @@ static int copyfile_offset(int ifd, loff_t off_in, int ofd, loff_t off_out, u64 size -= ret; off_in += ret; - off_out -= ret; + off_out += ret; } munmap(ptr, off_in + size); -- 2.13.6 ``` In fact, this is a patch from lkml,my goal is to design a kernel podcast for myself to focus on what happened in kernel. I have crawled the text with python and want to remove strings from *diff --git*, because reading the git commit above, i have a shape in head. I have tried split(), replace(), but i have no idea to deal with it. I will appericate if you have any idea. Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor