I want to calculate with a value in my dataframe, however, this string consists of an exponential number ('10⁻³'). Is this some kind of encoding issue? How can I convert this string into a float (e.g. 10e-3) so that can perform calculations with this value?
(using Python 3.8.8)
First problem is to convert the Unicode symbols to something easier to work with.
import unidecode
simpler = unidecode.unidecode('10⁻³')
Now you can put an 'e' in front of any '-' or '+':
simpler = simpler.replace('-', 'e-').replace('+', 'e+')
Now you have a format you can give to float.
f = float(simpler)
Related
i have a string like '0.00008400' and i just want to turn it 0.00008400 (float). But python changes it to scientific notation type and it is looking like 8.4e-05,
float('0.00008400') = 8.4e-05 #as float
I saw some formatting answers but they are turning it to string instead of float.
format(8.4e-05, '.8f') = 0.00008400 #as string
and of course I can't turn this string value into float again...
#stupid alert
float(format(8.4e-05, '.8f')) = 8.4e-05
I just want to my string type input to turn float exactly...
'0.00008400' => 0.00008400 #string to float
Thanks in advance...
Edit:
there is a function that I want to use gets float value but can't understand scientific notion format of float. So this is why I want to show this float normal.
SORRY:
string format is okay too. Sorry.
The scientific representation is "just" a representation of your float, that does not change the real value of the float, whatever how python display it.
So I don't understand what you want to do. Could you send more informations ?
I am using python v3. I have this string 1,027.86. I want to convert it to float 1027.86.
I found a solution while googling.
import locale
locale.setlocale(locale.LC_NUMERIC, "nl")
price = '1,027.86'
price = locale.atof(price)*1000
I searched the documentation on what locale.setlocale(locale.LC_NUMERIC, "nl") means but could not find an answer.
http://dc.dyu.edu.tw/python/350/library/locale.html
Is there a better argument to put inside setlocal() that will return the result directly without the need of multiplying by 1000 later?
Specifying nl for setlocale() is telling it to default to the format for the Netherlands. If you use something like uk it should convert correctly as the numeric format is of the formxxx,xxx,xxx.xxx.
import locale
locale.setlocale(locale.LC_NUMERIC, "uk")
price = '1,027.86'
print(locale.atof(price))
This would display:
1027.86
You can use a simple string replacement to turn it into a float that can be properly parsed price = float('1,027.86'.replace(',','')).
I am trying to figure out how to do some nice type inference on the columns of a CSV file.
Are there any libraries that might tell me, for example, that a column contains only integers?
All values are of course available in string format.
I will write my own tool if nothing of this sort already exists, but it seems weird to me that such a basic task does not have a library counterpart somewhere.
Why don't you do the straightforward approach?
if all values can be parsed as integers, to column is integers
otherwise, if all values can be parsed as doubles, to column is doubles
otherwise, the column is all strings
The reason why there is no library for this is probably because it's trivial to implement using the existing string to int and string to double conversion functions.
Regular expressions are good for that, in Python, you could use something like this:
import re
def str_is_num(s):
number_pattern = re.compile("-?^\d+(\.\d+)?$")
return re.match(number_pattern, s) != None
To check whether a cell is a number, you can evaluate str_is_num(cell)
I have just learned Python for this project I am working on and I am having trouble comparing two values - I am using the Python xlwt and xlrd libraries and pulling values of cells from the documents. The problem is some of the values are in the format 'NP_000000000', 'IPI00000000.0', and '000000000' so I need to check which format the value is in and then strip the characters and decimal points off if necessary before comparing them.
I have tried using S1[:3] to get the value without alphabet characters, but I get a 'float is not subscriptable' error
Then I tried doing re.sub(r'[^\d.]+, '', S1) but I get a Typerror: expected a string or buffer
I figured since the value of the cell that is being returned via sheet.cell( x, y).value would be a string since it is alphanumeric, but it seems like it must be returned as a float
What is the best way to format these values and then compare them?
You are trying to get the numbers from the strings in the format shown? Like to get 2344 from NP_2344? If yes then use this
float(str(S1)[3:])
to get what you want. You can change float to int.
It sounds like the API you're using is returning different types depending on the content of the cells. You have two options.
You can convert everything to a string and then do what you're currently doing:
s = str(S1)
...
You can check the types of the input and act appropriately:
if isinstance(S1, basestring):
# this is a string, strip off the prefix
elif isinstance(S1, float):
# this is a float, just use it
i wrote a simple function to write into a text file. like this,
def write_func(var):
var = str(var)
myfile.write(var)
a= 5
b= 5
c= a + b
write_func(c)
this will write the output to a desired file.
now, i want the output in another format. say,
write_func("Output is :"+c)
so that the output will have a meaningful name in the file. how do i do it?
and why is that i cant write an integer to a file? i do, int = str(int) before writing to a file?
You can't add/concatenate a string and integer directly.
If you do anything more complicated than "string :"+str(number), I would strongly recommend using string formatting:
write_func('Output is: %i' % (c))
Python is a strongly typed language. This means, among other things, that you cannot concatenate a string and an integer. Therefore you'll have to convert the integer to string before concatenating. This can be done using a format string (as Nick T suggested) or passing the integer to the built in str function (as NullUserException suggested).
Simple, you do:
write_func('Output is' + str(c))
You have to convert c to a string before you can concatenate it with another string. Then you can also take off the:
var = str(var)
From your function.
why is that i cant write an integer to
a file? i do, int = str(int) before
writing to a file?
You can write binary data to a file, but byte representations of numbers aren't really human readable. -2 for example is 0xfffffffe in a 2's complement 32-bit integer. It's even worse when the number is a float: 2.1 is 0x40066666.
If you plan on having a human-readable file, you need to human-readable characters on them. In an ASCII file '0.5' isn't a number (at least not as a computer understands numbers), but instead the characters '0', '.' and '5'. And that's why you need convert your numbers to strings.
From http://docs.python.org/library/stdtypes.html#file.write
file.write(str)
Write a string to the file. There is no return value. Due to buffering,
the string may not actually show up in
the file until the flush() or close()
method is called.
Note how documentation specifies that write's argument must be a string.
So you should create a string yourself before passing it to file.write().