中文翻译:从文件读取列表

huangapple go评论76阅读模式
英文:

PYTHON: reading list from file

问题

以下是翻译好的部分:

  1. 为什么函数 "read_from_file()" 不起作用?
  2. 有没有其他方法可以在全局范围内修改列表,而不是在函数 "working_func()" 中?
  3. 函数 "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.

  1. Why isn't the function "read_from_file()" working?
  2. Is there an option to modify the list globally in other way than in function "working_func()"
  3. 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".

More about returns

huangapple
  • 本文由 发表于 2023年6月15日 05:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76477575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定