首页 > 解决方案 > 使用 sys.stdin 的输入字符串与使用 sys.argv 的不一样?

问题描述

我有一个小应用程序,它接受一个字符串(一个 DNA 序列)并进行多次操作。

当我创建这个时,我使用这种方法作为输入:

user_input = sys.argv[1]

所以,如果一个类型python main.py TATA 有效。

现在,我想先用它echo TATA | python main.py来做那件事,我已经评论# user_input = sys.argv[1]过(目前),我已经添加了这一行

user_input = sys.stdin.read()

# and to see the input I have added

print(user_input)

当我echo TATA | python main.py输入输入是 TATA 时,这很好。但是,由于我不明白的原因,我的应用程序的 python 类不接受这一点,我得到的错误也没有解释原因。

我在这里复制并粘贴了 main.py 的代码,直到第一个错误的行

错误一:

Traceback(最近一次调用最后一次):文件“/Users/A/A/A/A/main.py”,第 29 行,在 print(DNA.length())

import sys
from clases.sequence import Sequence
from clases.read_file import Read_file

# The input:
# user_input = sys.argv[1]


user_input = sys.stdin.read()
print(user_input)



# To be able to get two type of input
# A sequence from the command line 
    # python main.py TATA
# A file.txt with the sequence
    # python main.py PATH/file_name.txt

if 'txt' in user_input:
    DNA = Read_file(user_input)
    data_loaded = DNA.load_data()
    DNA = Sequence(data_loaded)
else:
    DNA = Sequence(user_input)

# print(DNA.load_data())
print("Length:")
print(DNA.length()   # LINE 29

错误 2。

文件“/A/AAtop/A/A/clases/sequence.py”,第 46 行,长度返回 len(self.DNA) AttributeError: 'Sequence' 对象没有属性 'DNA'

该脚本的第 42 到 46 行

def length(self):
        '''
        That returns the length of the original sequence
        '''
        return len(self.DNA)

标签: pythonstdinsys

解决方案


推荐阅读