繁体   English   中英

TypeError:强制转换为Unicode:需要字符串或缓冲区,在python中找到文件(将现有文件中的数据写入单个文件)

[英]TypeError: coercing to Unicode: need string or buffer, file found in python(writing data from existing files into single file)

我应该将数据从三个现有文件写入python中的单个文件。 我遇到错误“ TypeError:强制转换为Unicode:需要字符串或缓冲区”,找到文件。 我的三个现有文件分别是e,g和m,我创建了一个名为results的文件,用于从上述三个现有文件中写入数据。 我非常感谢您的帮助

filenames = [e,g,m],其中open(结果,“ w”)作为outfile:用于文件名中的文件:open(file)作为infile:用于infile中的行:outfile.write(line)

您的文件名应为字符串。 文件名e,m,g应为“ e”,“ m”,“ g”,结果应为“结果”。 请参考下面的代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

filenames= ["e","g","m"]

with open("results", "w") as outfile:
    for file in filenames:
        with open(file) as infile:
            for line in infile:
                outfile.write(line)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM