python - 使用 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)
解决方案
推荐阅读
- c++ - 返回子字符串
- python - Processing.py 草图错误:未闭合的括号/引号
- python - 自动搜索特定excel文件的文件夹并导入pandas
- android - Android QuickBlox SDK 客户端未连接或不再连接
- node.js - .env 文件未定义使用 require('dotenv').config();
- php - 使用 HTMLpurifier 过滤 img 标签的 URL
- android - 图像是手动设计的吗?
- sas - 将 Looker 连接到 SAS
- c# - 如果不同的属性与不同列表中的值匹配,则为列表中的每个对象设置属性
- mysql - 从 Symfony Docker 容器到 MariaDB 容器的 Doctrine Connection 被拒绝