Converting a stripped string into an interger? - python

I'm trying to strip a string from all text and turn it into an integer so that I can use if statements on it easier...
Example:
count = "19 Count"
count = count.replace(" Count", "")
print repr(count)
print int(count)
Example Output:
'19'
19
However in my actual code my output is:
Real Output:
u'19'
Traceback (most recent call last):
File "C:\test.py", line 153, in <module>
print int(count)
ValueError: invalid literal for int() with base 10: ''

The reason for error ValueError: invalid literal for int() with base 10: '' is you are passing empty string in int(). Like int(''). The main problem is in the code which strips the non-digit characters.
Regex can be used to get first digits.
In [3]: import re
In [4]: a = re.search('\d+', ' 18 count 9 count')
In [5]: int(a.group())
Out[5]: 18

Checking digit for every word and used Filter to output if it it's true.
>>> count = "19 Count"
>>> filter(lambda x:x.isdigit(), count)
'19'

Try this, to meet your requirements. Will only match the first "set of numbers":
import re
regex = re.compile("(\d+)")
r = regex.search("19 Count 1 Count")
print(int(r.group(1)))
Output:
19
You can try out the code here: http://ideone.com/OwbMYm

Related

How to format a string with a width and a literal?

I have Python (2.7) code which takes a float, formats it with thousand-separating commas and 3 decimal places, and adds the string literal " sec" afterwards.
The result is then formatted further by being aligned left and given a width of 20:
num = '{:,.3f} sec'.format(1200300.4443333333)
print '{:<20}'.format(num) + 'more'
Ouput:
1,200,300.444 sec more
I wanted to condense this into a single format call, but I couldn't figure out how to use the width properly with the string literal.
I tried the following:
num = '{:,.3f}'.format(1200300.4443333333)
print '{:<20} sec'.format(num) + 'more'
But the output isn't the same:
1,200,300.444 secmore
I also tried the following:
num = '{:,.3f}'.format(1200300.4443333333)
print '{:<20 sec}'.format(num) + 'more'
But that failed:
Traceback (most recent call last):
File "test.py", line 8, in <module>
print '{:<20 sec}'.format(num) + 'more'
ValueError: Invalid conversion specification
Is there any way to condense the initial code into a single format call?
Not sure if it is but the following code might be what you're after:
num = '{:<20,.3f} sec more'.format(1200300.4443333333)
print(num)
# 1,200,300.444 sec more

Invalid literal for float

I am having the hardest time figuring out why the scientific notation string I am passing into the float() function will not work:
time = []
WatBalR = []
Area = np.empty([1,len(time)])
Volume = np.empty([1,len(time)])
searchfile = open("C:\GradSchool\Research\Caselton\Hydrus2d3d\H3D2_profile1v3\Balance.out", "r")
for line in searchfile:
if "Time" in line:
time.append(re.sub("[^0-9.]", "", line))
elif "WatBalR" in line:
WatBalR.append(re.sub("[^0-9.]", "", line))
elif "Area" in line:
Area0 = re.sub("[^0-9.\+]", "", line)
print repr(Area0[:-10])
Area0 = float(Area0[:-10].replace("'", ""))
Area = numpy.append(Area, Area0)
elif "Volume" in line:
Volume0 = re.sub("[^0-9.\+]", "", line)
Volume0 = float(Volume0[:-10].replace("'", ""))
Volume = numpy.append(Volume, Volume0)
searchfile.close()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-80-341de12bbc94> in <module>()
13 Area0 = re.sub("[^0-9.\+]", "", line)
14 print repr(Area0[:-10])
---> 15 Area0 = float(Area0[:-10].replace("'", ""))
16 Area = numpy.append(Area, Area0)
17 elif "Volume" in line:
ValueError: invalid literal for float(): 0.55077+03
However, the following works:
float(0.55077+03)
3.55077
If I put quotes around the argument, the same invalid literal comes up, but I am tried to remove the quotes from the string and cannot seem to do so.
0.55077+03 is 0.55077 added to 03. You need an e for scientific notation:
0.55077e+03
float(0.55077+03) adds 3 to .55077 and then converts it to a float (which it already is).
Note that this also only works on python2.x. On python3.x, 03 is an invalid token -- the correct way to write it there is 0o3...
float('0.55077+03') doesn't work (and raises the error that you're seeing) because that isn't a valid notation for a python float. You need: float('0.55077e03') if you're going for a sort of scientific notation. If you actually want to evaluate the expression, then things become a little bit trickier . . .

Why am I receiving this error when sorting a Text file by a certain column based upon number?

