好吧,我正在从我填充DataGridView的列表中获取数据,并将其导出到文本文件.我已经完成了将其导出为CSV的功能,并且也想做纯文本版本.
因为标题和其他元素的长度是可变的,当文件被保存然后在记事本中打开时,它看起来像一团糟,因为没有任何排列.
我想让输出看起来像这样:
Sample Title One Element One Whatever Else
Sample Title 2 Element 2 Whatever Else
S. T. 3 E3 Whatever Else
Run Code Online (Sandbox Code Playgroud)
我想我可以循环遍历每个元素以获得最长的元素的长度,这样我就可以计算要添加到每个剩余元素的空格数.
我的主要问题是:是否有一种优雅的方法可以将可变数量的字符添加到字符串中?有这样的东西会很高兴:myString.insert(index, charToInsert, howManyToInsert);
当然,我显然可以通过循环编写一个函数来执行此操作,但我想看看是否有更好的方法.
提前致谢!
-Sootah
我正在尝试使用WPF中的数据绑定在TextBox中显示格式化的十进制数.
目标1:在代码中设置小数属性时,在TextBox中显示2个小数位.
目标2:当用户与TextBox交互(输入)时,不要惹恼他/她.
目标3:绑定必须更新PropertyChanged上的源.
尝试1:没有格式化.
在这里,我们几乎从头开始.
<TextBox Text="{Binding Path=SomeDecimal, UpdateSourceTrigger=PropertyChanged}" />
Run Code Online (Sandbox Code Playgroud)
违反目标1. SomeDecimal = 4.5将在TextBox中显示"4.50000".
尝试2:在绑定中使用StringFormat.
<TextBox Text="{Binding Path=SomeDecimal, UpdateSourceTrigger=PropertyChanged, StringFormat=F2}" />
Run Code Online (Sandbox Code Playgroud)
违反目标2.说SomeDecimal是2.5,TextBox显示"2.50".如果我们选择all并输入"13.5",我们在TextBox中以"13.5.00"结束,因为格式化程序"有用"地插入小数和零.
尝试3:使用Extended WPF Toolkit的MaskedTextBox.
http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox
假设我正在正确阅读文档,掩码## 0.00表示"两个可选数字,后跟一个必需的数字,一个小数点,还有两个必需的数字.这迫使我说"可以进入的最大可能数字这个TextBox是999.99",但让我说我很好.
<xctk:MaskedTextBox Value="{Binding Path=SomeDecimal, UpdateSourceTrigger=PropertyChanged}" Mask="##0.00" />
Run Code Online (Sandbox Code Playgroud)
违反目标2. TextBox以___.__选择它并输入5.75产量开始575.__.获得5.75的唯一方法是选择TextBox并输入<space><space>5.75.
尝试4:使用Extended WPF Toolkit的DecimalUpDown微调器.
http://wpftoolkit.codeplex.com/wikipage?title=DecimalUpDown
<xctk:DecimalUpDown Value="{Binding Path=SomeDecimal, UpdateSourceTrigger=PropertyChanged}" FormatString="F2" />
Run Code Online (Sandbox Code Playgroud)
违反目标3.DecimalUpDown愉快地忽略UpdateSourceTrigger = PropertyChanged.Extended WPF Toolkit Codeplex页面上的协调员之一建议在http://wpftoolkit.codeplex.com/discussions/352551/上修改ControlTemplate .这符合目标3,但违反了目标2,表现出与尝试2中相同的行为.
尝试5:使用样式数据触发器,仅在用户未编辑时才使用格式.
使用TextBox上的StringFormat绑定到double
即使这个目标满足所有三个目标,我也不想使用它.(A)文本框每行变为12行而不是1行,我的应用程序包含许多很多文本框.(B)我的所有文本框都有一个Style属性,该属性指向设置边距,高度和其他东西的全局StaticResource.(C)您可能已经注意到以下代码将绑定路径设置为两次,这违反了DRY主体.
<TextBox>
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Setter Property="Text" Value="{Binding Path=SomeDecimal, StringFormat=F2}" />
<Style.Triggers> …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Python 3.6.通过新代码,我偶然发现了这个新语法:
f"My formatting string!"
Run Code Online (Sandbox Code Playgroud)
看来我们可以这样做:
>>> name = "George"
>>> print(f"My cool string is called {name}.")
My cool string is called George.
Run Code Online (Sandbox Code Playgroud)
任何人都可以对这个内部运作有所了解吗?特别是f前缀字符串可以采用的变量范围是多少?
我正在使用当前方法显示货币
String.Format("{0:C}", item.DonationAmount)
Run Code Online (Sandbox Code Playgroud)
哪个输出像10.00美元
我们将处理大笔金额,而不是美分.我们希望货币显示为10美元,不带小数或零.我怎样才能做到这一点?删除货币格式使数字显示为10.0000 :(提前感谢.
如何将小数值格式化为逗号/点后面的单个数字的字符串,以及值小于100的前导空格?
例如,十进制值12.3456应" 12.3"与单个前导空格一样输出.10.011会的" 10.0".123.123是"123.1"
我正在寻找一种解决方案,它适用于标准/自定义字符串格式,即
decimal value = 12.345456;
Console.Write("{0:magic}", value); // 'magic' would be a fancy pattern.
Run Code Online (Sandbox Code Playgroud) 我将获得一个自定义DateTime格式,包括AM/PM指示符,但我希望"AM"或"PM"为小写而不使其余的字符小写.
这可能使用单一格式而不使用正则表达式吗?
这就是我现在所拥有的:
item.PostedOn.ToString("dddd, MMMM d, yyyy a\\t h:mmtt")
Run Code Online (Sandbox Code Playgroud)
现在输出的一个例子是2009年1月31日星期六下午1:34
比方说我有:
action = '{bond}, {james} {bond}'.format(bond='bond', james='james')
Run Code Online (Sandbox Code Playgroud)
这个输出:
'bond, james bond'
Run Code Online (Sandbox Code Playgroud)
接下来我们有:
action = '{bond}, {james} {bond}'.format(bond='bond')
Run Code Online (Sandbox Code Playgroud)
这将输出:
KeyError: 'james'
Run Code Online (Sandbox Code Playgroud)
是否有一些解决方法可以防止此错误发生,例如:
如何str.format在Python中截断字符串?它甚至可能吗?
格式规范迷你语言中width提到了一个参数:
format_spec ::= [[fill]align][sign][#][0][width][,][.precision][type]
...
width ::= integer
...
Run Code Online (Sandbox Code Playgroud)
但是明确指定它只适用于填充,而不是截断:
>>> '{:5}'.format('aaa')
'aaa '
>>> '{:5}'.format('aaabbbccc')
'aaabbbccc'
Run Code Online (Sandbox Code Playgroud)
所以它实际上是宽度的最小宽度.
我知道我可以对字符串进行切片,但是我在这里处理的数据是完全动态的,包括格式字符串和进入的args.我不能只是去明确切片.
关于使用以下模式是否有任何缺点,警告或不良做法警告?
def buildString(user, name = 'john', age=22):
userId = user.getUserId()
return "Name: {name}, age: {age}, userid:{userId}".format(**locals())
Run Code Online (Sandbox Code Playgroud)
我有一个非常重复的字符串生成代码来编写并且很想使用它,但是关于使用的东西locals()让我感到不舒服.这有意外行为的危险吗?
编辑:上下文
我发现自己经常写下这样的东西:
"{name} {age} {userId} {etc}...".format(name=name, age=age, userId=userId, etc=etc)
Run Code Online (Sandbox Code Playgroud) 首先,我尝试过这篇文章(其中包括):Python中的货币格式.它对我的变量没有影响.我最好的猜测是因为我使用的是Python 3,而且是Python 2的代码.(除非我忽略了一些东西,因为我是Python的新手).
我想将一个浮点数(如1234.5)转换为String,例如"$ 1,234.50".我该怎么做呢?
为了以防万一,这里是我编译的代码,但不影响我的变量:
money = float(1234.5)
locale.setlocale(locale.LC_ALL, '')
locale.currency(money, grouping=True)
Run Code Online (Sandbox Code Playgroud)
也不成功:
money = float(1234.5)
print(money) #output is 1234.5
'${:,.2f}'.format(money)
print(money) #output is 1234.5
Run Code Online (Sandbox Code Playgroud) python ×5
.net ×3
string ×3
c# ×2
currency ×1
datetime ×1
decimal ×1
defaultdict ×1
f-string ×1
locals ×1
missing-data ×1
python-3.6 ×1
python-3.x ×1
scope ×1
textbox ×1
vb.net ×1
wpf ×1