Python 类中的字符串存储包括换行符 [英] String storage in Python Class includes newline character
问题描述
我有一个如下所示的输出类(有些编辑):
I have an output class that looks like the following (somewhat redacted):
from colorclass import Color
from colorclass import disable_all_colors, enable_all_colors, is_enabled
from time import localtime, strftime
from ApplicationFiles.lib.core.__version__ import __version__
from enum import IntEnum
class OutputHelper(object):
def __init__(self, arguments, app):
if arguments.nocolor:
disable_all_colors()
self.domain = "undefined"
self.severity_print = arguments.severity
self.verbose = arguments.verbose
self.silent = arguments.silent
self.seperator = "=============================================="
self.modules_count = app.modules_count()
# this is here to allow easier redirection of output to other
# interfaces later on in the project
def write(self, severity, message):
self.terminal(severity, message)
def terminal(self, severity, message):
if severity == 0 and not self.verbose:
return
if severity < self.severity_print:
return
formatting = {
0: Color('{autoblue}[VERBOSE]{/autoblue}')
}
leader = formatting.get(severity, '[#]')
format_args = {
'time': strftime("%H:%M:%S", localtime()),
'domain': self.domain,
'leader': leader,
'message': message,
}
template = '[{time}] [{domain}] {leader} {message}'
print(template.format(**format_args))
class Level(IntEnum):
VERBOSE = 0
我正在从文件中读取域列表,并通过此类将其传递给以下内容:
I'm reading in a list of domains from a file, and passing it via this class to the following:
for domain in arguments.domain_list:
domains.append(domain)
for domain in domains:
output.domain = domain
app.modules['example_module'].run(arguments, output)
然后是:
def run(arguments, output):
output.write(Level.VERBOSE, 'TEST DOMAIN')
无论出于何种原因,每次打印后都会打印换行符.例如:
For whatever reason this is printing newlines after each print. For example:
1 modules loaded
==============================================
[16:57:25] [testone.com
] [VERBOSE] TEST DOMAIN
[16:57:25] [testtwo.com
] [VERBOSE] TEST DOMAIN
[16:57:25] [testthree.com
] [VERBOSE] TEST DOMAIN
这应该是:
1 modules loaded
==============================================
[16:57:25] [testone.com] [VERBOSE] TEST DOMAIN
[16:57:25] [testtwo.com] [VERBOSE] TEST DOMAIN
[16:57:25] [testthree.com] [VERBOSE] TEST DOMAIN
我知道我对我的对象犯了错误,或者我在某处引用它们的方式 - 但我很难看到它.我不明白什么?在我的 TerminalOutput() 函数中处理这个似乎很愚蠢,我宁愿在存储域时对其进行排序.
I know I've made a mistake with my objects, or how I'm referencing them somewhere - but I'm struggling to see it. What am I not understanding? It seems silly to handle this in my TerminalOutput() function and I'd rather sort it as the domain is stored.
文件正在被读入:
def readable_file(parser, arg):
if not os.path.exists(arg):
parser.error("The file %s does not exist!" % arg)
else:
return open(arg, 'r') # return an open file handle
在 argparse 类中使用:
Which is called in an argparse class using:
domains.add_argument(
'-dL', dest='domain_list', required=False,
help='Specify a list of target domain names.',
metavar="FILE",
type=lambda x: CliFileHelper.readable_file(parser, x)
)
推荐答案
您没有向我们展示您如何阅读域,而是使用:
You are not showing us how you read the domains, but using:
for domain in domainFileDescriptor:
domains.append(domain)
#or
domains=domainFileDescriptor.readlines()
将在从文件中读取的每一行的末尾保留换行符.你需要剥离它们:
will conserve the newline at the end of every line read from the file. You need to strip them:
for domain in domainFileDescriptor:
domains.append(domain.strip())
提示是换行符的位置(在域之后).注意:文件描述符和句柄在这个上下文中是一回事.
The hint is the placement of the newline (right after the domain). Note: a file descriptor and handle is the same thing in this context.
这篇关于Python 类中的字符串存储包括换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!