Jill
Jill

Reputation: 441

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

This is an example of what is on the text file that I am searching:

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

If, for example, the user types in 15, I want python to copy the following info:

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

because I need to copy it into a different text file.

I get the user to input a project number (in this example the project numbers are 15 & 20) and then I need the program to copy the app_identifier, display_name, bundle_version and android_version of the project relating to the number that the user input.

How do I get python to search the text file for the number input by the user and only take the needed information from the lines directly below that specific project?

I have a whole program written but this is just one section of it. I don't really have any code yet to find and copy the specific information I need. Here is code i have to search for the project 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")

The solution thanks to 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

Upvotes: 0

Views: 868

Answers (3)

Conor
Conor

Reputation: 36

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

Then do with appIdentifier, displayName, bundleVersion & androidVersionCode what you will, they will return just the values after the '='.

Although I would recommend against generically searching for an integer, what if the integer is also in the bundle or android version?

Upvotes: 1

hzleonardo
hzleonardo

Reputation: 483

Here you go:

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

You can apply the same code for all values there, just change the title before "=".

Get the whole line from text then get only the value after "=" with this code for result output.

Upvotes: 1

Jongware
Jongware

Reputation: 22457

There is no reason to list all individual numbers in a long if..else list. You can use a regular expression to check if a line starts with any digit. If it does, check if it matches the number you are looking for, and if it does not, skip the following lines until you reach your blank line separator.

As soon as you have the data you are looking for, you can use a regular expression again to locate the =, or simply use .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')

Upvotes: 1

Related Questions