我想将循环输出打印到同一行的屏幕上.
我如何以最简单的方式处理Python 3.x.
我知道这个问题已经被要求用于Python 2.7,在行的末尾使用逗号即打印I,但我找不到Python 3.x的解决方案.
i = 0
while i <10:
i += 1
## print (i) # python 2.7 would be print i,
print (i) # python 2.7 would be 'print i,'
Run Code Online (Sandbox Code Playgroud)
屏幕输出.
1
2
3
4
5
6
7
8
9
10
Run Code Online (Sandbox Code Playgroud)
我想要打印的是:
12345678910
Run Code Online (Sandbox Code Playgroud)
新读者访问此链接以及http://docs.python.org/release/3.0.1/whatsnew/3.0.html
问题:我需要将字符串转换为十六进制,然后格式化十六进制输出.
tmp = b"test"
test = binascii.hexlify(tmp)
print(test)
Run Code Online (Sandbox Code Playgroud)
输出:b'74657374'
我想格式化这个十六进制输出看起来像这样:74:65:73:74
我遇到了障碍,不知道从哪里开始.我确实想过将输出再次转换为字符串,然后尝试格式化它,但必须有一个更简单的方法.
任何帮助将不胜感激,谢谢.
==========
操作系统:Windows 7
tmp = "test"
hex = str(binascii.hexlify(tmp), 'ascii')
print(hex)
formatted_hex = ':'.join(hex[i:i+2] for i in range(0, len(hex), 2))
print(formatted_hex
Run Code Online (Sandbox Code Playgroud)
[错误]回溯(最近一次调用最后一次):文件"C:\ pkg\scripts\Hex\hex.py",第24行,十六进制= str(binascii.hexlify(tmp),'ascii')TypeError:'str '不支持缓冲接口
此代码仅在使用tmp = b'test'时才有效.我需要能够使用tmp = importString,因为我正在从文件顺序向其传递另一个值,以使我的代码段正常工作.有什么想法吗?