Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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 - Fatal编程技术网

将文本文件中的值赋给可用于python下一步的变量

将文本文件中的值赋给可用于python下一步的变量,python,Python,我对python脚本非常陌生。我想创建一个脚本,可以打开一个文本文件c_name.txt,其中文本文件的内容将是单元格名称列表。打开后,我需要将所有单元格名称指定为一个变量。这是因为我需要单元名来打开下一个需要单元名的目录。我现在做的是打开文本文件并打印单元格名称。我不知道如何读取单元格名称并分配给变量。是否需要将所有单元格名称分配到一个数组中 我还需要进行分类,如果单元格名称以ce_u开头,则报告文件将位于ce_u目录中,而如果单元格名称以cell开头,则报告文件将位于cell目录中 这是我的

我对python脚本非常陌生。我想创建一个脚本,可以打开一个文本文件c_name.txt,其中文本文件的内容将是单元格名称列表。打开后,我需要将所有单元格名称指定为一个变量。这是因为我需要单元名来打开下一个需要单元名的目录。我现在做的是打开文本文件并打印单元格名称。我不知道如何读取单元格名称并分配给变量。是否需要将所有单元格名称分配到一个数组中

我还需要进行分类,如果单元格名称以ce_u开头,则报告文件将位于ce_u目录中,而如果单元格名称以cell开头,则报告文件将位于cell目录中

这是我的c_name.txt:

ce_clk
celladd
ce_sum
cellsub
以下是更新的代码:

#!/usr/bin/python

import os
import os.path

myPWD = os.getcwd()

# Implement functions get_test_directory and get_library_directory
# to retrieve correct directory paths
def get_test_directory( str ):
    return str

def get_library_directory (str ):
    os.chdir('..')
    str = os.getcwd()
    return str

test_directory = get_test_directory(myPWD)
library_directory = get_library_directory(myPWD)

cell_names_file = os.path.join(test_directory, "c_name.txt")

with open(cell_names_file, "r+") as f1:
    cell_names = f1.readlines()

for cell_name in cell_names:
    cell_directory = "ce_" if cell_name.startswith("ce_") else "cell"
    floc2 = os.path.join(library_directory, cell_directory, "{0}.rpt".format(cell_name))
    with open(floc2, "r+") as f2:
        for line in f2:
                 print line
新编辑: 我对line startswith的另一个问题是使其更通用,而不是使用如下硬编码:

for cell_name in cell_names:
        cell_directory = "ce_" if cell_name.startswith("ce_") else "cell"
        floc2 = os.path.join(library_directory, cell_directory,{0}.rpt".format(cell_name))
现在我正在使用“ce_u”和“cell”,是否可以要求脚本读取cell_u名称的前3个字符,即“ce_u”和“cel”,而不是自己定义它。我尝试使用这一行,但它似乎不起作用,因为它显示错误:

cell_directory = cell_name.startswith(cell_name,0,5)
错误:

AttributeError: 'bool' object has no attribute 'startswith'

在您当前的示例中,将值存储到列表中就足够了。稍后,当您需要这些值时,迭代列表并逐个获取每个单元格名称

#!/usr/bin/python

import os
import os.path

myPWD = os.getcwd()

# Implement functions get_test_directory and get_library_directory
# to retrieve correct directory paths
test_directory = get_test_directory(myPWD)
library_directory = get_library_directory(myPWD)

cell_names_file = os.path.join(test_directory, "c_name.txt")

with open(cell_names_file, "r+") as f1:
    cell_names = [line.strip() for line in f1]

for cell_name in cell_names:
    cell_directory = "ce_" if cell_name.startswith("ce_") else "cell"
    floc2 = os.path.join(library_directory, cell_directory, "{0}.rpt".format(cell_name))

编辑:代码已更新,以说明问题中的新信息。没有关于目录结构的精确信息,因此我使用了虚拟占位符。

我想打开cellname.rpt时遇到问题,因为他们找不到目录。这是因为目录测试和库处于同一级别。然而,从我的脚本来看,脚本认为库在测试后降低了一级。错误:workspace/test/。/library/ce_clk.rpt。实际位置是workspace/library/ce_clk.rpt。您正在代码中定义路径:
workspace=“%s/。”%(myPWD)
。删除对父文件夹(“..”)的引用,如果结构与您所说的一样,它应该可以工作。我尝试删除(“..”),它变成了workspace=“%s”%(myPWD),但随后我在第一个floc:Traceback(最近一次调用最后一次):File“/mmaz.py”,第12行,以f1:IOError:[Errno 2]的形式打开(floc,“r+”)没有这样的文件或目录:'/workspace/test/test/c_name.txt',不知何故,我已经修改了上面的问题,我需要识别以ce_或开头的单元格cell@mmaz答案已经更新。现在还不清楚您的目录结构是什么,所以您需要实现两个功能。