Python read non standard number format [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
What is the fastes way to convert a string to float if it doesn has a standard format?
In my special case I need to read these strings and convert them to float
-7.5-4
7.5-5
that correspond to the numbers -7.5E-4 and 7.5E-5
I need the fastest because I'm loading big size files.
Thanks

This lambda works with your test cases (also with a leading '+'):
to_num = lambda s: (1,-1)[s[0]=='-']*
float(s.lstrip('-+').replace('-','E-').replace('+','E+'))
The opening (1,-1)[s[0]=='-'] takes care of multiplying by -1 if there is a leading '-', then the float conversion strips leading '+' and '-' signs, and replaces embedded '+' and '-' with 'E+' and 'E-', permitting a valid conversion to float.

Related

How to replace double quotes inside single quotes using Regex in Python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 12 months ago.
Improve this question
How can I replace double quotes inside single quoted sections with something like \"?
Given this text:
{['abc.abc',"dsa",asd:'<td id="ssa" width="2px" class="odd-column">']}
I want it to be:
{['abc.abc',"dsa",asd:'<td id=\"ssa\" width=\"2px\" class=\"odd-column\">']}
In a later step I will replace all single quotes to get proper json for convertion.
Here is a python script which does what you are asking for.
We have to escape all the " and \
import re
string = r"""{['abc.abc',"dsa",asd:'<td id="ssa" width="2px" class="odd-column">']}"""
print(string)
sstring = re.sub(r'\"','\\"',string)
print(sstring)
the output is
{['abc.abc',\"dsa\",asd:'<td id=\"ssa\" width=\"2px\" class=\"odd-column\">']}

Why python not returning it's value in double quotes when it is string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I have converted integer value to string although it is not returning in quotes ad strings does
[cmd ][1]
print(str(31))
31 #I think it should give result as "31" because now it is string
When string is printed, quotes are usually not added. If you wanna see quote then use the repr() function. For example,
print(repr("31"))
Because while returning it prints the value, the double quotes are not printed. Though it will be treated as a string, you can verify it by using concatenation through + or string multiplication as str(32)*3 this will give you 323232

Python: Verifying a string represents a valid US currency value? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
How can I verify that a string represents a valid US currency value?
The string might be composed strictly of digits, or optionally have a dollar sign, commas, etc.
Context: I wish to verify a string is a proper dollar value, and then convert it to a number after removing or otherwise handling the non-numeric characters.
Regular Expression
Example: \$?(-?(\d+[,.])*\d+)
import re
re.match("\$?(-?(\d+[,.])*\d+)", "$-12,000.01") # match
re.match("\$?(-?(\d+[,.])*\d+)", "$-12,000.01").group(1) # extract matched value
>>> '-12,000.01'
re.sub('[,$]', '', '$-12,000.01') # remove comma and dollar sign
>>> '-12000.01'
float(re.sub('[,$]', '', '$-12,000.01')) # convert to float if the result doesn't contain any special character such as comma
>>> -12000.01
Add more cases to the regular expression if there are any in your dataset.
There can be many edge-cases that are invalid such as 13.000,000
This regular expression will fix it: \$?(-?\d*(\d+,)*\.?\d+).
So add as many cases as you need.

need space between two words in python with two inputs [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to put in 2 inputs then get them as an output, but I cant seem to get a space between them at the end . I have tried a + sign, space between x and y, and a comma and none work.
A formatted string will work nicely here.
We can provide variables to a string that has special format markers, like so:
print('{0} {1}'.format(word1, word2))
Or, more easily, a string concatenation:
print(word1 + ' ' + word2)

Explanation of str.partition() in python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am having a hard time understanding str.partition() function in python. I have read the definition of the function and searched online without finding an explanation that makes sense to me.
I have some code that uses it pretty heavily and have been trying to understand it. I could post the code if it would help but it is a pretty precise code segment that would probably complicate things.
Need in-depth, probably low-level, explanation of str.partition() function in python.
The docs are pretty clear ...
Split the string at the first occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.
So ...
>>> 'test'.partition('s')
('te', 's', 't')
>>> 'test'.partition('a')
('test', '', '')
You either get the front, splitter character, and tail, or you get the full string and two blank strings (depending on whether or not the partition character is present).

Categories