python - splitting string by first occurrence of Alpha character - python-3.x

I have a list of string like follow (each line is a string variable)
6.40 D5
8.45 F5
9.00 E5
10.30 D5
12.30 E5
13.00HUm5 <-- outlier, without space
13.15 F5
15.05 F5
15.45 Fm5
I simply want to split the time (the front one) from the attribute (the back one).
This can be simply achieved by str.split().
However, the problem is that there may be a case about an outlier string like 13.00HUm5, where there are no white space between time and attribute.
It will fail if I use the code above.
I want it to be ['13.00', 'HUm5']
How can I tell the code to split the string when an Alpha characters appears after a Digit characters ?
Is it possible to achieve something like that ?

Using Regex.
Ex:
import re
s = """6.40 D5
8.45 F5
9.00 E5
10.30 D5
12.30 E5
13.00HUm5
13.15 F5
15.05 F5
15.45 Fm5"""
for line in s.splitlines():
m = re.match(r"(\d+\.\d+)\s*(.*)$", line)
if m:
print([m.group(1), m.group(2)])
Output:
['6.40', 'D5']
['8.45', 'F5']
['9.00', 'E5']
['10.30', 'D5']
['12.30', 'E5']
['13.00', 'HUm5']
['13.15', 'F5']
['15.05', 'F5']
['15.45', 'Fm5']

Related

Why show in a cell result #VALUE

I want to get the result from the cell in excel so that if F8 = .01 to .6 then it will show E8&A and if F8 = .7 to .125 then it will show E8&B. If not anything then it will show E8. I have wirtten that statement
(=IF(AND(F8>=0.01,F8<=0.06),E8&"A"),IF(AND(F8>=0.07,F8<=0.125),E8&"B",E8)
but it show #VALUE , what is the problem? Please help me
VALUE does not show after input the statement from your suggestion that
=IF(AND(F8>=0.01,F8<=0.06),E8&"A",IF(AND(F8>=0.07,F8<=0.125),E8&"B",E8))
but a problem newly create that
when I press .05 in F8 it shows E8&A but when I press .06 it shows only E8 and next when I press .07 it shows E8&B
my question is why it shows only E8 when I press .06 in F8
Thank you
It's because of the parentheses () used incorrectly.
=IF(AND(F8>=0.01, F8<=0.06),E8 & "A", IF(AND(F8>=0.07, F8<=0.125), E8 & "B", E8))
will work
Try:
=IF(AND(F8>=0.01,F8<=0.06),E8&"A",IF(AND(F8>=0.07,F8<=0.125),E8&"B",E8))
ie move the ) after "A" to the end.
Here you go. This implementation specifies something to do when values are outside of your ranges.
=IF(F8>=0.01,IF(F8>0.06,IF(F8>0.125,"Greater than 0.125",E8&"B"),E8&"A"),"less than 0.01")
You'll have to edit the formula based on what you want to happen to values between 0.06 and 0.07 (this is not specified in your question).
What does E8&"A" means? Try + instead of &...
=IF(AND(F8>=0.01,F8<=0.06),E8+"A"),IF(AND(F8>=0.07,F8<=0.125),E8+"B",E8)

Vim incorrectly removes zeros when decrementing

Vim removes zeros from in front of some digits when decrementing:
If I take a text file with the following:
a02
a03
a04
a05
a06
a07
a08
a09
a10
a11
And use ctrl+V to highlight the second and third columns, and then hit ctrl+X to decrement, I am left with:
a01
a02
a03
a04
a05
a06
a7
a8
a9
a10
I am running Vim version 7.4.1689 and I loaded it without my .vimrc via
$ vim -u NONE
This is happening because Vim will automatically recognize and convert octal values.
From the help (:h variables):
Conversion from a Number to a String is by making the ASCII representation of
the Number.
Examples:
Number 123 --> String "123"
Number 0 --> String "0"
Number -1 --> String "-1"
Conversion from a String to a Number is done by converting the first digits to
a number. Hexadecimal "0xf9", Octal "017", and Binary "0b10" numbers are
recognized. If the String doesn't start with digits, the result is zero.
Examples:
String "456" --> Number 456
String "6bar" --> Number 6
String "foo" --> Number 0
String "0xf1" --> Number 241
String "0100" --> Number 64
String "0b101" --> Number 5
String "-8" --> Number -8
String "+8" --> Number 0
Your values 02 through 07 are being recognized as valid octal values and preserved as such, decremented to octal 01 through 06.
When you reach 08 it is not a valid octal value. It is treated as the string 08, converted to decimal value 8, and decremented to 7. This happens again with 09, which ends up being 8.
The 10 and 11 values are decremented as decimal as well. Because 10 was decimal, not octal, you don't get a leading 0 in the resulting 9 value.
I'm not aware of a way to do what you want with the decrement command.
EDIT: After finding this answer, I tested this expression and it does what you are trying to do in this specific case:
:%s/\v[0-9]+/\=printf("%02d", substitute(submatch(0), '^0\+', '', 0)-1)/
I'm not sure whether this solves your general use case, because it's quite different from the original operation using a selection. But for the file you provided, it achieves the result you were after.
Dissecting this a bit to explain it:
First we start by calling the global sub command %s and passing the \v flag to turn on "very magic" mode. This may or may not change the behavior depending on your settings, but this is a public example, so it is included here to ensure that mode is active.
:%s/\v
Then, we find all the contiguous sequences of digits. This will find 02, 03, and so on from your example.
[0-9]+
Then in the replacement portion we have this command, which does the real work:
\=printf("%02d", substitute(submatch(0), '^0\+', '', 0)-1)
The substitute() function determines what the new value is. submatch(0) means to use the entire match. Using a pattern of ^0\+ and a replacement of (empty string) says to strip the leading zero from any number which has one. The 0 at the end isn't too important; it just says there are no flags to the substitute() function.
The result of the substitute command is a number. Say 02 has been stripped down to be 2. Using the - 1 at the end, we subtract 1 from that result (decrement).
Finally, this result is passed to the printf function. Using a format string %02d says to print the values as decimal, in 2-digit wide format, padding with leading zeroes.
If you want Vim to treat all numbers as decimals, you may want to add the following line to your .vimrc:
set nrformats=

