我正在尝试维护/更新/重写/修复一些看起来有点像这样的Python:
variable = """My name is %s and it has been %s since I was born.
My parents decided to call me %s because they thought %s was a nice name.
%s is the same as %s.""" % (name, name, name, name, name, name)
Run Code Online (Sandbox Code Playgroud)
整个脚本都有一些看起来像这样的片段,我想知道是否有一种更简单(更Pythonic?)的方式来编写这段代码.我发现其中一个实例替换了相同的变量大约30次,而且感觉很难看.
围绕(在我看来)丑陋的唯一方法将它分成许多小点?
variable = """My name is %s and it has been %s since I was born.""" % (name, name)
variable += """My parents decided to call me %s because they thought %s was a nice …Run Code Online (Sandbox Code Playgroud) 是否有一种pythonic方法来执行该str.strip()方法所做的事情,除了所有事件,而不仅仅是字符串开头和结尾处的事件?
例:
>> '::2012-05-14 18:10:20.856000::'.strip(' -.:')
>> '2012-05-14 18:10:20.856000'
Run Code Online (Sandbox Code Playgroud)
我想要
>> '::2012-05-14 18:10:20.856000::'.crazy_function(' -.:')
>> '20120514181020856000'
Run Code Online (Sandbox Code Playgroud)
Python是否为我提供了内置的crazy_function???
我可以轻松地以编程方式执行此操作,但我想知道是否有内置功能.找不到一个.谢谢您的帮助.
班级:
class Book(object):
def __init__(self, title, author):
self.title = title
self.author = author
def get_entry(self):
return "{0} by {1} on {}".format(self.title, self.author, self.press)
Run Code Online (Sandbox Code Playgroud)
从中创建我的书的实例:
In [72]: mybook = Book('HTML','Lee')
In [75]: mybook.title
Out[75]: 'HTML'
In [76]: mybook.author
Out[76]: 'Lee'
Run Code Online (Sandbox Code Playgroud)
请注意,我没有初始化属性'self.press',而是在get_entry方法中使用它.继续输入数据.
mybook.press = 'Murach'
mybook.price = 'download'
Run Code Online (Sandbox Code Playgroud)
直到现在,我可以指定所有输入的数据 vars
In [77]: vars(mybook)
Out[77]: {'author': 'Lee', 'title': 'HTML',...}
Run Code Online (Sandbox Code Playgroud)
我在控制台中输入了很多关于mybook的数据.当尝试调用get_entry方法时,错误报告.
mybook.get_entry()
ValueError: cannot switch from manual field specification to automatic field numbering.
Run Code Online (Sandbox Code Playgroud)
所有这一切都在控制台上以交互模式进行.我珍惜数据输入,进一步mybook在文件中挑选对象.但是,它有缺陷.如何在交互模式下拯救它.或者我必须重新开始.
我听说使用StringBuilder比使用字符串连接更快,但我厌倦了一直在与StringBuilder对象进行摔跤.我最近接触过SLF4J日志库,与String.format相比,我喜欢格式化的"正确做事".那里有一个图书馆可以让我写下这样的东西:
int myInteger = 42;
MyObject myObject = new MyObject(); // Overrides toString()
String result = CoolFormatingLibrary.format("Simple way to format {} and {}",
myInteger, myObject);
Run Code Online (Sandbox Code Playgroud)
另外,是否有任何原因(包括性能但不包括对日期和重要数字格式的细粒度控制)为什么我可能想要在这样的库中使用String.format(如果它存在的话)?
如何在数据不存在时隐藏字符串格式.请考虑此示例
<TextBlock Text="{Binding Amount, StringFormat=Total: {0:C}}" />
Run Code Online (Sandbox Code Playgroud)
在这种情况下,如果Amount为null,那么它将只显示Total:.如果Amount为null或为空,如何隐藏它
有没有什么办法让printf一个size_t没有任何第一投射,或产生编译器警告?(我总是编译-Wall.)
语言是Java.%1$#...... 的意思是什么
static String padright (String str, int num) {
return String.format("%1$#" + num + "str", str);
}
Run Code Online (Sandbox Code Playgroud)
在Java API中,String.format()以这种方式使用:
public static String format(String format, Object... args)
Run Code Online (Sandbox Code Playgroud)
所以我认为%1$#是格式说明符.
%[flags][width][.precision][argsize]typechar 是模板.
是对的吗?
是否有一个简单的字符串格式,它将采用小数表示小时和小时的分数,并将其显示为小时和分钟?
例如:5.5格式化显示5小时30分钟.
我很乐意自己编写代码,但是如果可用的话,我更愿意使用现有的功能
public class Divers {
public static void main(String args[]){
String format = "|%1$-10s|%2$-10s|%3$-20s|\n";
System.out.format(format, "FirstName", "Init.", "LastName");
System.out.format(format, "Real", "", "Gagnon");
System.out.format(format, "John", "D", "Doe");
String ex[] = { "John", "F.", "Kennedy" };
System.out.format(String.format(format, (Object[])ex));
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
|FirstName |Init. |LastName |
|Real | |Gagnon |
|John |D |Doe |
|John |F. |Kennedy |
Run Code Online (Sandbox Code Playgroud)
我希望输出居中.如果我不使用' - '标志,则输出将与右侧对齐.
我没有在API中找到中心文本的标志.
该文章有关于格式的一些信息,但没有对中心自圆其说.
在IRC的谈话中,有人指出以下内容:
decimal.Parse("1.0000").ToString() // 1.0000
decimal.Parse("1.00").ToString() // 1.00
Run Code Online (Sandbox Code Playgroud)
该decimal类型如何/为什么保留这样的精度(或者更确切地说,重要数字)?我的印象是这两个值是相等的,不是很明显.
这也提出了进一步的问题: