我查看了其他一些帖子,但似乎没有任何帮助.所以我想要的是一个代码,用一个美元金额读出当前的余额,前面有一个短语.而不是打印美元符号其打印{0:C}.我使用{0:C}不正确吗?
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
double TotalAmount;
TotalAmount = 300.7 + 75.60;
string YourBalance = "Your account currently contains this much money: {0:C} " + TotalAmount;
Console.WriteLine(YourBalance);
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Python 3,我希望看到原始/真实表示与使用单引号和双引号的转义序列的字符串表示之间的区别,因此我创建了以下脚本:
raw = "%r" % "\'\""
str = "%s" % "\'\""
print(raw)
print(str)
Run Code Online (Sandbox Code Playgroud)
print(str)返回(按预期):
'"
Run Code Online (Sandbox Code Playgroud)
现在我希望print(raw)返回:
'\'\"'
Run Code Online (Sandbox Code Playgroud)
然而它返回:
'\'"'
Run Code Online (Sandbox Code Playgroud)
为什么print(raw)语句中只有一个反斜杠,不应该有两个,因为这会反映我已经解析为格式化字符串的值?对不起这个愚蠢的问题我很抱歉
我有一个javascript字符串,我想将它们转换成这样:
08:00:00 -> [8,0]
07:30:00 -> [7,30]
14:00:00 -> [14,0]
16:25:00 -> [16,25]
Run Code Online (Sandbox Code Playgroud)
我不知道该怎么做.在javascript中执行此操作的最快,最有效的方法是什么?
javascript arrays performance string-formatting string-parsing
我有一个类有几个类似的方法,每个方法都有相似的长文档字符串,但在几个短语/单词方面有所不同.我想构建一个docstring模板,然后将字符串格式应用于它.下面是一个笨拙的实现,其中__doc__s是在类方法之后定义的.
capture_doc = """
%(direc)s normal.
a %(sym)s b."""
class Cls():
def a(self):
pass
def b(self):
pass
a.__doc__ = capture_doc % {'direc' : 'below', 'sym' : '<'}
b.__doc__ = capture_doc % {'direc' : 'above', 'sym' : '>'}
c = Cls()
print(c.a.__doc__)
below normal.
a < b.
Run Code Online (Sandbox Code Playgroud)
问题:是否有Python文档或PEP规定的方法来执行此操作?我想保持基本的东西,我见过使用@Appender 装饰师,但认为这对我的需求有点花哨.
我试图创建一个JSON对象并将其附加到列表,但没有成功。我收到此错误消息:
Traceback (most recent call last):
File "/projects/circos/test.py", line 32, in <module>
read_relationship('data/chr03_small_n10.blast')
File "/projects/circos/test.py", line 20, in read_relationship
tmp = ("[source: {id: '{}',start: {},end: {}},target: {id: '{}',start: {}, end: {}}],").format(parts[0],parts[2],parts[3],parts[1],parts[4],parts[5])
KeyError: 'id'
Run Code Online (Sandbox Code Playgroud)
用下面的代码
def read_relationship(filename):
data = []
with open(filename) as f:
f.next()
for line in f:
try:
parts = line.rstrip().split('\t')
query_name = parts[0]
subject_name = parts[1]
query_start = parts[2]
query_end = parts[3]
subject_start = parts[4]
subject_end = parts[5]
# I need: [source: {id: 'Locus_1', start: 1, end: 1054}, …Run Code Online (Sandbox Code Playgroud) 我试图将int格式化为格式化字符串,如23 - >"0023",100 - > 0100等等.我已经完成了下面的功能,但它吃了10的倍数的每个数字的最后一个数字,比如900变成090而不是0900.请帮我修复那个bug,谢谢.
func convert(_ score: Int) -> String {
return String(Float(score) / 1000.0).components(separatedBy: ".").joined()
}
Run Code Online (Sandbox Code Playgroud) 假设我有以下功能:
void fprint(float f, int ds) {
printf("%.%df\n", ds, f);
}
Run Code Online (Sandbox Code Playgroud)
我希望调用者指定要打印的浮点数,以及小数点后的位数.(由说明者.%d指示)
但是,在编译时我得到2个警告:
./sprint.h: In function ‘fprint’:
./sprint.h:19:14: warning: conversion lacks type at end of format [-Wformat=]
printf("%.%df\n", ds, f);
^
Run Code Online (Sandbox Code Playgroud)
和
./sprint.h:19:12: warning: too many arguments for format [-Wformat-extra-args]
printf("%.%df\n", ds, f);
^~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
在调用它时:fprint(3.1422223f, 3);它产生输出:%df.我也尝试在函数声明中交换参数的顺序,但它会产生相同的警告.
我的问题是:如何将格式说明符(例如%d在本例中)注入现有格式说明符?
使用Python执行一系列SQL查询,并希望将我的查询的每次迭代的输出导出为自己的csv文件.
例如:
clients = ['Ellen','Jose','Tina']
for client in clients:
print(client)
with open('/sales.csv', 'wt') as outfile:
dw=csv.writer(outfile)
dw.writerow(['index', 'client','product','sales'])
query = """
SELECT '{}' as client,
product,
COUNT(1) AS sales
FROM datasource
GROUP BY 1, 2
ORDER BY 3 DESC
LIMIT 100""".format(market,market)
with open('sales.csv'.format(client,client), 'w') as output:
output.write(client)
Run Code Online (Sandbox Code Playgroud)
我想要一个说sales_ellen.csv,sales_jose.csv的文件名 - 我知道这不是这样做的(它将每个都附加在sales.csv文件中).谢谢
我有什么 - > 1m16.044455998s
我想要的 - > 1m16s没有毫米,微米,纳秒.
Python3具有超级string.format打印:
'{} {}'.format('one', 'two')
Run Code Online (Sandbox Code Playgroud)
如果我的字符串在数组中,则一种方法是将它们键入:
a = ['one','two']
'{} {}'.format(a[0],a[1])
Run Code Online (Sandbox Code Playgroud)
但是,如何从数组中打印,而不必键入每个元素呢?
例如,损坏的代码:
a = ['one','two']
'{} {}'.format(a)
Run Code Online (Sandbox Code Playgroud)
给我一个预期的错误: IndexError: tuple index out of range
当然,玩','.join(a)不会有所帮助,因为它给出的是一个字符串而不是2。
(或者有没有办法用f弦更好地做到这一点?)
对于完全公开,我使用的是原始字符串,因为它具有某些几何意义,而我的真实代码如下所示:
hex_string = r'''
_____
/ \
/ \
,----( {} )----.
/ \ / \
/ {} \_____/ {} \
\ / \ /
\ / \ /
)----( {} )----(
/ \ / \
/ \_____/ \
\ {} / \ {} /
\ / \ /
`----( {} )----' …Run Code Online (Sandbox Code Playgroud) python ×5
python-3.x ×3
arrays ×1
c ×1
c# ×1
currency ×1
docstring ×1
file ×1
for-loop ×1
formatting ×1
go ×1
javascript ×1
performance ×1
printing ×1
python-2.7 ×1
string ×1
swift ×1
time ×1