Using the isnumber() function in Excel and Formatting Issue

I have a list of values, some being integers and some being non-integers. I would like to return the values that are integers. My idea:
if(ISNUMBER(C1)=TRUE,C1,0)
The data is laid out as so
88
Francesc Fabregas
m
86
Andrey Arshavin
a
86
Therefore I would only return the 88, 86, and 86. 88 is in cell C1.
Update: THE CELLS HAVE THE VALUES STORED IN THEM AS TEXT. HOW MIGHT I CHANGE ALL THE CELLS FORMATTING TO NUMBERS?
Try:
=IFERROR(--C1,"")
It will try to multiply -1*-1 to the value, if it is like a number it will return the number but if not it will error out and return "".
If you are using 2003 or later then use:
=IF(ISERR(--C1),"",--C1)
To do it in place highlight the range and you will see a little box in the upper left corner:
Hit that button and you will have a drop down list of option. Choose the "Convert to Number" option.
The NUBMERVALUE() function converts text to numbers. So the formula
=IF(ISNUMBER(NUMBERVALUE(D14)), NUMBERVALUE(D14), 0)
would turn your data to
88
0
0
86
0
0
86

Excel query regarding IF statements

I think this should be a fairly quick question, hopefully!
I would like to add a line to the following formula, after the line that says IF C6 =0,0 that if B6 is equal to W, the result should be 9.95:
=IF(C6=0,0,
MAX(SUM(IF(F6<=0,0,39),
IF(F6>30,(C6*0.08),
IF(F6>20,(C6*0.07),
IF(F6>10,(C6*0.06),
IF(F6>5,(C6*0.05),
IF(F6>2,(C6*0.04),
IF(F6>1,(C6*0.03),
IF(F6>=0.25,(C6*0.02),
IF(F6>=0,(0.03*C6*F6),0))))))))),43))
Basically, the first thing it should look for is that if C6 is 0, the result should be 0 - but if C6 > 0, then to check B6 for the letter 'W', and if that is true, then to display 9.95, otherwise to go along with the remainder of the formula.
Try this:-
=IF(C6=0,0,
IF(B6="W",9.95,
MAX(SUM(IF(F6<=0,0,39),
IF(F6>30,(C6*0.08),
IF(F6>20,(C6*0.07),
IF(F6>10,(C6*0.06),
IF(F6>5,(C6*0.05),
IF(F6>2,(C6*0.04),
IF(F6>1,(C6*0.03),
IF(F6>=0.25,(C6*0.02),
IF(F6>=0,(0.03*C6*F6),0))))))))),43)))

ASCII text to Hexadecimal in Excel

I want to this but i don't know what to do, the only functions it seems to be useful is "DEC.TO.HEX".
This is the problem, i have in one cell this text:
1234
And in the next cell i want the hexadecimal value of each character, the expected result would be:
31323334
Each character must be represented by two hexadecimal characters. I don't have an idea how to solve this in excel avoiding make a coded program.
Regards!
Edit: Hexadecimal conversion
Text value Ascii Value (Dec) Hexadecimal Value
1 49 31
2 50 32
3 51 33
4 52 34
Please try:
=DEC2HEX(CODE(MID(A1,1,1)))&DEC2HEX(CODE(MID(A1,2,1)))&DEC2HEX(CODE(MID(A1,3,1)))&DEC2HEX(CODE(MID(A1,4,1)))
In your version you might need the .s in the function (and perhaps ;s rather than ,s).
DEC2HEX may be of assistance. Use, as follows:
=DEC2HEX(A3)
First split 1234 to 1 2 3 4 by using MID(), then use Code() for each character, and then again concentate. Below is the formula, Y21 is the cell in which 1234 is written
=CONCATENATE(CODE(MID(Y21,1,1)),CODE(MID(Y21,2,1)),CODE(MID(Y21,3,1)),CODE(MID(Y21,4,1)))
1234 >> 49505152

Resources