Formatting "key: value" strings with middle spaces - python-3.x

I'm showing key-value pair in a QListWidget. Since key names have different lengths, numbers start in different positions:
TestParameter1: 1.2345
Param2: 6.7890
If it wasn't for the key name, I know that I can use format to introduce spaces with a syntax like '{:7.4f}'.format(value).
Is there any easy way (I mean, not switching to a table or creating my own implementation os a QListView) of achieving something like...?:
TestParameter1: 1.2345
Param2: 6.7890

I suggest you take a further look at string formatting here.
The first thing that comes to mind is to either use the \t (tab) to align as necessary; or to use something on the lines of the below as stated in the python docs.
>>> for align, text in zip('<^>', ['left', 'center', 'right']):
... '{0:{fill}{align}16}'.format(text, fill=align, align=align)
...
'left<<<<<<<<<<<<'
'^^^^^center^^^^^'
'>>>>>>>>>>>right'
>>>

Related

how do I get rid of leading/trailing spaces in SAS search terms?

I have had to look up hundreds (if not thousands) of free-text answers on google, making notes in Excel along the way and inserting SAS-code around the answers as a last step.
The output looks like this:
This output contains an unnecessary number of blank spaces, which seems to confuse SAS's search to the point where the observations can't be properly located.
It works if I manually erase superflous spaces, but that will probably take hours. Is there an automated fix for this, either in SAS or in excel?
I tried using the STRIP-function, to no avail:
else if R_res_ort_txt=strip(" arild ") and R_kom_lan=strip(" skåne ") then R_kommun=strip(" Höganäs " );
If you want to generate a string like:
if R_res_ort_txt="arild" and R_kom_lan="skåne" then R_kommun="Höganäs";
from three variables, let's call them A B C, then just use code like:
string=catx(' ','if R_res_ort_txt=',quote(trim(A))
,'and R_kom_lan=',quote(trim(B))
,'then R_kommun=',quote(trim(C)),';') ;
Or if you are just writing that string to a file just use this PUT statement syntax.
put 'if R_res_ort_txt=' A :$quote. 'and R_kom_lan=' B :$quote.
'then R_kommun=' C :$quote. ';' ;
A saner solution would be to continue using the free-text answers as data and perform your matching criteria for transformations with a left join.
proc import out=answers datafile='my-free-text-answers.xlsx';
data have;
attrib R_res_ort_txt R_kom_lan length=$100;
input R_res_ort_txt ...;
datalines4;
... whatever all those transforms will be performed on...
;;;;
proc sql;
create table want as
select
have.* ,
answers.R_kommun_answer as R_kommun
from
have
left join
answers
on
have.R_res_ort_txt = answers.res_ort_answer
& have.R_kom_lan = abswers.kom_lan_answer
;
I solved this by adding quotes in excel using the flash fill function:
https://www.youtube.com/watch?v=nE65QeDoepc

Using REGEX to grab the information after the match

