python: unicode problem

后端 未结 3 1639
隐瞒了意图╮
隐瞒了意图╮ 2021-02-14 11:02

I am trying to decode a string I took from file:

file = open (\"./Downloads/lamp-post.csv\", \'r\')
data = file.readlines()
data[0]
3条回答
  •  执笔经年
    2021-02-14 11:18

    EDIT

    Since you posted 2.7 this is the 2.7 solution:

    file = open("./Downloads/lamp-post.csv", "r")
    data = [line.decode("utf-16", "replace") for line in file]
    

    Ignoring undecodeable characters:

    file = open("./Downloads/lamp-post.csv", "r")
    data = [line.decode("utf-16", "ignore") for line in file]
    

提交回复
热议问题