【问题标题】:Python: TypeError: coercing to Unicode: need string or buffer, module foundPython:TypeError:强制转换为 Unicode:需要字符串或缓冲区,找到模块
【发布时间】:2016-04-06 01:04:09
【问题描述】:

我是 Python 新手,刚刚学习。

我将其中一个文件作为输入,我想使用 python 在控制台上打印文本。

# This will take the file as input

import fileinput
for line in fileinput.input():

    print "The file name you provided is " + fileinput.filename()

    #file of = open(fileinput, "r", 0)
    with open(fileinput,'r') as myfile:
        data=myfile.read()
        print "This is your actual data \n\n" + data

我遇到的错误是:

TypeError: coercing to Unicode: need string or buffer, module found

谁能帮帮我。

【问题讨论】:

  • 您根本不需要打开文件。看看fileinput 模块实际上做了什么。

标签: python file-io typeerror


【解决方案1】:

这个特殊的问题在下面一行:

with open(fileinput,'r') as myfile:

fileinput 是一个模块。我想你的意思是:

with open(fileinput.filename(),'r') as myfile:

【讨论】:

    猜你喜欢
    • 2017-09-23
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 2021-03-28
    • 2015-02-01
    • 2013-11-23
    • 2014-12-21
    • 2012-04-12
    相关资源
    最近更新 更多