Re: Help with code = Extract numerical value to variable
Sorry I'm not being clear Input** sold: 16 sold: 20 sold: 2 sold: 0 sold: storefront 7 0 storefront sold null Output 16 20 2 0 0 7 0 0 0 0 -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with code = Extract numerical value to variable
Steve zerocostprod...@gmail.com wrote: If there is a number in the line I want the number otherwise I want a 0 I don't think I can use strip because the lines have no standards What do you think strip() does? Read http://docs.python.org/library/stdtypes.html#str.lstrip *carefully* (help(''.lstrip) is slightly ambiguous). set(string.printable).difference(string.digits) may help. -- \S under construction -- http://mail.python.org/mailman/listinfo/python-list
Help with code = Extract numerical value to variable
Steve wrote: Sorry I'm not being clear Input** sold: 16 sold: 20 sold: 2 sold: 0 sold: storefront 7 0 storefront sold null Output 16 20 2 0 0 7 0 0 0 0 Since you're looking for only digits, simply make a string containing all characters that aren't digits. Now, loop through the file and use str.translate() to delete all the non-digits from each line, using the above table. Check if the result is , and if so, substitute 0. Done. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with code = Extract numerical value to variable
On 10/23/2009 05:16 AM, Dave Angel wrote: Steve wrote: Sorry I'm not being clear Input** sold: 16 sold: 20 sold: 2 sold: 0 sold: storefront 7 0 storefront sold null Output 16 20 2 0 0 7 0 0 0 0 Since you're looking for only digits, simply make a string containing all characters that aren't digits. Now, loop through the file and use str.translate() to delete all the non-digits from each line, using the above table. Check if the result is , and if so, substitute 0. Done. delchars = ''.join([chr(i) for i in range (256) if chr(i)'0' or chr(i) '9']) f = open ('your_input_filename.txt') for line in f: num = line.translate (None, delchars) if num == '': num = '0' print num IMO, Python's translate function in unpleasant enough to use that I might use a regex here, although the code above is likely faster if you are running this on large input files. import re f = open (your_input_filename.txt) for line in f: mo = re.search ('\d+', line) if mo: print mo.group(0) else: print '0' -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with code = Extract numerical value to variable
On Thu, 22 Oct 2009 10:27:57 -0700 (PDT), Steve wrote: I have some data that I'm performing some analysis on. How do I grab the numerical value if it's present and ignore otherwise. So in the following example I would have assign the following values to my var 16 20 2 7 0 In Field6 Sample String data is sold: 16 sold: 20 sold: 2 sold: 0 sold: storefront 7 0 storefront sold null What result do you want from these lines: sold: 1 2 3 sold: 123x 4 sold: 5 y 6 sold: 3-2 sold: 3 - 2 sold: -3 sold: 1.5 sold: 1e6 sold: 2*2 -- To email me, substitute nowhere-spamcop, invalid-net. -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with code = Extract numerical value to variable
On Oct 22, 11:27 am, Steve zerocostprod...@gmail.com wrote: I have some data that I'm performing some analysis on. How do I grab the numerical value if it's present and ignore otherwise. So in the following example I would have assign the following values to my var 16 20 2 7 0 In Field6 Sample String data is sold: 16 sold: 20 sold: 2 sold: 0 sold: storefront 7 0 storefront sold null So you want a list of any number that occurs in the file, if it occurs at the start of the line, or is preceded by a space character? And you don't want any duplicates in the list? Is the order of the results important? That is, would a result of 16,20,2,0,7 be acceptable? Or how about 20,2,16,7,0? -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with code = Extract numerical value to variable
If there is a number in the line I want the number otherwise I want a 0 I don't think I can use strip because the lines have no standards Thanks again Steve On Oct 22, 1:53 pm, ru...@yahoo.com wrote: On Oct 22, 11:27 am, Steve zerocostprod...@gmail.com wrote: I have some data that I'm performing some analysis on. How do I grab the numerical value if it's present and ignore otherwise. So in the following example I would have assign the following values to my var 16 20 2 7 0 In Field6 Sample String data is sold: 16 sold: 20 sold: 2 sold: 0 sold: storefront 7 0 storefront sold null So you want a list of any number that occurs in the file, if it occurs at the start of the line, or is preceded by a space character? And you don't want any duplicates in the list? Is the order of the results important? That is, would a result of 16,20,2,0,7 be acceptable? Or how about 20,2,16,7,0?- Hide quoted text - - Show quoted text - -- http://mail.python.org/mailman/listinfo/python-list
Re: Help with code = Extract numerical value to variable
Sorry, I still don't understand. I gather you don't literally want a 0 output for every line that does not contain a number since then your output would be, 0, 0, 16, 20, ... (I'm assuming you want to ignore the 6 in In Field6) which does not match your sample output. Do you mean, for every sold: line that does not contain a number (for example sold: storefront) you want a 0? That does not match your output either since then I'd expect 16, 20, 2, 0, 0, 7, 0. Are you sure your sample output is really what you expect from the sample input? Specifically, which line of your output resulted from the sold: 0 input line? Which output line resulted from the input line 0? On 10/22/2009 04:41 PM, Steve wrote: If there is a number in the line I want the number otherwise I want a 0 I don't think I can use strip because the lines have no standards On Oct 22, 1:53 pm, ru...@yahoo.com wrote: On Oct 22, 11:27 am, Steve zerocostprod...@gmail.com wrote: I have some data that I'm performing some analysis on. How do I grab the numerical value if it's present and ignore otherwise. So in the following example I would have assign the following values to my var 16 20 2 7 0 In Field6 Sample String data is sold: 16 sold: 20 sold: 2 sold: 0 sold: storefront 7 0 storefront sold null -- http://mail.python.org/mailman/listinfo/python-list