Python 读取文件并将文件中的数据提供给函数
因此,基本上我需要编写一些代码,提示用户输入文件名,并尝试打开提供的任何名称。然后程序应该从文件中读取每一行,并将其提供给我创建的函数,该函数随后将文件中的文本转换为元组 到目前为止,我的文件中有以下内容: 这是函数的一部分: 希望这有助于:Python 读取文件并将文件中的数据提供给函数,python,Python,因此,基本上我需要编写一些代码,提示用户输入文件名,并尝试打开提供的任何名称。然后程序应该从文件中读取每一行,并将其提供给我创建的函数,该函数随后将文件中的文本转换为元组 到目前为止,我的文件中有以下内容: 这是函数的一部分: 希望这有助于: from os.path import isfile, exists filename = input('Enter file name : ') if(exists(filename) and isfile(filename)):
from os.path import isfile, exists
filename = input('Enter file name : ')
if(exists(filename) and isfile(filename)):
with open(filename) as f:
content = f.readlines()
# Call your function here with content as argument and process it, content has all lines in the file as a list
我不确定你分享的第二个函数链接是否适合这里。但对于您想要做的事情,一个通用的解决方案是:
filepath = input("Message")
with open(filepath) as fp:
line = fp.readline()
while line:
line = fp.readline()
custom_function(line) #Define and call your function
希望能有所帮助。添加代码截图是个坏主意,请分享您的代码。