Python not all arguments converted during string formatting - python-3.x

I have the following python code
table += '<tr><td>{0}</td><td>{1}</td></tr>'% format(str(key)),
format(str(value))
TypeError: not all arguments converted during string formatting
What could be the problem?

You are mixing three different methods of converting values to strings. To use the {..} placeholders, use the str.format() method, directly on the string literal that is the template:
table += '<tr><td>{0}</td><td>{1}</td></tr>'.format(key, value)
There's no need to use the str() or format() functions, and you don't need to use % either.
The format() function turns objects into a string using the format specification mini language, the same language that lets you format the values in a str.format() placeholder. It's the formatting part of the template language, available as a stand-alone function.
str() turns an object into a string without specific formatting. You'll always get the same result. Because str.format() already converts objects to strings (but with formatting controls) you don't need to use this. And if you need it anyway (because, say, you want to apply string formatting controls and not those of another object type), str.format() has the !s conversion option.
Applying % to a string object gives you a different string formatting operation called *printf-style formatting. This method is much less flexible than the str.format() version.
If your are using Python 3.6 or newer I recommend you use the faster f-string formatting string literals:
table += f'<tr><td>{key}</td><td>{value}</td></tr>'

You have to call the .format method on the string. Just like this:
table += '<tr><td>{key}</td><td>{value}</td></tr>'.format(key=str(key), value=str(value))

Related

PySpark Can't substring into variable

I'm trying to get the value from a column to feed it later as a parameter. I need to substring it to get the correct values as the date format is DDMMYYYY.
But when I try applying the substring into the resulting variable, a Column object type is generated.. any suggestions?
You can't call Spark functions on Python strings. You need to use Python string methods, e.g.
print(dataCollect[:3])
which should give '301'.

Convert negative currency values stored as text

I regularly copy and paste values into an Excel spreadsheet and use the VALUE() function to convert them from text to numbers. However the following value is not getting converted and results in a #VALUE error:
−£13.24
I could do some complex string manipulation to remove the currency symbol, but just wondered if there was a simpler solution.
Any suggestions appreciated.
EDIT: I have just realised that it is not the currency symbol that is causing the problem, but the minus sign. I am copying the data from a website, and I guess it is using a different character encoding. Are there functions in Excel for handling character encoding?
I would use SUBSTITUTE to get rid of the currency character, e.g.
= SUBSTITUTE(D43,"£","")
And you could wrap the whole thing around a VALUE function, e.g.
= VALUE(SUBSTITUTE(D43,"£",""))
If you have issues with "long dash" vs. "short dash", (− vs. -), you can do this:
= VALUE(SUBSTITUTE(SUBSTITUTE(D43,"£",""),"−","-"))
Short dashes are required for Excel to recognize the string as a number.
=IF(LEFT(D43,2)="−£",-VALUE(MID(D43,3,LEN(D43)-2)),VALUE(D43))
This seems to do the job, but would be interested if there is a better/simpler solution.

Compare a numeric value inside a string in Freemarker

I have a use case wherein I get an amount in the form of a string and I need to compare it to an integer value. The string has the following conditions:
1. Its first three letters denote the currency
2. The string can consist of commas
For e.g. EUR 540,000 denotes 540000 Euros.
One way I can think of doing this is to:
1 Take a substring from the 4rth character using ?substring
2 Eliminate the commas using ?replace
3 Convert to a integer using ?number
4 and then compare
Is there a cleaner way to achieve this?
You can encapsulate that into a #function or a TemplateMethodModelEx. But really, the data-model (template context) you are using is rather strange. Especially if the template is supposed to do math, you should get the numbers (like BigDecimal-s or Integer-s), not those strings. So I would try to clean up the data-model before passing it to the template. That's the clean solution.

converting strings to formula objects in Julia

I have a dataframe in Julia with less than 10 column names. I want to generate a list of all possible formulas that could be fed into a linear model (eg, [Y~X1+X2+X3, Y~X1+X2, ....]). I can accomplish this easily with combinations() and string versions of the column names. However, when I try to convert the strings into Formula objects, it breaks down. Looking at DataFrames.jl documentation, it seems like one can only construct Formulas from "expressions" and I can indeed make a list of individual column names as expressions. Is there any way I can somehow join together a bunch of different expressions using the "+" operator programmatically such that the resulting composite expression can then be passed into RHS of the Formula constructor? My impulse is to search for some function that will convert an arbitrary string into the equivalent expression, but not sure if that is correct.
The function parse takes a string, parses it, and returns an expression. I see nothing wrong with using it for what you're talking about.
Here is some actual working code, because I have been struggling with getting a similar problem to work. Please note this is Julia version 1.3.1 so parse is now Meta.parse and instead of combinations I used IterTools.subsets.
using RDatasets, DataFrames, IterTools, GLM
airquality = rename(dataset("datasets", "airquality"), "Solar.R" => "Solar_R")
predictors = setdiff(names(airquality), [:Temp])
for combination in subsets(predictors)
formula = FormulaTerm(Term(:Temp), Tuple(Term.(combination)))
if length(combination) > 0
#show lm(formula, airquality)
end
end

Is there a character in Excel to mask a STRING using TEXT function?

Is there a character to mask STRING values within the Excel TEXT function?
Attempting to use a mask of "0000-000000-00000-0000" seems to convert a string to a number. I simply want to add hyphens in between a specific number of characters.
I have also tried "####-######-#####-####" and "####-######-#####-####" but to no avail.
Background:
In a previous question, it was determined that a particular custom number mask could not be applied to a string because of the 15 significant digit limitation in Excel.
The goal was to convert a TEXT value of 5145350002005000080 to 5145-350002-00500-0080 using the following formula:
=text(A1,"0000-000000-00000-0000")
The output produced was:
5145-350002-00500-0000
You will need to use Excel string functions.
This works, though it is not the usual way of getting the job done:
=REPLACE(REPLACE(REPLACE(A1,16,0,"-"),11,0,"-"),5,0,"-")
The more typical method:
=LEFT(A1,4)&"-"&MID(A1,5,6)&"-"&MID(A1,10,5)&"-"&RIGHT(A1,4)
Unfortunately it's impossible to apply markup to any string value using TEXT - as per TEXT function description, it may be done only for numbers:
The TEXT function converts a numeric value to text and lets you
specify the display formatting by using special format strings.
Syntax
TEXT(value, format_text)
The TEXT function syntax has the following arguments:
value Required. A numeric value, a formula that evaluates to a numeric value, or a reference to a cell containing a numeric value.
So it looks like the only way for you to achieve what you want - is to apply recommended string conversions.
Select the cells->Press Ctrl+1->from Number Tab of Format Cells Dialog select "custom" and paste in Type edit box Below.
"Boxes";"Boxes";"Boxes";"Boxes"
Source: Here

Resources