如何在 Python 处读取每行文本并将其转换为 ASCII 小数?

How can I read text per line at Python and convert then to ASCII decimals?

我尝试过的事情:

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)

但这只转换最后一行而忽略前面的行。

我认为您想打印每一行 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)

试试这个:

with open('j:/text.txt', 'r', encoding="ascii") as file :
    lines = f.readlines()

for line in lines:
    line = line.replace("\n", "")
    print(''.join(str(ord(c)) for c in line))