英文:
PYTHON: reading list from file
问题
以下是翻译好的部分:
- 为什么函数 "read_from_file()" 不起作用?
- 有没有其他方法可以在全局范围内修改列表,而不是在函数 "working_func()" 中?
- 函数 "func_return()" 是否适合替代情况 2.?
这是代码:
test_file.txt:
dog
horse
spider
from pathlib import Path
def read_from_file(values):
path = Path('test_file.txt')
file_contents = path.read_text()
values = file_contents.split()
def working_func():
global animals
path = Path('test_file.txt')
file_contents = path.read_text()
animals = file_contents.split()
def func_return():
path = Path('test_file.txt')
file_contents = path.read_text()
values = file_contents.split()
return values
animals = []
read_from_file(animals)
read_from_file(animals)
print(animals) #[]
working_func()
print(animals) #['dog', 'horse', 'spider']
animals = func_return()
print(animals) #['dog', 'horse', 'spider']
我想找出执行这种任务的最有效和常见的方法。
英文:
i'm storing animals spiecies' names inside a file, one name in a line, and i'd like to read them from file into a list.
- Why isn't the function "read_from_file()" working?
- Is there an option to modify the list globally in other way than in function "working_func()"
- Is the function "func_return()" a proper substitute to the case 2.?
Here's the code:
test_file.txt:
dog
horse
spider
from pathlib import Path
def read_from_file(values):
path = Path('test_file.txt')
file_contents = path.read_text()
values = file_contents.split()
def working_func():
global animals
path = Path('test_file.txt')
file_contents = path.read_text()
animals = file_contents.split()
def func_return():
path = Path('test_file.txt')
file_contents = path.read_text()
values = file_contents.split()
return values
animals = []
read_from_file(animals)
read_from_file(animals)
print(animals) #[]
working_func()
print(animals) #['dog', 'horse', 'spider']
animals = func_return()
print(animals) #['dog', 'horse', 'spider']
i'd like to find out what is the most efficient and common way to do such task
答案1
得分: 1
考虑将文件路径作为参数传递,而不是将其硬编码到函数中。以下是一个示例:
import os
test_data_path = "./test_data.txt"
def read_from_file(filepath):
animals = []
with open(filepath) as f:
lines = f.readlines()
for line in lines:
animals.append(line.strip())
return animals
animals = read_from_file(test_data_path)
print(animals)
返回:
['dog', 'horse', 'spider']
英文:
Consider passing the filepath as an argument rather than coding that into the function. Here is one example:
import os
test_data_path = "./test_data.txt"
def read_from_file(filepath):
animals = []
with open(filepath) as f:
lines = f.readlines()
for line in lines:
animals.append(line.strip())
return animals
animals = read_from_file(test_data_path)
print(animals)
Returns:
['dog', 'horse', 'spider']
答案2
得分: 0
在 read_from_file()
中,基本上你不返回任何东西。它从文件中读取数据,将值存储在 values
中,但在函数退出时,这个变量会超出作用域。
你的 working_func()
函数使用了 global
关键字,这意味着这个变量在全局范围内,并且在调用函数后可以从任何代码中检索到存储在全局变量中的值。
func_return()
返回分配给变量 animals
的值,因此这意味着"animals - 是 func_return() 返回的全部内容"。
英文:
Basically, you just don't return anything in read_from_file()
. It reads from files, stores values in values
, but when exiting the function, this variable goes out of scope.
Your working_func()
function uses the global
keyword, which means
that this variable is in global scope and stores a value in a global variable that can be retrieved from anywhere in your code after calling your function.
func_return()
returns the value that is assigned to the variable animals
on line 28, so this means that "animals - is all that func_return() returns".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论