UTF-8 problem in python when reading chars

后端 未结 5 1670
温柔的废话
温柔的废话 2021-02-06 06:58

I\'m using Python 2.5. What is going on here? What have I misunderstood? How can I fix it?

in.txt:

Stäckövérfløw

code.py

5条回答
  •  逝去的感伤
    2021-02-06 07:26

    Use codecs.open instead, it works for me.

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    print """Content-Type: text/plain; charset="UTF-8"\n"""
    f = codecs.open('in','r','utf8')
    for line in f:
        print line
        for i in line:
            print i,
    f.close()
    

提交回复
热议问题