python中line.split_Python-line.split()

从你的文章来看,我认为每一组3个数字并不总是在不同的行上。你要寻找的每一套都是从用户想要的东西开始(比如10)。

浏览你的代码。。。levelChart = open("RunescapeLevelsChart.txt", "r")

actualLevel = raw_input("Level : ")

到目前为止还不错。if actualLevel in open("RunescapeLevelsChart.txt").read() :

此时,actualLevel是您的输入(例如'10')

open(“RunescapeLevelsChart.txt”).read()将整个文本文件存储在内存中。

所以你要从整个文件中搜索“10”。从你的例子来看,它将被评估为“真的”actualLevelSplit = actualLevel.split()

print actualLevelSplit

split()用空格分隔字符串。所以在这里,你把“10”分成[“10”](一个列表)else:

print("Failed.")

raw_input("End")raw_input将等待用户输入,然后再尝试继续,我假设您尝试在此“暂停”。你所拥有的应该有用。

既然这么说了。。这会让你得到你想要的。。levelChart = open("RunescapeLevelsChart.txt", "r")

actualLevel = raw_input("Level : ")

for line in levelchart: # Read the file line-by-line.

number_sets = line.split()

for set in number_sets:

if set.startswith(actualLevel + '-'):

print set

#>>> "10-50-100"

# Now you can further split each number into individual numbers

nums = set.split('-')

print nums

#>>> ['10', '50', '100']

# At this point, you can fetch the numbers from the list

levelChart.close() # Dont' forget to close the file object when you're done.

希望这有帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值