问题:类似于sprintf的Python功能
我想创建一个字符串缓冲区来进行大量处理,格式化,最后使用sprintf
Python中的C样式功能将缓冲区写入文本文件中。由于条件语句,我无法将它们直接写到文件中。
例如伪代码:
sprintf(buf,"A = %d\n , B= %s\n",A,B)
/* some processing */
sprint(buf,"C=%d\n",c)
....
...
fprintf(file,buf)
所以在输出文件中,我们有这种o / p:
A= foo B= bar
C= ded
etc...
编辑,以澄清我的问题:
buf
是一个大缓冲区,其中包含所有使用sprintf格式化的字符串。按照您的示例,buf
将仅包含当前值,而不包含旧值。例如,buf
我最初写的书A= something ,B= something
后来C= something
被附加在同一书中buf
,但是在您的Python答案buf
中仅包含最后一个值,这不是我想要的-我想拥有自开始以来所做的buf
所有printf
操作,例如in C
。
I would like to create a string buffer to do lots of processing, format and finally write the buffer in a text file using a C-style sprintf
functionality in Python. Because of conditional statements, I can’t write them directly to the file.
e.g pseudo code:
sprintf(buf,"A = %d\n , B= %s\n",A,B)
/* some processing */
sprint(buf,"C=%d\n",c)
....
...
fprintf(file,buf)
So in the output file we have this kind of o/p:
A= foo B= bar
C= ded
etc...
Edit, to clarify my question:
buf
is a big buffer contains all these strings which have formatted using sprintf.
Going by your examples, buf
will only contain current values, not older ones.
e.g first in buf
I wrote A= something ,B= something
later C= something
was appended in the same buf
, but in your Python answers buf
contains only last value, which is not I want – I want buf
to have all the printf
s I have done since the beginning, like in C
.
回答 0
Python %
为此提供了一个运算符。
>>> a = 5
>>> b = "hello"
>>> buf = "A = %d\n , B = %s\n" % (a, b)
>>> print buf
A = 5
, B = hello
>>> c = 10
>>> buf = "C = %d\n" % c
>>> print buf
C = 10
有关所有受支持的格式说明符,请参见此参考。
您也可以使用format
:
>>> print "This is the {}th tome of {}".format(5, "knowledge")
This is the 5th tome of knowledge
Python has a %
operator for this.
>>> a = 5
>>> b = "hello"
>>> buf = "A = %d\n , B = %s\n" % (a, b)
>>> print buf
A = 5
, B = hello
>>> c = 10
>>> buf = "C = %d\n" % c
>>> print buf
C = 10
See this reference for all supported format specifiers.
You could as well use format
:
>>> print "This is the {}th tome of {}".format(5, "knowledge")
This is the 5th tome of knowledge
回答 1
如果我正确理解了您的问题,那么format()就是您所要的东西,以及它的迷你语言。
python 2.7及更高版本的愚蠢示例:
>>> print "{} ...\r\n {}!".format("Hello", "world")
Hello ...
world!
对于早期的python版本:(已通过2.6.2测试)
>>> print "{0} ...\r\n {1}!".format("Hello", "world")
Hello ...
world!
If I understand your question correctly, format() is what you are looking for, along with its mini-language.
Silly example for python 2.7 and up:
>>> print "{} ...\r\n {}!".format("Hello", "world")
Hello ...
world!
For earlier python versions: (tested with 2.6.2)
>>> print "{0} ...\r\n {1}!".format("Hello", "world")
Hello ...
world!
回答 2
我并不完全确定我了解您的目标,但是您可以将StringIO
实例用作缓冲区:
>>> import StringIO
>>> buf = StringIO.StringIO()
>>> buf.write("A = %d, B = %s\n" % (3, "bar"))
>>> buf.write("C=%d\n" % 5)
>>> print(buf.getvalue())
A = 3, B = bar
C=5
与不同sprintf
,您只需将字符串传递给buf.write
,即可使用%
运算符或format
字符串方法对其进行格式化。
您当然可以定义一个函数来获取sprintf
您希望的接口:
def sprintf(buf, fmt, *args):
buf.write(fmt % args)
可以这样使用:
>>> buf = StringIO.StringIO()
>>> sprintf(buf, "A = %d, B = %s\n", 3, "foo")
>>> sprintf(buf, "C = %d\n", 5)
>>> print(buf.getvalue())
A = 3, B = foo
C = 5
I’m not completely certain that I understand your goal, but you can use a StringIO
instance as a buffer:
>>> import StringIO
>>> buf = StringIO.StringIO()
>>> buf.write("A = %d, B = %s\n" % (3, "bar"))
>>> buf.write("C=%d\n" % 5)
>>> print(buf.getvalue())
A = 3, B = bar
C=5
Unlike sprintf
, you just pass a string to buf.write
, formatting it with the %
operator or the format
method of strings.
You could of course define a function to get the sprintf
interface you’re hoping for:
def sprintf(buf, fmt, *args):
buf.write(fmt % args)
which would be used like this:
>>> buf = StringIO.StringIO()
>>> sprintf(buf, "A = %d, B = %s\n", 3, "foo")
>>> sprintf(buf, "C = %d\n", 5)
>>> print(buf.getvalue())
A = 3, B = foo
C = 5
回答 3
使用格式运算符%
:
buf = "A = %d\n , B= %s\n" % (a, b)
print >>f, buf
回答 4
您可以使用字符串格式:
>>> a=42
>>> b="bar"
>>> "The number is %d and the word is %s" % (a,b)
'The number is 42 and the word is bar'
但这已在Python 3中删除,您应该使用“ str.format()”:
>>> a=42
>>> b="bar"
>>> "The number is {0} and the word is {1}".format(a,b)
'The number is 42 and the word is bar'
You can use string formatting:
>>> a=42
>>> b="bar"
>>> "The number is %d and the word is %s" % (a,b)
'The number is 42 and the word is bar'
But this is removed in Python 3, you should use “str.format()”:
>>> a=42
>>> b="bar"
>>> "The number is {0} and the word is {1}".format(a,b)
'The number is 42 and the word is bar'
回答 5
要插入很长的字符串,最好为不同的参数使用名称,而不是希望它们放在正确的位置。这也使替换多次重复变得更容易。
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
取自“ 格式”示例,其中Format
还显示了所有其他与之相关的答案。
To insert into a very long string it is nice to use names for the different arguments, instead of hoping they are in the right positions. This also makes it easier to replace multiple recurrences.
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
Taken from Format examples, where all the other Format
-related answers are also shown.
回答 6
这可能是从C代码到Python代码最接近的翻译。
A = 1
B = "hello"
buf = "A = %d\n , B= %s\n" % (A, B)
c = 2
buf += "C=%d\n" % c
f = open('output.txt', 'w')
print >> f, c
f.close()
%
Python中的运算符几乎执行与C相同的操作sprintf
。您也可以将字符串直接打印到文件中。如果涉及许多此类字符串格式的字符串,那么明智的做法是使用StringIO
对象来加快处理时间。
因此+=
,不要这样做,而是这样做:
import cStringIO
buf = cStringIO.StringIO()
...
print >> buf, "A = %d\n , B= %s\n" % (A, B)
...
print >> buf, "C=%d\n" % c
...
print >> f, buf.getvalue()
This is probably the closest translation from your C code to Python code.
A = 1
B = "hello"
buf = "A = %d\n , B= %s\n" % (A, B)
c = 2
buf += "C=%d\n" % c
f = open('output.txt', 'w')
print >> f, c
f.close()
The %
operator in Python does almost exactly the same thing as C’s sprintf
. You can also print the string to a file directly. If there are lots of these string formatted stringlets involved, it might be wise to use a StringIO
object to speed up processing time.
So instead of doing +=
, do this:
import cStringIO
buf = cStringIO.StringIO()
...
print >> buf, "A = %d\n , B= %s\n" % (A, B)
...
print >> buf, "C=%d\n" % c
...
print >> f, buf.getvalue()
回答 7
如果您想要类似python3打印功能的东西,但是想要一个字符串:
def sprint(*args, **kwargs):
sio = io.StringIO()
print(*args, **kwargs, file=sio)
return sio.getvalue()
>>> x = sprint('abc', 10, ['one', 'two'], {'a': 1, 'b': 2}, {1, 2, 3})
>>> x
"abc 10 ['one', 'two'] {'a': 1, 'b': 2} {1, 2, 3}\n"
或'\n'
结尾没有:
def sprint(*args, end='', **kwargs):
sio = io.StringIO()
print(*args, **kwargs, end=end, file=sio)
return sio.getvalue()
>>> x = sprint('abc', 10, ['one', 'two'], {'a': 1, 'b': 2}, {1, 2, 3})
>>> x
"abc 10 ['one', 'two'] {'a': 1, 'b': 2} {1, 2, 3}"
If you want something like the python3 print function but to a string:
def sprint(*args, **kwargs):
sio = io.StringIO()
print(*args, **kwargs, file=sio)
return sio.getvalue()
>>> x = sprint('abc', 10, ['one', 'two'], {'a': 1, 'b': 2}, {1, 2, 3})
>>> x
"abc 10 ['one', 'two'] {'a': 1, 'b': 2} {1, 2, 3}\n"
or without the '\n'
at the end:
def sprint(*args, end='', **kwargs):
sio = io.StringIO()
print(*args, **kwargs, end=end, file=sio)
return sio.getvalue()
>>> x = sprint('abc', 10, ['one', 'two'], {'a': 1, 'b': 2}, {1, 2, 3})
>>> x
"abc 10 ['one', 'two'] {'a': 1, 'b': 2} {1, 2, 3}"
回答 8
就像是…
greetings = 'Hello {name}'.format(name = 'John')
Hello John
Something like…
greetings = 'Hello {name}'.format(name = 'John')
Hello John
回答 9
回答 10
两种方法是写入字符串缓冲区或将行写入列表,然后再将它们连接。我认为该StringIO
方法更具pythonic功能,但在python 2.6之前不起作用。
from io import StringIO
with StringIO() as s:
print("Hello", file=s)
print("Goodbye", file=s)
# And later...
with open('myfile', 'w') as f:
f.write(s.getvalue())
您也可以不使用ContextMananger
(s = StringIO()
)使用它们。当前,我正在使用带有print
函数的上下文管理器类。为了能够插入调试或奇数页的需求,此片段可能很有用:
class Report:
... usual init/enter/exit
def print(self, *args, **kwargs):
with StringIO() as s:
print(*args, **kwargs, file=s)
out = s.getvalue()
... stuff with out
with Report() as r:
r.print(f"This is {datetime.date.today()}!", 'Yikes!', end=':')
Two approaches are to write to a string buffer or to write lines to a list and join them later. I think the StringIO
approach is more pythonic, but didn’t work before Python 2.6.
from io import StringIO
with StringIO() as s:
print("Hello", file=s)
print("Goodbye", file=s)
# And later...
with open('myfile', 'w') as f:
f.write(s.getvalue())
You can also use these without a ContextMananger
(s = StringIO()
). Currently, I’m using a context manager class with a print
function. This fragment might be useful to be able to insert debugging or odd paging requirements:
class Report:
... usual init/enter/exit
def print(self, *args, **kwargs):
with StringIO() as s:
print(*args, **kwargs, file=s)
out = s.getvalue()
... stuff with out
with Report() as r:
r.print(f"This is {datetime.date.today()}!", 'Yikes!', end=':')