Re: PIL : How to write array to image ???
On Oct 4, 9:47 am, Martin wrote: > On Oct 3, 11:56 pm, Peter Otten <__pete...@web.de> wrote: > > > > > > > Martin wrote: > > > Dear group > > > > I'm trying to use PIL to write an array (a NumPy array to be exact) to > > > an image. > > > Peace of cake, but it comes out looking strange. > > > > I use the below mini code, that I wrote for the purpose. The print of > > > a looks like expected: > > > > [[ 200. 200. 200. ..., 0. 0. 0.] > > > [ 200. 200. 200. ..., 0. 0. 0.] > > > [ 200. 200. 200. ..., 0. 0. 0.] > > > ..., > > > [ 0. 0. 0. ..., 200. 200. 200.] > > > [ 0. 0. 0. ..., 200. 200. 200.] > > > [ 0. 0. 0. ..., 200. 200. 200.]] > > > > But the image looks nothing like that. > > > > Please see the images on: > > >http://hvidberg.net/Martin/temp/quat_col.png > > >http://hvidberg.net/Martin/temp/quat_bw.png > > > > or run the code to see them locally. > > > > Please – what do I do wrong in the PIL part ??? > > > > :-? Martin > > > > import numpy as np > > > from PIL import Image > > > from PIL import ImageOps > > > > maxcol = 100 > > > maxrow = 100 > > > > a = np.zeros((maxcol,maxrow),float) > > > > for i in range(maxcol): > > > for j in range(maxrow): > > > if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>= > > > (maxrow/2)): > > > a[i,j] = 200 > > > else: > > > a[i,j] = 0 > > > > print a > > > > pilImage = Image.fromarray(a,'RGB') > > > pilImage.save('quat_col.png') > > > pilImage = ImageOps.grayscale(pilImage) > > > pilImage.save('quat_bw.png') > > > The PIL seems to copy the array contents directly from memory without any > > conversions or sanity check. In your example The float values determine the > > gray value of 8 consecutive pixels. > > > If you want a[i,j] to become the color of the pixel (i, j) you have to use > > an array with a memory layout that is compatible to the Image. > > Here are a few examples: > > > >>> import numpy > > >>> from PIL import Image > > >>> a = numpy.zeros((100, 100), numpy.uint8) > > >>> a[:50, :50] = a[50:, 50:] = 255 > > >>> Image.fromarray(a).save("tmp1.png") > > >>> b = numpy.zeros((100, 100, 3), numpy.uint8) > > >>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0] > > >>> Image.fromarray(b).save("tmp2.png") > > >>> c = numpy.zeros((100, 100), numpy.uint32) > > >>> c[:50, :50] = c[50:, 50:] = 0xff808000 > > >>> Image.fromarray(c, "RGBA").save("tmp3.png") > > > Peter > > Thanks All - That helped a lot... > > The working code ended with: > > imga = np.zeros((imgL.shape[1],imgL.shape[0]),np.uint8) > for ro in range(imgL.shape[1]): > for co in range(imgL.shape[0]): > imga[ro,co] = imgL[ro,co] > Image.fromarray(imga).save('_a'+str(lev)+'.png') Without knowing how big your image is (can't remember if you said!). Perhaps rather than looping in the way you might in C for example, the numpy where might be quicker if you have a big image. Just a thought... -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL : How to write array to image ???
On Oct 5, 5:14 pm, Martin wrote: > On Oct 4, 10:16 pm, "Mart." wrote: > > > > > > > On Oct 4, 9:47 am, Martin wrote: > > > > On Oct 3, 11:56 pm, Peter Otten <__pete...@web.de> wrote: > > > > > Martin wrote: > > > > > Dear group > > > > > > I'm trying to use PIL to write an array (a NumPy array to be exact) to > > > > > an image. > > > > > Peace of cake, but it comes out looking strange. > > > > > > I use the below mini code, that I wrote for the purpose. The print of > > > > > a looks like expected: > > > > > > [[ 200. 200. 200. ..., 0. 0. 0.] > > > > > [ 200. 200. 200. ..., 0. 0. 0.] > > > > > [ 200. 200. 200. ..., 0. 0. 0.] > > > > > ..., > > > > > [ 0. 0. 0. ..., 200. 200. 200.] > > > > > [ 0. 0. 0. ..., 200. 200. 200.] > > > > > [ 0. 0. 0. ..., 200. 200. 200.]] > > > > > > But the image looks nothing like that. > > > > > > Please see the images on: > > > > >http://hvidberg.net/Martin/temp/quat_col.png > > > > >http://hvidberg.net/Martin/temp/quat_bw.png > > > > > > or run the code to see them locally. > > > > > > Please – what do I do wrong in the PIL part ??? > > > > > > :-? Martin > > > > > > import numpy as np > > > > > from PIL import Image > > > > > from PIL import ImageOps > > > > > > maxcol = 100 > > > > > maxrow = 100 > > > > > > a = np.zeros((maxcol,maxrow),float) > > > > > > for i in range(maxcol): > > > > > for j in range(maxrow): > > > > > if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>= > > > > > (maxrow/2)): > > > > > a[i,j] = 200 > > > > > else: > > > > > a[i,j] = 0 > > > > > > print a > > > > > > pilImage = Image.fromarray(a,'RGB') > > > > > pilImage.save('quat_col.png') > > > > > pilImage = ImageOps.grayscale(pilImage) > > > > > pilImage.save('quat_bw.png') > > > > > The PIL seems to copy the array contents directly from memory without > > > > any > > > > conversions or sanity check. In your example The float values determine > > > > the > > > > gray value of 8 consecutive pixels. > > > > > If you want a[i,j] to become the color of the pixel (i, j) you have to > > > > use > > > > an array with a memory layout that is compatible to the Image. > > > > Here are a few examples: > > > > > >>> import numpy > > > > >>> from PIL import Image > > > > >>> a = numpy.zeros((100, 100), numpy.uint8) > > > > >>> a[:50, :50] = a[50:, 50:] = 255 > > > > >>> Image.fromarray(a).save("tmp1.png") > > > > >>> b = numpy.zeros((100, 100, 3), numpy.uint8) > > > > >>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0] > > > > >>> Image.fromarray(b).save("tmp2.png") > > > > >>> c = numpy.zeros((100, 100), numpy.uint32) > > > > >>> c[:50, :50] = c[50:, 50:] = 0xff808000 > > > > >>> Image.fromarray(c, "RGBA").save("tmp3.png") > > > > > Peter > > > > Thanks All - That helped a lot... > > > > The working code ended with: > > > > imga = np.zeros((imgL.shape[1],imgL.shape[0]),np.uint8) > > > for ro in range(imgL.shape[1]): > > > for co in range(imgL.shape[0]): > > > imga[ro,co] = imgL[ro,co] > > > Image.fromarray(imga).save('_a'+str(lev)+'.png') > > > Without knowing how big your image is (can't remember if you said!). > > Perhaps rather than looping in the way you might in C for example, the > > numpy where might be quicker if you have a big image. Just a > > thought... > > And a good thought too... I use to teach ansi C at Univ. many ears > ago, and it's still one of my favorits. But I prefere Python for this > type of work. Python have a much faster develop time, is much easier > to write (fewer linees, simpler syntax) and is much more 'hot' in > these days. Finally my favorit GIS and RS applications support Python > out-of-the-box. > > :-) Martin I wasn't advocating doing it in c just that you could speed up your program considerably by not looping and using some of the numpy array options. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with arrays
On Aug 25, 11:50 pm, Stephen Fairchild wrote: > Philip Semanchuk wrote: > > > On Aug 25, 2009, at 6:14 PM, Gleb Belov wrote: > > >> Hello! I'm working on an exercise wherein I have to write a Guess The > >> Number game, but it's the computer who's guessing MY number. I can get > >> it to work, but there's one obvious problem: the computer generates > >> random numbers until one of them corresponds to my number, but it will > >> often generate one number (eg. 4) numerous times, meaning it doesn't > >> know that this number is invalid. What I mean is, it will sometimes > >> use 37 tries to guess a number out of 1 - 9, which makes no sense, > >> since it should only take 9 tries, at most. I was trying to find a way > >> to make a dynamic list of all the numbers the computer generates in > >> the loop and then make it re-generate the number if the previous > >> number is present in the list, so it doesn't keep on generating 4 (as > >> an example). I don't know if that makes sense... Basically, we humans > >> know that once something is incorrect, there's no point in trying to > >> use it as the answer next time, because we already know it's > >> incorrect. How do I go about coding this in Python? I'm still quite > >> new to the language so any help will be appreciated... > > > One cheap way to do it (not necessarily efficient) is to make a list > > of your possible guesses (e.g. range(1,10)), use random.shuffle() to > > put them in random order and then run through the guesses one at a time. > > import random > import time > > l = range(1, 10) > > while l: > print l.pop(random.randint(0, len(l) - 1)) > time.sleep(2) > > -- > Stephen Fairchild Perhaps generate all of the possible moves at the outset in some form of lookup table that you can refer to? I think it is a similar thing to how computers play chess, I think it is called "hash tables" -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with arrays
On Aug 26, 3:02 am, Dave Angel wrote: > Stephen Fairchild wrote: > > Philip Semanchuk wrote: > > >> On Aug 25, 2009, at 6:14 PM, Gleb Belov wrote: > > >>> Hello! I'm working on an exercise wherein I have to write a Guess The > >>> Number game, but it's the computer who's guessing MY number. I can get > >>> it to work, but there's one obvious problem: the computer generates > >>> random numbers until one of them corresponds to my number, but it will > >>> often generate one number (eg. 4) numerous times, meaning it doesn't > >>> know that this number is invalid. What I mean is, it will sometimes > >>> use 37 tries to guess a number out of 1 - 9, which makes no sense, > >>> since it should only take 9 tries, at most. I was trying to find a way > >>> to make a dynamic list of all the numbers the computer generates in > >>> the loop and then make it re-generate the number if the previous > >>> number is present in the list, so it doesn't keep on generating 4 (as > >>> an example). I don't know if that makes sense... Basically, we humans > >>> know that once something is incorrect, there's no point in trying to > >>> use it as the answer next time, because we already know it's > >>> incorrect. How do I go about coding this in Python? I'm still quite > >>> new to the language so any help will be appreciated... > > >> One cheap way to do it (not necessarily efficient) is to make a list > >> of your possible guesses (e.g. range(1,10)), use random.shuffle() to > >> put them in random order and then run through the guesses one at a time. > > > import random > > import time > > > l = range(1, 10) > > > while l: > > print l.pop(random.randint(0, len(l) - 1)) > > time.sleep(2) > > While both of these will work well, I'd point out that a direct > translation of your question is to use a set. Define an empty set, and > each time you try a number unsuccessfully, add it to the set. Then just use >while x in myset: >x = newguess() > > to find the next guess. This approach isn't as efficient, but it's a > useful paradigm to understand. > > A separate question is whether the human is supposed to tell the > computer whether the guess is high or low. If so, you can eliminate > many numbers on each guess. For example, suppose the solution is 8. A > guess of 5 would say "too low." Then you'd cross off now only 5 but 1-4 > as well. > > With this change the best solution changes from a random shuffle to a > binary search. > > DaveA That's a good point if you can define whether the computer has guessed too low/high, then you could use the previous guess to set a bounds and rescale the next random guess. min + (random_number * max) although I think random.randomint does this for you! -- http://mail.python.org/mailman/listinfo/python-list
Re: regexp help
On Aug 27, 7:15 pm, Bakes wrote: > If I were using the code: > > (?P[0-9]+) > > to get an integer between 0 and 9, how would I allow it to register > negative integers as well? -? -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting patterns after matching a regex
On Sep 8, 2:15 pm, MRAB wrote: > Martin wrote: > > Hi, > > > I need to extract a string after a matching a regular expression. For > > example I have the string... > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > > > and once I match "FTPHOST" I would like to extract > > "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the > > problem, I had been trying to match the string using something like > > this: > > > m = re.findall(r"FTPHOST", s) > > > But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov" > > part. Perhaps I need to find the string and then split it? I had some > > help with a similar problem, but now I don't seem to be able to > > transfer that to this problem! > > > Thanks in advance for the help, > > m = re.search(r"FTPHOST: (.*)", s) > print m.group(1) so the .* means to match everything after the regex? That doesn't help in this case as the string is placed amongst others for example. MEDIATYPE: FtpPull\r\n', 'MEDIAFORMAT: FILEFORMAT\r\n', 'FTPHOST: e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n', 'Ftp Pull Download Links: \r\n', -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting patterns after matching a regex
On Sep 8, 2:21 pm, "Mark Tolonen" wrote: > "Martin" wrote in message > > news:5941d8f1-27c0-47d9-8221-d21f07200...@j39g2000yqh.googlegroups.com... > > > > > Hi, > > > I need to extract a string after a matching a regular expression. For > > example I have the string... > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > > > and once I match "FTPHOST" I would like to extract > > "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the > > problem, I had been trying to match the string using something like > > this: > > > m = re.findall(r"FTPHOST", s) > > > But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov" > > part. Perhaps I need to find the string and then split it? I had some > > help with a similar problem, but now I don't seem to be able to > > transfer that to this problem! > > In regular expressions, you match the entire string you are interested in, > and parenthesize the parts that you want to parse out of that string. The > group() method is used to get the whole string with group(0), and each of > the parenthesized parts with group(n). An example: > > >>> s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > >>> import re > >>> re.search(r'FTPHOST: (.*)',s).group(0) > > 'FTPHOST: e4ftl01u.ecs.nasa.gov'>>> re.search(r'FTPHOST: (.*)',s).group(1) > > 'e4ftl01u.ecs.nasa.gov' > > -Mark I see what you mean regarding the groups. Because my string is nested in amongst others e.g. MEDIATYPE: FtpPull\r\n', 'MEDIAFORMAT: FILEFORMAT\r\n', 'FTPHOST: e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n', 'Ftp Pull Download Links: \r\n', I get the information that follows as well. So is the only way to then parse the new string? I am trying to construct something that is fairly robust, so not sure just printing before the \r is the best solution. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting patterns after matching a regex
On Sep 8, 2:16 pm, "Andreas Tawn" wrote: > > Hi, > > > I need to extract a string after a matching a regular expression. For > > example I have the string... > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > > > and once I match "FTPHOST" I would like to extract > > "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the > > problem, I had been trying to match the string using something like > > this: > > > m = re.findall(r"FTPHOST", s) > > > But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov" > > part. Perhaps I need to find the string and then split it? I had some > > help with a similar problem, but now I don't seem to be able to > > transfer that to this problem! > > > Thanks in advance for the help, > > > Martin > > No need for regex. > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > If "FTPHOST" in s: > return s[9:] > > Cheers, > > Drea Sorry perhaps I didn't make it clear enough, so apologies. I only presented the example s = "FTPHOST: e4ftl01u.ecs.nasa.gov" as I thought this easily encompassed the problem. The solution presented works fine for this i.e. re.search(r'FTPHOST: (.*)',s).group(1). But when I used this on the actual file I am trying to parse I realised it is slightly more complicated as this also pulls out other information, for example it prints e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n', 'Ftp Pull Download Links: \r\n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/ 0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n', etc. So I need to find a way to stop it before the \r slicing the string wouldn't work in this scenario as I can envisage a situation where the string lenght increases and I would prefer not to keep having to change the string. Many thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting patterns after matching a regex
On Sep 8, 3:53 pm, MRAB wrote: > Mart. wrote: > > On Sep 8, 3:14 pm, "Andreas Tawn" wrote: > >>>>> Hi, > >>>>> I need to extract a string after a matching a regular expression. For > >>>>> example I have the string... > >>>>> s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > >>>>> and once I match "FTPHOST" I would like to extract > >>>>> "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the > >>>>> problem, I had been trying to match the string using something like > >>>>> this: > >>>>> m = re.findall(r"FTPHOST", s) > >>>>> But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov" > >>>>> part. Perhaps I need to find the string and then split it? I had some > >>>>> help with a similar problem, but now I don't seem to be able to > >>>>> transfer that to this problem! > >>>>> Thanks in advance for the help, > >>>>> Martin > >>>> No need for regex. > >>>> s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > >>>> If "FTPHOST" in s: > >>>> return s[9:] > >>>> Cheers, > >>>> Drea > >>> Sorry perhaps I didn't make it clear enough, so apologies. I only > >>> presented the example s = "FTPHOST: e4ftl01u.ecs.nasa.gov" as I > >>> thought this easily encompassed the problem. The solution presented > >>> works fine for this i.e. re.search(r'FTPHOST: (.*)',s).group(1). But > >>> when I used this on the actual file I am trying to parse I realised it > >>> is slightly more complicated as this also pulls out other information, > >>> for example it prints > >>> e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n', > >>> 'Ftp Pull Download Links: \r\n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/ > >>> 0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n', > >>> etc. So I need to find a way to stop it before the \r > >>> slicing the string wouldn't work in this scenario as I can envisage a > >>> situation where the string lenght increases and I would prefer not to > >>> keep having to change the string. > >> If, as Terry suggested, you do have a tuple of strings and the first > >> element has FTPHOST, then s[0].split(":")[1].strip() will work. > > > It is an email which contains information before and after the main > > section I am interested in, namely... > > > FINISHED: 09/07/2009 08:42:31 > > > MEDIATYPE: FtpPull > > MEDIAFORMAT: FILEFORMAT > > FTPHOST: e4ftl01u.ecs.nasa.gov > > FTPDIR: /PullDir/0301872638CySfQB > > Ftp Pull Download Links: > >ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB > > Down load ZIP file of packaged order: > >ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB.zip > > FTPEXPR: 09/12/2009 08:42:31 > > MEDIA 1 of 1 > > MEDIAID: > > > I have been doing this to turn the email into a string > > > email = sys.argv[1] > > f = open(email, 'r') > > s = str(f.readlines()) > > To me that seems a strange thing to do. You could just read the entire > file as a string: > > f = open(email, 'r') > s = f.read() > > > so FTPHOST isn't the first element, it is just part of a larger > > string. When I turn the email into a string it looks like... > > > 'FINISHED: 09/07/2009 08:42:31\r\n', '\r\n', 'MEDIATYPE: FtpPull\r\n', > > 'MEDIAFORMAT: FILEFORMAT\r\n', 'FTPHOST: e4ftl01u.ecs.nasa.gov\r\n', > > 'FTPDIR: /PullDir/0301872638CySfQB\r\n', 'Ftp Pull Download Links: \r > > \n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB\r\n', 'Down > > load ZIP file of packaged order:\r\n', > > > So not sure splitting it like you suggested works in this case. > > Within the file are a list of files, e.g. TOTAL FILES: 2 FILENAME: MOD13A2.A2007033.h17v08.005.2007101023605.hdf FILESIZE: 11028908 FILENAME: MOD13A2.A2007033.h17v08.005.2007101023605.hdf.xml FILESIZE: 18975 and what i want to do is get the ftp address from the file and collect these files to pull down from the web e.g. MOD13A2.A2007033.h17v08.005.2007101023605.hdf MOD13A2.A2007033.h17v08.005.2007101023605.hdf.xml Thus far I have #!/usr/bin/env python import sys import re import urllib email = sys.argv[1] f = open(email, 'r') s = str(f.readlines()) m = re.findall(r"MOD\.\.h..v..\.005\..\ \", s) ftphost = re.search(r'FTPHOST: (.*?)\\r',s).group(1) ftpdir = re.search(r'FTPDIR: (.*?)\\r',s).group(1) url = 'ftp://' + ftphost + ftpdir for i in xrange(len(m)): print i, ':', len(m) file1 = m[i][:-4] # remove xml bit. file2 = m[i] urllib.urlretrieve(url, file1) urllib.urlretrieve(url, file2) which works, clearly my match for the MOD13A2* files isn't ideal I guess, but they will always occupt those dimensions, so it should work. Any suggestions on how to improve this are appreciated. Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting patterns after matching a regex
On Sep 8, 3:14 pm, "Andreas Tawn" wrote: > > > > Hi, > > > > > I need to extract a string after a matching a regular expression. For > > > > example I have the string... > > > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > > > > > and once I match "FTPHOST" I would like to extract > > > > "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the > > > > problem, I had been trying to match the string using something like > > > > this: > > > > > m = re.findall(r"FTPHOST", s) > > > > > But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov" > > > > part. Perhaps I need to find the string and then split it? I had some > > > > help with a similar problem, but now I don't seem to be able to > > > > transfer that to this problem! > > > > > Thanks in advance for the help, > > > > > Martin > > > > No need for regex. > > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > > > If "FTPHOST" in s: > > > return s[9:] > > > > Cheers, > > > > Drea > > > Sorry perhaps I didn't make it clear enough, so apologies. I only > > presented the example s = "FTPHOST: e4ftl01u.ecs.nasa.gov" as I > > thought this easily encompassed the problem. The solution presented > > works fine for this i.e. re.search(r'FTPHOST: (.*)',s).group(1). But > > when I used this on the actual file I am trying to parse I realised it > > is slightly more complicated as this also pulls out other information, > > for example it prints > > > e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n', > > 'Ftp Pull Download Links: \r\n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/ > > 0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n', > > > etc. So I need to find a way to stop it before the \r > > > slicing the string wouldn't work in this scenario as I can envisage a > > situation where the string lenght increases and I would prefer not to > > keep having to change the string. > > If, as Terry suggested, you do have a tuple of strings and the first element > has FTPHOST, then s[0].split(":")[1].strip() will work. It is an email which contains information before and after the main section I am interested in, namely... FINISHED: 09/07/2009 08:42:31 MEDIATYPE: FtpPull MEDIAFORMAT: FILEFORMAT FTPHOST: e4ftl01u.ecs.nasa.gov FTPDIR: /PullDir/0301872638CySfQB Ftp Pull Download Links: ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB Down load ZIP file of packaged order: ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB.zip FTPEXPR: 09/12/2009 08:42:31 MEDIA 1 of 1 MEDIAID: I have been doing this to turn the email into a string email = sys.argv[1] f = open(email, 'r') s = str(f.readlines()) so FTPHOST isn't the first element, it is just part of a larger string. When I turn the email into a string it looks like... 'FINISHED: 09/07/2009 08:42:31\r\n', '\r\n', 'MEDIATYPE: FtpPull\r\n', 'MEDIAFORMAT: FILEFORMAT\r\n', 'FTPHOST: e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n', 'Ftp Pull Download Links: \r \n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n', So not sure splitting it like you suggested works in this case. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting patterns after matching a regex
On Sep 8, 4:33 pm, MRAB wrote: > Mart. wrote: > > On Sep 8, 3:53 pm, MRAB wrote: > >> Mart. wrote: > >>> On Sep 8, 3:14 pm, "Andreas Tawn" wrote: > >>>>>>> Hi, > >>>>>>> I need to extract a string after a matching a regular expression. For > >>>>>>> example I have the string... > >>>>>>> s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > >>>>>>> and once I match "FTPHOST" I would like to extract > >>>>>>> "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the > >>>>>>> problem, I had been trying to match the string using something like > >>>>>>> this: > >>>>>>> m = re.findall(r"FTPHOST", s) > >>>>>>> But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov" > >>>>>>> part. Perhaps I need to find the string and then split it? I had some > >>>>>>> help with a similar problem, but now I don't seem to be able to > >>>>>>> transfer that to this problem! > >>>>>>> Thanks in advance for the help, > >>>>>>> Martin > >>>>>> No need for regex. > >>>>>> s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > >>>>>> If "FTPHOST" in s: > >>>>>> return s[9:] > >>>>>> Cheers, > >>>>>> Drea > >>>>> Sorry perhaps I didn't make it clear enough, so apologies. I only > >>>>> presented the example s = "FTPHOST: e4ftl01u.ecs.nasa.gov" as I > >>>>> thought this easily encompassed the problem. The solution presented > >>>>> works fine for this i.e. re.search(r'FTPHOST: (.*)',s).group(1). But > >>>>> when I used this on the actual file I am trying to parse I realised it > >>>>> is slightly more complicated as this also pulls out other information, > >>>>> for example it prints > >>>>> e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n', > >>>>> 'Ftp Pull Download Links: \r\n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/ > >>>>> 0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n', > >>>>> etc. So I need to find a way to stop it before the \r > >>>>> slicing the string wouldn't work in this scenario as I can envisage a > >>>>> situation where the string lenght increases and I would prefer not to > >>>>> keep having to change the string. > >>>> If, as Terry suggested, you do have a tuple of strings and the first > >>>> element has FTPHOST, then s[0].split(":")[1].strip() will work. > >>> It is an email which contains information before and after the main > >>> section I am interested in, namely... > >>> FINISHED: 09/07/2009 08:42:31 > >>> MEDIATYPE: FtpPull > >>> MEDIAFORMAT: FILEFORMAT > >>> FTPHOST: e4ftl01u.ecs.nasa.gov > >>> FTPDIR: /PullDir/0301872638CySfQB > >>> Ftp Pull Download Links: > >>>ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB > >>> Down load ZIP file of packaged order: > >>>ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB.zip > >>> FTPEXPR: 09/12/2009 08:42:31 > >>> MEDIA 1 of 1 > >>> MEDIAID: > >>> I have been doing this to turn the email into a string > >>> email = sys.argv[1] > >>> f = open(email, 'r') > >>> s = str(f.readlines()) > >> To me that seems a strange thing to do. You could just read the entire > >> file as a string: > > >> f = open(email, 'r') > >> s = f.read() > > >>> so FTPHOST isn't the first element, it is just part of a larger > >>> string. When I turn the email into a string it looks like... > >>> 'FINISHED: 09/07/2009 08:42:31\r\n', '\r\n', 'MEDIATYPE: FtpPull\r\n', > >>> 'MEDIAFORMAT: FILEFORMAT\r\n', 'FTPHOST: e4ftl01u.ecs.nasa.gov\r\n', > >>> 'FTPDIR: /PullDir/0301872638CySfQB\r\n', 'Ftp Pull Download Links: \r > >>> \n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB\r\n', 'Down > >>> load ZIP file of packaged order:\r\n', >
Re: Extracting patterns after matching a regex
On Sep 8, 3:21 pm, nn wrote: > On Sep 8, 9:55 am, "Mart." wrote: > > > > > On Sep 8, 2:16 pm, "Andreas Tawn" wrote: > > > > > Hi, > > > > > I need to extract a string after a matching a regular expression. For > > > > example I have the string... > > > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > > > > > and once I match "FTPHOST" I would like to extract > > > > "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the > > > > problem, I had been trying to match the string using something like > > > > this: > > > > > m = re.findall(r"FTPHOST", s) > > > > > But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov" > > > > part. Perhaps I need to find the string and then split it? I had some > > > > help with a similar problem, but now I don't seem to be able to > > > > transfer that to this problem! > > > > > Thanks in advance for the help, > > > > > Martin > > > > No need for regex. > > > > s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > > > If "FTPHOST" in s: > > > return s[9:] > > > > Cheers, > > > > Drea > > > Sorry perhaps I didn't make it clear enough, so apologies. I only > > presented the example s = "FTPHOST: e4ftl01u.ecs.nasa.gov" as I > > thought this easily encompassed the problem. The solution presented > > works fine for this i.e. re.search(r'FTPHOST: (.*)',s).group(1). But > > when I used this on the actual file I am trying to parse I realised it > > is slightly more complicated as this also pulls out other information, > > for example it prints > > > e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n', > > 'Ftp Pull Download Links: \r\n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/ > > 0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n', > > > etc. So I need to find a way to stop it before the \r > > > slicing the string wouldn't work in this scenario as I can envisage a > > situation where the string lenght increases and I would prefer not to > > keep having to change the string. > > > Many thanks > > It is not clear from your post what the input is really like. But just > guessing this might work: > > >>> print s > > 'MEDIATYPE: FtpPull\r\n', 'MEDIAFORMAT: FILEFORMAT\r\n','FTPHOST: > e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r > \n','Ftp Pull Download Links: \r\n' > > >>> re.search(r'FTPHOST: (.*?)\\r',s).group(1) > > 'e4ftl01u.ecs.nasa.gov' Hi, That does work. So the \ escapes the \r, does this tell it to stop when it reaches the "\r"? Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Extracting patterns after matching a regex
On Sep 8, 4:33 pm, MRAB wrote: > Mart. wrote: > > On Sep 8, 3:53 pm, MRAB wrote: > >> Mart. wrote: > >>> On Sep 8, 3:14 pm, "Andreas Tawn" wrote: > >>>>>>> Hi, > >>>>>>> I need to extract a string after a matching a regular expression. For > >>>>>>> example I have the string... > >>>>>>> s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > >>>>>>> and once I match "FTPHOST" I would like to extract > >>>>>>> "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to the > >>>>>>> problem, I had been trying to match the string using something like > >>>>>>> this: > >>>>>>> m = re.findall(r"FTPHOST", s) > >>>>>>> But I couldn't then work out how to return the "e4ftl01u.ecs.nasa.gov" > >>>>>>> part. Perhaps I need to find the string and then split it? I had some > >>>>>>> help with a similar problem, but now I don't seem to be able to > >>>>>>> transfer that to this problem! > >>>>>>> Thanks in advance for the help, > >>>>>>> Martin > >>>>>> No need for regex. > >>>>>> s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > >>>>>> If "FTPHOST" in s: > >>>>>> return s[9:] > >>>>>> Cheers, > >>>>>> Drea > >>>>> Sorry perhaps I didn't make it clear enough, so apologies. I only > >>>>> presented the example s = "FTPHOST: e4ftl01u.ecs.nasa.gov" as I > >>>>> thought this easily encompassed the problem. The solution presented > >>>>> works fine for this i.e. re.search(r'FTPHOST: (.*)',s).group(1). But > >>>>> when I used this on the actual file I am trying to parse I realised it > >>>>> is slightly more complicated as this also pulls out other information, > >>>>> for example it prints > >>>>> e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n', > >>>>> 'Ftp Pull Download Links: \r\n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/ > >>>>> 0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n', > >>>>> etc. So I need to find a way to stop it before the \r > >>>>> slicing the string wouldn't work in this scenario as I can envisage a > >>>>> situation where the string lenght increases and I would prefer not to > >>>>> keep having to change the string. > >>>> If, as Terry suggested, you do have a tuple of strings and the first > >>>> element has FTPHOST, then s[0].split(":")[1].strip() will work. > >>> It is an email which contains information before and after the main > >>> section I am interested in, namely... > >>> FINISHED: 09/07/2009 08:42:31 > >>> MEDIATYPE: FtpPull > >>> MEDIAFORMAT: FILEFORMAT > >>> FTPHOST: e4ftl01u.ecs.nasa.gov > >>> FTPDIR: /PullDir/0301872638CySfQB > >>> Ftp Pull Download Links: > >>>ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB > >>> Down load ZIP file of packaged order: > >>>ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB.zip > >>> FTPEXPR: 09/12/2009 08:42:31 > >>> MEDIA 1 of 1 > >>> MEDIAID: > >>> I have been doing this to turn the email into a string > >>> email = sys.argv[1] > >>> f = open(email, 'r') > >>> s = str(f.readlines()) > >> To me that seems a strange thing to do. You could just read the entire > >> file as a string: > > >> f = open(email, 'r') > >> s = f.read() > > >>> so FTPHOST isn't the first element, it is just part of a larger > >>> string. When I turn the email into a string it looks like... > >>> 'FINISHED: 09/07/2009 08:42:31\r\n', '\r\n', 'MEDIATYPE: FtpPull\r\n', > >>> 'MEDIAFORMAT: FILEFORMAT\r\n', 'FTPHOST: e4ftl01u.ecs.nasa.gov\r\n', > >>> 'FTPDIR: /PullDir/0301872638CySfQB\r\n', 'Ftp Pull Download Links: \r > >>> \n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB\r\n', 'Down > >>> load ZIP file of packaged order:\r\n', >
Re: Extracting patterns after matching a regex
On Sep 9, 4:58 pm, Al Fansome wrote: > Mart. wrote: > > On Sep 8, 4:33 pm, MRAB wrote: > >>Mart. wrote: > >>> On Sep 8, 3:53 pm, MRAB wrote: > >>>>Mart. wrote: > >>>>> On Sep 8, 3:14 pm, "Andreas Tawn" wrote: > >>>>>>>>> Hi, > >>>>>>>>> I need to extract a string after a matching a regular expression. > >>>>>>>>> For > >>>>>>>>> example I have the string... > >>>>>>>>> s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > >>>>>>>>> and once I match "FTPHOST" I would like to extract > >>>>>>>>> "e4ftl01u.ecs.nasa.gov". I am not sure as to the best approach to > >>>>>>>>> the > >>>>>>>>> problem, I had been trying to match the string using something like > >>>>>>>>> this: > >>>>>>>>> m = re.findall(r"FTPHOST", s) > >>>>>>>>> But I couldn't then work out how to return the > >>>>>>>>> "e4ftl01u.ecs.nasa.gov" > >>>>>>>>> part. Perhaps I need to find the string and then split it? I had > >>>>>>>>> some > >>>>>>>>> help with a similar problem, but now I don't seem to be able to > >>>>>>>>> transfer that to this problem! > >>>>>>>>> Thanks in advance for the help, > >>>>>>>>> Martin > >>>>>>>> No need for regex. > >>>>>>>> s = "FTPHOST: e4ftl01u.ecs.nasa.gov" > >>>>>>>> If "FTPHOST" in s: > >>>>>>>> return s[9:] > >>>>>>>> Cheers, > >>>>>>>> Drea > >>>>>>> Sorry perhaps I didn't make it clear enough, so apologies. I only > >>>>>>> presented the example s = "FTPHOST: e4ftl01u.ecs.nasa.gov" as I > >>>>>>> thought this easily encompassed the problem. The solution presented > >>>>>>> works fine for this i.e. re.search(r'FTPHOST: (.*)',s).group(1). But > >>>>>>> when I used this on the actual file I am trying to parse I realised it > >>>>>>> is slightly more complicated as this also pulls out other information, > >>>>>>> for example it prints > >>>>>>> e4ftl01u.ecs.nasa.gov\r\n', 'FTPDIR: /PullDir/0301872638CySfQB\r\n', > >>>>>>> 'Ftp Pull Download Links: \r\n', 'ftp://e4ftl01u.ecs.nasa.gov/PullDir/ > >>>>>>> 0301872638CySfQB\r\n', 'Down load ZIP file of packaged order:\r\n', > >>>>>>> etc. So I need to find a way to stop it before the \r > >>>>>>> slicing the string wouldn't work in this scenario as I can envisage a > >>>>>>> situation where the string lenght increases and I would prefer not to > >>>>>>> keep having to change the string. > >>>>>> If, as Terry suggested, you do have a tuple of strings and the first > >>>>>> element has FTPHOST, then s[0].split(":")[1].strip() will work. > >>>>> It is an email which contains information before and after the main > >>>>> section I am interested in, namely... > >>>>> FINISHED: 09/07/2009 08:42:31 > >>>>> MEDIATYPE: FtpPull > >>>>> MEDIAFORMAT: FILEFORMAT > >>>>> FTPHOST: e4ftl01u.ecs.nasa.gov > >>>>> FTPDIR: /PullDir/0301872638CySfQB > >>>>> Ftp Pull Download Links: > >>>>>ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB > >>>>> Down load ZIP file of packaged order: > >>>>>ftp://e4ftl01u.ecs.nasa.gov/PullDir/0301872638CySfQB.zip > >>>>> FTPEXPR: 09/12/2009 08:42:31 > >>>>> MEDIA 1 of 1 > >>>>> MEDIAID: > >>>>> I have been doing this to turn the email into a string > >>>>> email = sys.argv[1] > >>>>> f = open(email, 'r') > >>>>> s = str(f.readlines()) > >>>> To me that seems a strange thing to do. You could just read the entire > >>>> file as a string: > >>&
Re: Mapping in python? Transforming shapefile so that basemap can read them?
On Sep 11, 7:22 pm, C Barr Leigh wrote: > I'm trying to get started with plotting maps in python. I need to read > "shape files" (.shp) and make maps. There seem to be many efforts but > none is complete? I'm looking for suggestions and troubleshooting. > > The basemap package is obviously at an impressive stage and comes with > some data:http://www.scipy.org/Cookbook/Matplotlib/Maps > > but it cannot read shapefiles when their coordinates are not in > geographic projection. min are in a lambert, so the readshapefile > fails. > > Apparently there is a utility that can convert a .shp file to lat/lon > coordinates, but it fails for me. “You can convert the shapefile to > geographic - coordinates using the shpproj utility from the shapelib > tools - (http://shapelib.maptools.org/shapelib-tools.html)" > For me, this gives: > “unable to process projection, exiting...” > > Has anyone overcome these problems? > > Thanks! > Chris Not sure - but perhaps the GDAL? -- http://mail.python.org/mailman/listinfo/python-list
Re: looking for a script that automaticly logs into hotmail
On Sep 11, 10:31 pm, mmoalem wrote: > hi there all - trying to find a script wich will log into an hotmail > account - that is for the reason that after a certain amount of > inactivity time a hotmail account is expired and to prevent that from > happening accidentaly a script wich i can schedule to run every month > and can log into the account will be very usefull - is anything like > this exist? lots of stuff if you google for python and gmail. So maybe you could work from that? -- http://mail.python.org/mailman/listinfo/python-list
Unofficial PyBitmessage port to run with Python3 and PyQt5
from https://www.reddit.com/r/bitmessage/comments/1d5ff18/unofficial_pybitmessage_port_to_run_with_python3/ Unofficial PyBitmessage port to run with Python3 and PyQt5 The official PyBitmessage still runs with outdated Python2 and PyQt4. Recently, I'm trying to port PyBitmessage to run with Python3 and PyQt5. Although it's unofficial version and still should have some bugs, it seems now running as expected as long as I use it. I use PyBitmessage mostly for communications on chans. One-to-one messaging is not tested well since I have no friends at all. The source code is published at GutHub: https://github.com/kashikoibumi/PyBitmessage . The default branch 'py3qt' is most matured among other many branches. If you try to use it, at first backup your PyBitmessage databases and settings which are found $HOME/.config/PyBitmessage/ if you are using Linux. Any bug reports or comments are welcome. I'm using it on Devuan GNU+Linux Daedalus which is mostly compatible to Debian GNU/Linux bookworm except systemd utilizations. All dependencies are installed from Devuan (Debian) packages. ─┏┓──┏━━┓───┏━━┓──Spooky Mart Channel ─┗━━┓─┃──┗┓─┃───┗┓─┃──[chan] 711 ┃─┃──┏┛─┗┓──┏┛─┗┓─always open | stay spooky ┗━┛──┗━━━┛──┗━━━┛─https://bitmessage.org -- https://mail.python.org/mailman/listinfo/python-list
PyBitmessage is not dead. Ignore the FUD.
from [chan] bitmessage PyBitmessage is not dead. Ignore the FUD. I think Peter and gang just got tired of responding to this recurring claim. I wouldn't even expect a rebuttal from them at this point. Newer forks have been under development, one in Python3 and one in Rust. Here is the PyBitmessage fork in Python3: https://github.com/kashikoibumi/PyBitmessage Somebody should help the maintainer. I am no longer a Pythonista or I would. I dumped Python after the 2.7 sunset as I expect they will likely break Py3 equally bad some day. I will be occasionally reviewing Koibumi's bash scripts, build scripts and documentation looking for errors or points of improvement. ─┏┓──┏━━┓───┏━━┓── Spooky Mart Channel ─┗━━┓─┃──┗┓─┃───┗┓─┃──[chan] 711 ┃─┃──┏┛─┗┓──┏┛─┗┓─always open | stay spooky ┗━┛──┗━━━┛──┗━━━┛─https://bitmessage.org -- https://mail.python.org/mailman/listinfo/python-list
Python3 Fork of BMWrapper
from https://github.com/kashikoibumi/bmwrapper bmwrapper is a poorly hacked together python script to let Thunderbird and PyBitmessage communicate, similar to AyrA's (generally much better) application: Bitmessage2Mail. I'm on Linux, and don't feel like dealing with wine. So I wrote this to fill the same role as B2M, until the source code was released. (Which has since been open-sourced: https://github.com/AyrA/BitMailServer) The script (usually) parses outgoing messages to strip the ugly email header information and put quoted text in PyBitmessage’s '---’ delimited form. Attached images are included, base64 encoded, in an img tag. Incoming messages are likewise parsed to reconstruct a email, with attachment. This works...most of the time, and I’ve tried to make it fail gracefully when something goes wrong. ─┏┓──┏━━┓───┏━━┓──Spooky Mart Channel ─┗━━┓─┃──┗┓─┃───┗┓─┃──[chan] 711 ┃─┃──┏┛─┗┓──┏┛─┗┓─always open | stay spooky ┗━┛──┗━━━┛──┗━━━┛─https://bitmessage.org -- https://mail.python.org/mailman/listinfo/python-list
BitChan (python project)
from https://github.com/813492291816/BitChan BitChan is a decentralized anonymous imageboard inspired by BitBoard and built on top of Bitmessage with Tor, I2P, and GnuPG. BitChan solves a number of security and free speech problems that have plagued most imageboards. Centralized imageboards can be taken offline or hijacked and can leak user data. BitChan reduces the likelihood of this by being decentralized, allowing each user to host their own instance of the software, requiring all connections to go through Tor/I2P, and not requiring JavaScript. Users of centralized forums often have to deal with overzealous moderators and sometimes even pressure from state powers that tend to suffocate the forum's culture. BitChan's moderation is multifaceted, but to be brief, the option exists to create entirely unmoderatable boards to post content on. Due to its decentralized design, BitChan cannot be moderated by its developers, the government, or any other entity. Indeed, there is no way to disconnect BitChan from the internet, and as long as people are still running Bitmessage, BitChan is completely untouchable. ─┏┓──┏━━┓───┏━━┓── Spooky Mart Channel ─┗━━┓─┃──┗┓─┃───┗┓─┃──[chan] 711 ┃─┃──┏┛─┗┓──┏┛─┗┓─always open | stay spooky ┗━┛──┗━━━┛──┗━━━┛─https://bitmessage.org -- https://mail.python.org/mailman/listinfo/python-list