簡體   English   中英

Python:如何在文本文件中搜索用戶輸入的包含整數的字符串?

[英]Python: How can I search a text file for a string input by the user that contains an integer?

這是我正在搜索的文本文件上的內容的一個示例:

15 - Project `enter code here`Name
APP_IDENTIFIER=ie.example.example
DISPLAY_NAME=Mobile Banking
BUNDLE_VERSION=1.1.1
HEADER_COLOR=#72453h
ANDROID_VERSION_CODE=3


20 - Project Name
APP_IDENTIFIER=ie.exampleTwo.exampleTwp
DISPLAY_NAME=More Mobile Banking
BUNDLE_VERSION=1.2.3
HEADER_COLOR=#23456g
ANDROID_VERSION_CODE=6

例如,如果用戶輸入15,我希望python復制以下信息:

ie.example.example
Mobile Banking
1.1.1
#72453h
3

因為我需要將其復制到其他文本文件中。

我讓用戶輸入項目編號(在此示例中,項目編號為15和20),然后我需要程序來復制與用戶輸入的編號相關的項目的app_identifier,display_name,bundle_version和android_version。

如何獲得python在文本文件中搜索用戶輸入的數字並僅從該特定項目正下方的行中獲取所需信息?

我已經編寫了整個程序,但這只是其中的一部分。 我實際上還沒有任何代碼可以查找和復制所需的特定信息。 這是我必須搜索項目ID的代碼

while True: 
    CUID = int(input("\nPlease choose an option:\n"))
    if (CUID) == 0:
        print ("Project one")
        break
    elif (CUID) == 15:
        print ("Project two")
        break
    elif (CUID) == 89:
        print ("Project three")
        break
    else:
        print ("Incorrect input")

感謝Conor的解決方案:

projectFile = open("C:/mobileBuildSettings.txt" , "r")
for line in projectFile:
    CUID = str(CUID)
    if CUID + " - " in line:
            appIdentifier = next(projectFile).split("=")[1]
            displayName = next(projectFile).split("=")[1]
            bundleVersion = next(projectFile).split("=")[1]
            next(projectFile)
            androidVersionCode = next(projectFile).split("=")[1]

            print (appIdentifier, displayName, bundleVersion,     androidVersionCode)

            break

沒有理由在if..else長列表中列出所有單個數字。 您可以使用正則表達式檢查行是否以任何數字開頭。 如果匹配,請檢查它是否與您要查找的數字匹配,如果不匹配,請跳過以下幾行,直到到達空白行分隔符。

有了要查找的數據后,就可以再次使用正則表達式查找= ,或者簡單地使用.find

import re
numberToLookFor = '18'
with open("project.txt") as file:
    while True:
        line = file.readline()
        if not line:
            break
        line = line.rstrip('\r\n')
        if re.match('^'+numberToLookFor+r'\b', line):
            while line and line != '':
                if line.find('='):
                    print line[line.find('=')+1:]
                line = file.readline().rstrip('\r\n')
        else:
            while line and line != '':
                line = file.readline().rstrip('\r\n')

干得好:

while True: 
    CUID = int(input("\nPlease choose an option:\n"))
    if (CUID) == 0:
       appid = value.split("APP_IDENTIFIER=")[1] # get the value after "APP_IDENTIFIER="
       print appid
       output >>> ie.example.example

您可以對所有值應用相同的代碼,只需在“ =”之前更改標題。

從文本獲取整行,然后僅使用此代碼獲取“ =”之后的值以輸出結果。

projectfile = open("projects", "r")
    for line in projectfile:
        if CUID in line:
            appIdentifier = next(projectfile).split("=")[1]
            displayName = next(projectfile).split("=")[1]
            bundleVersion = next(projectfile).split("=")[1]
            next(projectfile)
            androidVersionCode = next(projectfile).split("=")[1]

            # Do whatever with the 4 values here, call function etc.

            break

然后使用appIdentifier,displayName,bundleVersion和androidVersionCode進行操作,它們將僅返回'='之后的值。

盡管我建議不要一般性地搜索整數,但是如果整數也位於捆綁軟件或Android版本中怎么辦?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM