pythonpython-3.xord

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


What I have tried to do:

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)

But this converts only the last line and ignores the previous ones.


Solution

  • Try this:

    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))