My code for the sorting of the file.
g = open('Lapse File.txt', 'r')
column = []
i = 1
next(g)
for line in g:
column.append(int(line.split('\t')[2]))
column.sort()
This is the error I get.
Traceback (most recent call last):
File "E:/Owles/new lapse .py", line 51, in <module>
column.append(int(line.split('\t')[2]))
ValueError: invalid literal for int() with base 10: '-8.3\n
My main question is why is there a \n. Earlier in the code I had written to another text file and wrote it by column from a previously read in file.
This is my code for writing the file
for columns in (raw.strip().split() for raw in Sounding):
if (i >2 and i <=33):
G.write(columns [3]+'\t'+columns[2]+'\t'+columns[4]+'\n')
i = i + 1
elif (i >= 34):
G.write(columns [0]+'\t'+columns[1]+'\t'+columns[2]+'\n')
i = i + 1
else:
i = i + 1
I am unsure if writing the lines like that is the issue because I have inserted the new line function.
The traceback is telling you exactly what happened:
ValueError: invalid literal for int() with base 10: '-8.3\n'
The problem here is that, while int() can handle the negative sign and the trailing newline character, it can't handle the decimal point, '.'. As you know, -8.3 may be a real, rational number, but it's not an integer. If you want to preserve the fractional value to end up with -8.3, use float() instead of int(). If you want to discard the fractional value to end up with -8, use float() to parse the string and then use int() on the result.
-8.3:
column.append(float(line.split('\t')[2]))
-8:
column.append(int(float(line.split('\t')[2])))
Because only numeric strings can be cast to integers; look at this:
numeric_string = "109"
not_numeric_string = "f9"
This is okay:
>>> int(numeric_string)
109
And it cannot be cast:
>>> int(not_numeric_string)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'f9'
So somewhere in your script it is getting a non-numeric string.
It seems as though the "-8.3\n" string sequence has raised the error, so you must strip escape chars as well.

python retrieve integer value of tag with minidom [duplicate]

This question already has answers here:
How to extract a floating number from a string [duplicate]
(7 answers)
Closed 8 years ago.
I would like to recover the integer value of a tag
from xml.dom import minidom
docXML = minidom.parse('/root/Desktop/doc.xml')
node = docXML.getElementsByTagName('span')[0]
value = node.firstChild.data
" return value is 5.22%"
str1 = value.split('%')
"return [u'\n5.22', u'\n']"
finalvalue = ''.join(str1)
"return 5.22"
but if I would to convert this string character
convert = int(finalvalue)
I got the following error
"ValueError invalid literal for int() with base 10: '5.22 ' "
When I use the split method I get the following result:
[u'\n5.22', u'\n']
use strip() to remove whitespaces from the string before converting to integer, & use float(the_str) to convert it to float.
>>> num_str = '5.22 '
>>> int(num_str)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '5.22 '
>>> float(num_str.strip())
5.22

How to convert str to int and add them together?

I've spent the last 2 hours trying to find a solution for this and came up with nothing. So either this is not possible or its so basic that no one write about this. Basically I have 2 strings that both equal numbers, but when I go to add them together I get a concatenate instead of a number.. here is my code (Python)
currentNukeScriptName = nuke.root().name()
splitUpScriptName1 = currentNukeScriptName.split('/')
splitUpScriptName2 = splitUpScriptName1[-1]
splitScriptNameAndExtention = splitUpScriptName2.split('.')
currentNukeScriptName = splitScriptNameAndExtention[0]
splitUpCurrentScriptName = currentNukeScriptName.split('_')
currentVersionNumber = splitUpCurrentScriptName[-1]
decimalVersionNumber = "1" + "," + str(currentVersionNumber)
addingNumber = 1
newVersionNumber = str(decimalVersionNumber) + str(addingNumber)
print newVersionNumber
decimaleVersionNumber = 1,019
If I change the newVersionNumber code too:
newVersionNumber = int(decimalVersionNumber) + int(addingNumber)
I get:
# Result: Traceback (most recent call last):
File "<string>", line 10, in <module>
ValueError: invalid literal for int() with base 10: '1,019'
I am unsure what to do.. Is this not possible? Or am I doing something totally wrong?
Edit:
So the problem was found in the decimalVersionNumber where I was adding a comma. What would be the best way of keeping the comma and still adding the numbers together?
ValueError: invalid literal for int() with base 10: '1,019'
Sounds like it doesn't like the comma - try removing it first.
You need to use
int.Parse(decimalVersionNumber) + int.Parse(addingNumber)
This will parse the string representation of the numbers into integers, so they can be added.
eg:
String concatenation:
"10" + "20" = "1020"
Integer addition, parsed from strings:
int.Parse("10") + int.Parse("20") = 30

Categories