python - 我如何抓取输入字符串的某些部分?

标签 python jython

主要是,我试图让用户输入一些数据

def getmonsterData():
  monster1 = raw_input("Enter the Monster Affinity|HP|AD> ")
  x = monster1.index('|');
  affinityType = monster1[0:x]
  printNow(affinitytype)

我能够创建一个变量来存储怪物亲和性,因此在打印时它会列出用户输入的内容(即:火/土),但我不确定如何为 HP 和 AD 获取和创建类似的变量

这将打印我列出的 Monster1 的亲和性,例如火。这一切都很好, 但是当我不太确定如何使用 Jython/Python 拼接字符串时..可能像

HP = monster1[affinityType+1:x] ? not too sure how to do this

类似地,我需要获取最后一个变量 AD,我相信如果我获取 HP 变量我可以做到这一点

 y = len(monster1)
 AD = monster1[x+1:y]

对此的任何输入都会有所帮助 :) 谢谢

最佳答案

你可以使用拆分:

>>> aff,HP,AD = raw_input("Enter the Monster Affinity|HP|AD> ").split('|')
Enter the Monster Affinity|HP|AD> a|11|22
>>> aff
'a'
>>> HP
'11'
>>> AD
'22'

但是如果你想使用索引,你可以使用start参数(S.index(sub [,start [,end]]) -> int)

>>> monster =  raw_input("Enter the Monster Affinity|HP|AD> ")
Enter the Monster Affinity|HP|AD> a|11|22
>>> x = monster.index('|')
>>> monster[:x]
'a'
>>> y = monster.index('|',x+1)
>>> monster[x+1:y]
'11'
>>> monster[y+1:]
'22'

关于python - 我如何抓取输入字符串的某些部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27043031/

相关文章:

python - 嵌套标签网络抓取python

python - python中的行结尾

python - Django 1.7 - 带有自定义应用程序标签的模型发现和应用程序配置

python - 可用的最先进的纯 Python XML 解析器是什么?

Java 类方法不会在使用 Eclipse Pydev 的 Jython 中自动完成

java - Jython 独立 jar 和包

python - openpyxl 如何将单元格设置为 'ignore error'?

python - Mac OS High Sierra : Tensorflow verions returned by `pip3 upgrade ` and `python3 -c ' import tensorflow as tf; print(tf. __version__ )'` 不同

java - 使用 jython 或 groovy 中的 htsjdk 定义的类

spring-boot - 使用 Spring Boot 打包时找不到 Jython 模块?