Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何打开我的文件?_Python_Python 3.x - Fatal编程技术网

Python 如何打开我的文件?

Python 如何打开我的文件?,python,python-3.x,Python,Python 3.x,你好,我正在开发一个功能,它可以合并两个单独的.txt文件并输出一封个性化的信件。问题是,我可以将我的文本包含在funciton模块中,它工作得非常好。但是当我试图在函数中打开它们并被函数使用时,我得到了这个结果 错误消息: 回溯(最近一次呼叫最后一次): 文件“/Users/nathandavis9752/CP104/davi0030_a10/src/q2_function.py”,第25行,在 数据=清洁数据(第2季度) 文件“/Users/nathandavis9752/CP104/dav

你好,我正在开发一个功能,它可以合并两个单独的.txt文件并输出一封个性化的信件。问题是,我可以将我的文本包含在funciton模块中,它工作得非常好。但是当我试图在函数中打开它们并被函数使用时,我得到了这个结果

错误消息:

回溯(最近一次呼叫最后一次): 文件“/Users/nathandavis9752/CP104/davi0030_a10/src/q2_function.py”,第25行,在 数据=清洁数据(第2季度) 文件“/Users/nathandavis9752/CP104/davi0030_a10/src/q2_function.py”,第17行,在cleanData中 为查询中的项返回[item.strip().split('\n\n')。split('--')] AttributeError:“文件”对象没有属性“拆分”

代码:

q2.txt文件: letter.txt文件:
您正在尝试拆分文件缓冲区而不是字符串

def cleanData(query):
    return [item.strip().split('\n\n') for item in query.read().split('--')]

仍然不工作。。。错误消息:回溯(最近一次调用最后一次):文件“/Users/nathandavis9752/CP104/davi0030_a10/src/q2_function.py”,第31行,字母=[writeLetter(letter,variables,person)for person in data]文件“/Users/nathandavis9752/CP104/davi0030_a10/src/q2_function.py”,第22行,在writeLetter template=template.replace(variables[i],替换[i])索引器:列出索引超出范围的回溯(最近一次调用):文件“/Users/nathandavis9752/CP104/dav0030_a10/src/q2_function.py”,第25行,在data=cleanData(q2)文件“/Users/nathandavis9752/CP104/dav0030_a10/src/q2_function.py”中,在cleanData返回[item.strip().split(“\n\n”中对于query.read().split('--')]AttributeError:'str'对象没有属性'read'@stacker保持代码不变,这意味着恢复
q2
变量。然后将函数更改为此。使用原始代码,然后关闭函数。记住读取错误。重要的部分是最后一部分:
AttributeError:“file”对象没有属性“split”
Michael

dawn

lock hart ln

Dublin

--

kate

Nan

webster st

king city

--

raj

zakjg

late Road

Toronto

--

dave

porter

Rock Ave

nobleton
[fname] [lname]
[street]
[city]

Dear [fname]:

   As a fellow citizen of [city], you and all your neighbours 
on [street] are invited to a celebration this Saturday at 
[city]'s Central Park. Bring beer and food!
def cleanData(query):
    return [item.strip().split('\n\n') for item in query.read().split('--')]