首页 > 解决方案 > 如何在 Python 中读取每行文本并将其转换为 ASCII 小数?

问题描述

我试图做的事情:

f = open('j:/text.txt', 'r')
lines = []
for line in f:
    lines.append(line)
print (''.join(str(ord(c)) for c in line))
print (line)
print (lines)

但这仅转换最后一行并忽略前面的行。

标签: pythonpython-3.xord

解决方案


我认为您想打印每一行的 ASCII 等效项和行本身。将前两个print语句放入for循环中(缩进一次)。

f = open('j:/text.txt', 'r')
lines = []
for line in f:
    lines.append(line)
    print (''.join(str(ord(c)) for c in line))
    print (line)
print (lines)

推荐阅读