Python 无法从文件行提取文件扩展名
当我运行下面的程序时,我没有得到预期的输出Python 无法从文件行提取文件扩展名,python,python-2.7,Python,Python 2.7,当我运行下面的程序时,我没有得到预期的输出 import os import re f = open('outputFile','w') #flag set to 1 when we are processing the required diffs diff_flag=0 #Initialized to 0 in beginning #with open('Diff_output.txt') as fp: with open('testFile') as fp: for l
import os
import re
f = open('outputFile','w')
#flag set to 1 when we are processing the required diffs
diff_flag=0 #Initialized to 0 in beginning
#with open('Diff_output.txt') as fp:
with open('testFile') as fp:
for line in fp:
if re.match('diff --git',line):
#fileExtension = os.path.splitext(line)[1]
words=line.split(".")
diff_flag=0
# print fileExtension
str=".rtf"
print words[-1]
if words[-1] != "rtf":
print "Not a text file.."
diff_flag = 1
f.write(line)
print "writing -> " + line
elif diff_flag == 1:
f.write(line)
else:
continue
我得到如下输出:
python read.py
rtf
Not a text file..
writing -> diff --git a/archived-output/NEW/action-core[best].rtf b/archived-output/NEW/action-core[best].rtf
它是一个文本文件,if条件的计算结果应为false。当我打印words[-1]或fileExtension时,我得到了正确的扩展名。但我无法理解为什么这种情况会失败。这两个变量的内容是否有问题,因为条件的计算结果为真(不等于)。我正在尝试逐行读取文件并提取文件名的扩展名。当您像这样迭代文件时,这些行将包含换行符“\n”,您应该执行以下操作之一:
words = line.strip().split(".").
或
但如果我是你,我会做的是:
if line.strip().endswith(".rtf"):
而不是分割线
顺便说一句,换行符的证明是您的输出:
rtf
<-- empty line here.
rtf
2点:
一,。尝试从行的开头匹配模式如果要在字符串中的任何位置找到匹配项,请改用。(另见)
2.words=line.split(“.”
不会提供单词列表,因为它会在文件的尾部或前导处包含空格,如\n
,您需要首先删除行:
words=line.strip().split(".")
words=line.strip().split(".")