Help with code = Extract numerical value to variable

S

Steve

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
 
P

Peter Pearson

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
 
R

rurpy

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?
 
S

Steve

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
 
R

rurpy

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"?
 
S

Steve

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
 
D

Dave Angel

Steve said:
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
 
R

rurpy

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)
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'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top