Python无法在文件中找到字符串

时间:2017-06-29 18:42:31

标签: python-2.7

所以我正在编写一个程序来从卸载注册表中查找转储中的特定行,然后将这些行写入新的文本文件。这是代码。

fileName = "export.txt"
outputFileName = input("Enter the Output File Name")
inputFile = open(fileName, "r")
outputFile = open(outputFileName, "w")
displayName = ""
displayVersion = ""
publisher = ""

for line in inputFile:
    if "DisplayName" in line:
        lst = line.split("=")
        displayName = lst[1][1:len(lst[1])-1]

    if "DisplayVersion" in line:
        lst = line.split("=")
        displayVersion = lst[1][1:len(lst[1])-1]
    if "Publisher" in line:
        lst = line.split("=")
        publisher = lst[1][1:len(lst[1])-1]
    if displayName!= "" or displayVersion != "" or publisher != "":
        outputFile.write(displayName + "\t" + displayVersion + "\t" +publisher + "\n")
        displayName = ""
        displayVersion = ""
        publisher = ""

inputFile.close()
outputFile.close()

由于某种原因,没有输入前三个if语句。以下是export.txt文本文件的片段。

[HKEY_LOCAL_MACHINE\SoftWare\Microsoft\Windows\CurrentVersion\Uninstall\Matlab R2016b]
"DisplayName"="MATLAB R2016b"
"UninstallString"="C:\\Program Files\\MATLAB\\R2016b\\uninstall\\bin\\win64\\uninstall.exe C:\\Program Files\\MATLAB\\R2016b"
"DisplayIcon"="C:\\Program Files\\MATLAB\\R2016b\\bin\\win64\\matlab.ico"
"InstallLocation"="C:\\Program Files\\MATLAB\\R2016b"
"DisplayVersion"="9.1"
"URLInfoAbout"="www.mathworks.com"
"Publisher"="MathWorks"
"HelpLink"="www.mathworks.com/support"
"Comments"=" "    

ê[HKEY_LOCAL_MACHINE\SoftWare\Microsoft\Windows\CurrentVersion\Uninstall\Matlab
R2016b]
“DisplayName””MATLAB R201 6b”
“UninstallString””C: \\Prograrn
Files\\MATLAB\\R2016b\\uninstall\\bin\\win64\\uninstall.exe C: \\Prograrn
Files\\frIATLAB\\R201 6b”
“Displaylcon””C:\\Prograrn Files\\MATLAB\\R2016b\\bin\\win64\\matlab. ico”
“InstallLocation””C: \\Prograrn Files\\MATLAB\\R201 6b”
“DisplayVersion””9. 1”
“URLlnfoAbout””www. mathworks. corn”
“Publisher”=”MathWorks”
“HelpLink””www. rnathworks. corn/support”
“Comments”” “

2 个答案:

答案 0 :(得分:0)

使用find()以不同方式检查怎么办?

if line.find("DisplayName") != -1:
    do stuff.

我跑得很好,这里是代码:

fileName = "export.txt"
outputFileName = input("Enter the Output File Name")
inputFile = open(fileName, "r")
outputFile = open(outputFileName, "w")
displayName = ""
displayVersion = ""
publisher = ""

for line in inputFile:
    print line
    if line.find("DisplayName") != -1:
        lst = line.split("=")
        displayName = lst[1][1:len(lst[1])-2]

    if line.find("DisplayVersion") != -1:
        print "here2"
        lst = line.split("=")
        displayVersion = lst[1][1:len(lst[1])-2]
    if line.find("Publisher") != -1:
        print "here3"
        lst = line.split("=")
        publisher = lst[1][1:len(lst[1])-2]
    if displayName!= "" and displayVersion != "" and publisher != "":
        print "Here4"
        print displayName + "\t" + displayVersion + "\t" +publisher
        outputFile.write(displayName + "\t" + displayVersion + "\t" +publisher)
        displayName = ""
        displayVersion = ""
        publisher = ""

inputFile.close()
outputFile.close()

生产:     MATLAB R2016b 9.1 MathWorks

运行脚本时的输出如下所示:

输入输出文件名" out.txt" [HKEY_LOCAL_MACHINE \ SoftWare \ Microsoft \ Windows \ CurrentVersion \ Uninstall \ Matlab R2016b]

" DisplayName" =" MATLAB R2016b"

" UninstallString" =" C:\ Program Files \ MATLAB \ R2016b \ uninstall \ bin \ win64 \ uninstall.exe C:\ Program Files \ MATLAB \ R2016b"

" DisplayIcon" =" C:\ Program Files \ MATLAB \ R2016b \ bin \ win64 \ matlab.ico"

" InstallLocation" =" C:\ Program Files \ MATLAB \ R2016b"

" DisplayVersion" =" 9.1"

here2 " URLInfoAbout" =" www.mathworks.com"

"出版者" =" The MathWorks公司"

here3 Here4 MATLAB R2016b 9.1 MathWorks " HELPLINK" =" www.mathworks.com/support"

"注释" =" "

答案 1 :(得分:0)

你最后一句if语句的逻辑完全相反。它应该是或代替并且为了使您的陈述正常工作。