简体   繁体   English

构造:Python - 打印无新行

[英]Construct : Python - Print without new line

I am literally going crazy:我真的快疯了:

I am using the Pyhon Library: construct我正在使用 Pyhon 库:构造

What I want to acheive is to print the parsed stream on a single line, instead of having Value1 and Value 2 on different lines.我想要实现的是在一行上打印解析的 stream,而不是在不同的行上打印 Value1 和 Value 2。 Is there a way to do so?有没有办法这样做?

data = 0xAF

from construct import *

testOut1 = BitStruct("Value1" /Nibble, "Value2" /Nibble).parse(data) 

print(testOut1)

This is WHat I have:这就是我所拥有的:

Container:

Value1 = 10
Value2 = 15

But I would line to have something like:但我会排队有类似的东西:

Value1 = 10, Value2 = 15

You can modify a string before you print it.您可以在打印之前修改字符串。

print(str(testOut1).replace("\n", ", "))

Best way to do it is to use最好的方法是使用

print('hello', end='')

In your case our code will be:在您的情况下,我们的代码将是:

data = 0xAF

from construct import *

testOut1 = BitStruct("Value1" /Nibble, "Value2" /Nibble).parse(data) 

print('Value1 =' + str(testOut1.Value1) + ',', end='')
print('Value2 =' + str(testOut1.Value2) + '', end='')

Or better:或更好:

print('Value1 =' + str(testOut1.Value1) + ',' + 'Value1 =' + str(testOut1.Value2))

Best regards,此致,

Reference: https://128mots.com/index.php/2022/03/09/python-print-without-newline-2/参考: https://128mots.com/index.php/2022/03/09/python-print-without-newline-2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM