Python 从文件打印到控制台和其他文件

Python 从文件打印到控制台和其他文件,python,spaces,Python,Spaces,我正在从文件中读这几行 yara_rule1: rule match: problem: yara_rule2: rule match: problem: 当我将其打印到控制台时,“规则匹配”和“问题”之前的空格被省略。 有什么问题 input_data = open(file) for line in input_data: print line.strip() 删除字符串结尾和开头的所有空白。换句话说,是line.strip()方法调用生成了一行没有初始

我正在从文件中读这几行

yara_rule1:
   rule match:
   problem:

yara_rule2:
   rule match:
   problem:
当我将其打印到控制台时,“规则匹配”和“问题”之前的空格被省略。 有什么问题

input_data = open(file)
for line in input_data:
    print line.strip()
删除字符串结尾和开头的所有空白。换句话说,是
line.strip()
方法调用生成了一行没有初始空格的行

如果只想删除换行符,请使用
str.rstrip()

比较:

>>> '   rule match:\n'.strip()
'rule match:'
>>> '   rule match:\n'.rstrip('\n')
'   rule match:'

请将控制台的输出复制/粘贴到您的问题中。另外,删除周围的空白正是
strip()
所做的-事实上,这是我能想象你会使用它的唯一原因。哇。您正在
strip()
>>> '   rule match:\n'.strip()
'rule match:'
>>> '   rule match:\n'.rstrip('\n')
'   rule match:'