我正在编写一个 python 脚本,我需要在其中打开一个“.txt”文件夹并分析其中的文本。
我已将此“.txt”文档保存在与 Python 脚本相同的文件夹中。
但是,当我去打开文件时;file = open("words.txt",'r')
我收到错误:No such file or directory: 'words.txt'。
我不明白为什么会发生这种情况?
我有一个类似于下面的 DataFrame:,我想向它添加一个 Streak 列(参见下面的示例):
Date Home_Team Away_Team Winner Streak
2005-08-06 A G A 0
2005-08-06 B H H 0
2005-08-06 C I C 0
2005-08-06 D J J 0
2005-08-06 E K K 0
2005-08-06 F L F 0
2005-08-13 A B A 1
2005-08-13 C D D 1
2005-08-13 E F F 0
2005-08-13 G H H 0
2005-08-13 I J J 0
2005-08-13 K L K 1
2005-08-20 B C B 0
2005-08-20 A D A 2
2005-08-20 …Run Code Online (Sandbox Code Playgroud) 我是测试新手,我偶然发现了 pytest 固定装置,但我不完全确定何时使用它们以及它们为什么有用。
例如,请参阅以下代码:
import pytest
@pytest.fixture
def input_value():
input = 39
return input
def test_divisible_by_3(input_value):
assert input_value % 3 == 0
def test_divisible_by_6(input_value):
assert input_value % 6 == 0
Run Code Online (Sandbox Code Playgroud)
这里pytest.fixture的作用是什么?为什么我们不能简单地创建一个被调用的函数input_value()并在测试函数内运行该函数?例如:
import pytest
def input_value():
input = 39
return input
def test_divisible_by_3():
assert input_value() % 3 == 0
def test_divisible_by_6():
assert input_value() % 6 == 0
Run Code Online (Sandbox Code Playgroud)
为什么我们不能这样做?使用fixture有什么用?
python ×2
dataframe ×1
decorator ×1
directory ×1
file ×1
numpy ×1
pandas ×1
pytest ×1
python-3.x ×1
unit-testing ×1