I ran a PDF through a series of processes to extra the text from it. I was successful in that regard. However, now I want to extract specific text from documents.
The document is set up as a multi lined string (I believe. when I paste it into Word the paragraph character is at the end of each line):
Send Unit: COMPLETE
NOA Selection: 20-0429.07
#for some reason, in this editor, despite the next line having > infront of it, the following line (Pni/Trk) keeps wrapping up to the line above. This doesn't exist in the actual doc.
Pni/Trk: 3 Panel / 3 Track
Panel Stack: STD
Width: 142.0000
The information is want to extract are the numbers following "NOA Selection:".
I know I can do a regex something to the effect of:
pattern = re.compile(r'NOA\sSelection:\s\d*-\d*\.\d*)
but I only want the numbers after the NOA selection, especially because NOA Selection will always be the same but the format of the numbers/letters/./-/etc. can vary pretty wildly. This looked promising but it is in Java and I haven't had much luck recreating it in Python.
I think I need to use (?<=...), but haven't been able to implement it.
Also, several of the examples show the string stored in the python file as a variable, but I'm trying to access it from a .txt file, so I might be going wrong there. This is what I have so far.
with open('export1.txt', 'r') as d:    
contents = d.read()    
p = re.compile('(?<=NOA)')
s = re.search(p, contents)
print(s.group())
Thank you for any help you can provide.
With your shown samples, you could try following too. For sample 20-0429.07 I have kept .07 part optional in regex in case you have values 20-0429 only it should work for those also.
import re
val = """Send Unit: COMPLETE
NOA Selection: 20-0429.07"""
matches = re.findall(r'NOA\s+Selection:\s+(\d+-\d+(?:\.\d+)?)', val)
print(matches)
['20-0429.07']
Explanation: Adding detailed explanation(only for explanation purposes).
NOA\s+Selection:\s+ ##matching NOA spaces(1 or more occurrences) Selection: spaces(1 or more occurrences)
(\d+-\d+(?:\.\d+)?) ##Creating capturing group matching(1 or more occurrences) digits-digits(1 or more occurrences)
##and in a non-capturing group matching dot followed by digits keeping it optional.
Keeping it simple, you could use re.findall here:
inp = """Send Unit: COMPLETE
NOA Selection: 20-0429.07"""
matches = re.findall(r'\bNOA Selection: (\S+)', inp)
print(matches) # ['20-0429.07']

Add hyphen in between letters and hyphen

I have a list of sample names:
TW1
UD1
SS1
S17
SS23
UD12
I wish to add a hyphen in between the letters and the numbers as such:
TW-1
UD-1
SS-1
S-17
SS-23
UD-12
UD786
I tried this:
=MID(A1,1,COUNT(1*MID(A1,{1,2,3,4,5,6,7,8,9,0},1)))&"-"&SUBSTITUTE(A1,MID(A1,1,COUNT(1*MID(A1,{1,2,3,4,5,6,7,8,9,0},1))),"")
The results were not consistent. It gives the following results:
T-W1
U-D1
S-1
S1-7
SS-23
UD-12
How may I achieve the desired output?
=LEFT(A1,2-ISNUMBER(--MID(A1,2,1)))&"-"&RIGHT(A1,LEN(A1)-(2-ISNUMBER(--MID(A1,2,1))))
Is another option. Trick in this is to convert your number as a string to a number before testing if its a number. Instead of -- you could have done 1* or 0+.
This solution only works as your sample data was 1 or 2 characters before the first digit.
Slightly shorter than #Forward Ed's A:
=REPLACE(A1,2+(CODE(MID(A1,2,1))>64),,"-")

Groovy currency formatting

I'm writing some rows to a text file using groovy (grails 1.3.7) and I want to format the currency like this example output:
$100,000,000.00
$9,123,123.25
$10.20
$1,907.23
So basically right-justified, or left padded, with the dollar sign in front of the number so they all line up like the above. The first number is the longest we would expect to see. Right now I have an amount variable that is simply defined with a def and not string or number or anything specific like that but I can obviously change that if need be. Thanks!
You probably want to use NumberFormat.getCurrencyInstance(). This will return a NumberFormat object that uses the standard currency representation for your default Locale (or optionally, the one you pass in).
To right justify, you can use String.padLeft().
Example:
def formatter = java.text.NumberFormat.currencyInstance
def values = [0, 100000000, 9123123.25, 10.20, 1907.23]
def formatted = values.collect { formatter.format(it) }
def maxLen = formatted*.length().max()
println formatted.collect { it.padLeft(maxLen) }.join("\n")
//output
$0.00
$100,000,000.00
$9,123,123.25
$10.20
$1,907.23
In grails soemthing like this will format it nicely with comma separators.
<g:formatNumber number="${150000}" type="currency" currencyCode="USD"/>
For right aligning I would use style:
<td style='text-align:right;...'>

Oracle/SQL - Removing undefined chars from string

I currently have an assignemnt where i have to handle data from a lot of countries. My customer have given me a list of acceptable characters, lets call it:
'aber =*'
All other characters should just be changed to '_'.
I know the conversion for my country's specific chars (æøå), easily done with something like
select replace ('Ål', 'Å', 'AA') from dual;
But how would i go about removing all unwanted "noise" without splitting it up in char-by-char comparison?
For example "bear*2 = fear" should become "bear*_ = _ear" as 2 and f are not in the accepted list.
Oracle 10g and up. As one of the approaches, you can use regular expression function regexp_replace():
select regexp_replace('bear*2 = fear', '[^aber =*]', '_') as res
from dual
res
------------------------------
bear*_ = _ear
Find out more about regexp_replace() function.

Resources