2016-06-22 72 views
0

这是什么是对,我搜索文本文件的例子:Python:如何搜索文本文件以查找包含整数的用户输入的字符串?

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个用户的类型,我想蟒蛇复制下列信息:

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

,因为我需要将其复制到不同的文本文件中。

我让用户输入一个项目编号(在这个例子中,项目编号是15 & 20),然后我需要程序复制项目的app_identifier,display_name,bundle_version和android_version,用户输入。

如何让python在文本文件中搜索用户输入的数字,并只从直接在该特定项目下面的行中获取所需信息?

我写了一个完整的程序,但这只是其中的一部分。 我真的没有任何代码可以找到并复制我需要的特定信息。 这里是代码,我必须

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") 

该解决方案的感谢搜索项目的ID,以康纳:

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 
+0

@cdarke这就是我对用户输入: #User输入选择了CUDID 而真: CUID = INT(输入( “\ n请选择一个选项:\ n”)) 如果(CUID )== 0: 打印( “我的CU”) 破 ELIF(CUID)== 15: 打印( “你CU”) 破 ELIF(CUID)== 89: 打印( “他们的CU” ) break else: print(“Incorrect input”) 我是新来的stackoverflow,我不知道如何添加代码,以正确评论 – Jill

+0

你的代码会更适合作为主要问题的补充。点击问题底部的“编辑”链接,然后添加和编辑您的代码。 –

+0

你想得到什么信息?我的意思是需要什么信息?如果你想获得价值,那么有一个简单的解决方案。但是你的代码除了比较项目号外什么都不做。 – hzleonardo

回答

1
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,显示名,bundleVersion & androidVersionCode你会,他们将在 '=' 后只返回值。

虽然我会建议不要一般搜索一个整数,如果整数也是在捆绑或Android版本?

+0

非常感谢Conor,你是一个伟大的帮助!我已经添加到我的主要questoin你的代码,我改变了一下做我所需要的。所以很高兴谢谢你:D – Jill

+0

我添加了下面的代码来获取python来找到我需要的int,而不是一些随机的,可能在捆绑包或android版本!希望它能完成这项工作,我会在一分钟内对它进行测试。 如果CUID +“ - ”符合: – Jill

1

没有理由列出所有个体数量在相当长的if..else名单。您可以使用regular expression来检查一行是否以任何数字开头。如果是,请检查它是否与您正在查找的号码匹配,如果不匹配,请跳过以下几行,直到您到达空白行分隔符。

只要你有你所寻找的,你可以再次使用正则表达式来定位=,或者干脆使用.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') 
+0

非常感谢您的帮助! – Jill

1

在这里你去:

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 

您可以对所有值应用相同的代码,只需在“=”之前更改标题即可。

从文本中获取整行,然后使用此代码获取结果输出后的值为“=”。

+0

非常感谢您的帮助! – Jill

相关问题