在Python 3中,可以格式化一个字符串,如:
"{0}, {1}, {2}".format(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
但是如何格式化字节?
b"{0}, {1}, {2}".format(1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
加油AttributeError: 'bytes' object has no attribute 'format'.
如果没有format字节方法,如何格式化或"重写"字节?
String.Format不起作用TypeScript.
错误:
Run Code Online (Sandbox Code Playgroud)The property 'format' does not exist on value of type '{ prototype: String; fromCharCode(...codes: number[]): string; (value?: any): string; new(value?: any): String; }'.
attributes["Title"] = String.format(
Settings.labelKeyValuePhraseCollection["[WAIT DAYS]"],
originalAttributes.Days
);
Run Code Online (Sandbox Code Playgroud) 如果我有一个像这样的字符串:
"{0} {1} {1}" % ("foo", "bar")
Run Code Online (Sandbox Code Playgroud)
而且我要:
"foo bar bar"
Run Code Online (Sandbox Code Playgroud)
替换令牌必须是什么?(我知道上面的例子不正确;我只是想表达我的目标.)
我正在使用C#创建一个轻量级编辑器,并希望知道将字符串转换为格式良好的XML字符串的最佳方法.我希望C#库中有一个公共方法,比如"public bool FormatAsXml(string text,out string formattedXmlText)",但它可能不那么容易,是吗?
非常具体地,"SomeMethod"方法必须是什么才能产生下面的输出?
string unformattedXml;
string formattedXml;
unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>"
formattedXml = SomeMethod(unformattedXml);
Console.WriteLine(formattedXml);
Run Code Online (Sandbox Code Playgroud)
输出:
<?xml version="1.0"?>
<book id="123">
<author>Lewis, C.S.</author>
<title>The Four Loves</title>
</book>
Run Code Online (Sandbox Code Playgroud) 我想知道是否存在在string.Format中格式化NULL值的语法,例如Excel使用的语法
例如,使用Excel我可以指定格式值{0:#,000.00;-#,000.00,NULL},这意味着如果为正数则将数值显示为数字格式,如果为负数则显示在括号中的数字格式,如果值为null则显示为NULL
string.Format("${0:#,000.00;(#,000.00);NULL}", someNumericValue);
Run Code Online (Sandbox Code Playgroud)
编辑
我正在寻找所有数据类型的格式NULL/ Nothing值,而不仅仅是数字类型.
我的例子实际上是不正确的,因为我错误地认为Excel使用第3个参数,如果值为NULL,但它实际上在值为0时使用.我将它留在那里因为它是我能想到的最接近我的东西希望能做到.
我希望避免使用null合并操作符,因为我正在编写日志记录,并且数据通常不是字符串
写一些类似的东西要容易得多
Log(string.Format("Value1 changes from {0:NULL} to {1:NULL}",
new object[] { oldObject.SomeValue, newObject.SomeValue }));
Run Code Online (Sandbox Code Playgroud)
而不是写
var old = (oldObject.SomeValue == null ? "null" : oldObject.SomeValue.ToString());
var new = (newObject.SomeValue == null ? "null" : newObject.SomeValue.ToString());
Log(string.Format("Value1 changes from {0} to {1}",
new object[] { old, new }));
Run Code Online (Sandbox Code Playgroud) 格式化要打印的数字时,正在使用点后面的冒号格式化12位数字.为什么会这样?这是AIX系统上的Python 2.7.
$ uname -a ; /opt/bin/python2.7
AIX myserver 1 6 00F6A5CC4C00
Python 2.7.12 (default, Sep 29 2016, 12:02:17) [C] on aix5
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0:.10f}'.format(123456789012)
'123456789011.:000000000'
>>> from decimal import Decimal
>>> u=123456789012
>>> print "%.10f" % Decimal(u)
123456789011.:000000000
Run Code Online (Sandbox Code Playgroud)
更多的信息:
它不是每12位数字:
>>> for x in range(123456789010,123456789020):
... print '{0:.10f}'.format(x)
...
12345678900:.0000000000
123456789010.:000000000
123456789011.:000000000
123456789013.0000000000
123456789013.:000000000
123456789015.0000000000
123456789016.0000000000
123456789017.0000000000
123456789017.:000000000
123456789019.0000000000
Run Code Online (Sandbox Code Playgroud)
任何其他长度数字都不会发生这种情况.另外,我尝试了bash和perl的printf,而且这两种情况都没有发生.
这里发生了什么?
根据要求,这是一个截屏视频.
更多要求的信息:
>>> import locale
>>> locale.getdefaultlocale()
('en_US', …Run Code Online (Sandbox Code Playgroud) 我有一个向数字添加逗号的功能:
function commafy( num ) {
num.toString().replace( /\B(?=(?:\d{3})+)$/g, "," );
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,它不太喜欢小数.鉴于以下用法示例,扩展我的功能的最佳方法是什么?
commafy( "123" ) // "123"
commafy( "1234" ) // "1234"
// Don't add commas until 5 integer digits
commafy( "12345" ) // "12,345"
commafy( "1234567" ) // "1,234,567"
commafy( "12345.2" ) // "12,345.2"
commafy( "12345.6789" ) // "12,345.6789"
// Again, nothing until 5
commafy( ".123456" ) // ".123 456"
// Group with spaces (no leading digit)
commafy( "12345.6789012345678" ) // "12,345.678 901 234 567 8"
Run Code Online (Sandbox Code Playgroud)
大概最简单的方法是首先分割小数点(如果有的话).哪里最好去哪里?
如何在XX XX XX XX没有循环的情况下将int(4字节)转换为十六进制(" ")?
例如:
i=13 hex="00 00 00 0D"
Run Code Online (Sandbox Code Playgroud)
i.ToString("X")返回"D",但我需要一个4字节的十六进制值.
我似乎找不到一种方法来格式化Double将精度限制在小数点后的几个位置,当在标签中显示时.也许我错过了一些重要的东西,但我在官方文档中找不到任何东西.
在此先感谢您的帮助!
python ×4
c# ×3
javascript ×2
string ×2
aix ×1
formatting ×1
hex ×1
python-3.x ×1
swift ×1
typescript ×1
xml ×1