Python 标准输出显示 emacs 中的隐藏字符
Python standard output shows hidden characters in emacs
我编写了一个 python 函数来生成 list
个单词。首先,它读取一个由换行符分隔的单词组成的文件。根据单词的不同,它要么将其插入到 list
中,要么插入一个空白 space,由制表符表示。这是代码的相关部分:
xclusives1
、xclusives2
、dups
,都是list
s.
generator
是包含该函数的 class 的实例。
def xfile1(self):
for item1 in self.lines1:
for item2 in self.lines2:
if item1 == item2:
self.xclusives1.append("\t")
self.xclusives2.append("\t")
self.dups.append(item1)
break
self.xclusives1.append(item1)
self.xclusives2.append("\t")
self.dups.append("\t")
...
...
...
...
print generator.xclusives2
如您所见,我在 list
后附加了文件中的选项卡和项目。我希望输出到一个文件,所以在命令行上,我这样做:
comm.py
是程序名,test
,test2
是测试输入。
$python comm.py test test2 >commOut
在 emacs 中打开输出文件,结果如下所示:
'\t', '\t', '\t', 'aword\n', 'anotherword\n', ...
每个 list
项目都用单引号括起来,并且所有隐藏的字符都显示在 emacs 上,即使在函数从中获取单词的原始列表中,换行符被隐藏
如何使换行符和制表符显示为正确的隐藏字符?
这是因为当您打印列表时,它会打印所有项目的 repr。这样你就不会,比如说,混淆列表 [1, '1']
和 ['1', 1]
(其中 1
是一个 int 而 '1'
是一个 str)
要解决此问题,如果所有项目都是字符串,请打印由 ', '
、
连接的它们
print ', '.join(generator.xclusives2)
或者,如果您还想在字符串周围使用引号,
print "'" + "', '".join(generator.xclusives2) + "'"
注意str(string) == string
。 (str('Hello world!') == 'Hello world!'
) 和 repr(string) != string
(repr('Hello world!') == "'Hello world!'"
)
我编写了一个 python 函数来生成 list
个单词。首先,它读取一个由换行符分隔的单词组成的文件。根据单词的不同,它要么将其插入到 list
中,要么插入一个空白 space,由制表符表示。这是代码的相关部分:
xclusives1
、xclusives2
、dups
,都是list
s.
generator
是包含该函数的 class 的实例。
def xfile1(self):
for item1 in self.lines1:
for item2 in self.lines2:
if item1 == item2:
self.xclusives1.append("\t")
self.xclusives2.append("\t")
self.dups.append(item1)
break
self.xclusives1.append(item1)
self.xclusives2.append("\t")
self.dups.append("\t")
...
...
...
...
print generator.xclusives2
如您所见,我在 list
后附加了文件中的选项卡和项目。我希望输出到一个文件,所以在命令行上,我这样做:
comm.py
是程序名,test
,test2
是测试输入。
$python comm.py test test2 >commOut
在 emacs 中打开输出文件,结果如下所示:
'\t', '\t', '\t', 'aword\n', 'anotherword\n', ...
每个 list
项目都用单引号括起来,并且所有隐藏的字符都显示在 emacs 上,即使在函数从中获取单词的原始列表中,换行符被隐藏
如何使换行符和制表符显示为正确的隐藏字符?
这是因为当您打印列表时,它会打印所有项目的 repr。这样你就不会,比如说,混淆列表 [1, '1']
和 ['1', 1]
(其中 1
是一个 int 而 '1'
是一个 str)
要解决此问题,如果所有项目都是字符串,请打印由 ', '
、
print ', '.join(generator.xclusives2)
或者,如果您还想在字符串周围使用引号,
print "'" + "', '".join(generator.xclusives2) + "'"
注意str(string) == string
。 (str('Hello world!') == 'Hello world!'
) 和 repr(string) != string
(repr('Hello world!') == "'Hello world!'"
)