Discuss / Python / 初始化StringIO报错

初始化StringIO报错

Topic source

Angular2club

#1 Created at ... [Delete] [Delete and Lock User]
from io import StringIO

f = StringIO('hello!\nHi!\nGoodBye!')

while True:
    s = f.readline()
    if s == '':
        break
    print(s)

运行段时,报以下错误:

TypeError: initial_value must be unicode or None, not str

upbeat_peach

#2 Created at ... [Delete] [Delete and Lock User]

emmmm我的没有出错呢,也没看出来你哪里出问题了…… 照理说strip()只是去除空格吖……

f = StringIO(u'hello!\nHi!\nGoodBye!')

可能是python版本的问题,我用的python2.7.16. 也报了这个错,字符前加个u就行了

coLBooy

#5 Created at ... [Delete] [Delete and Lock User]

确实可能是版本问题,python3与python2的String语法有一些不同,详情可百度。


  • 1

Reply