Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python2.7无法索引行,而是获取字母_Python_Python 2.7_Indexing - Fatal编程技术网

Python2.7无法索引行,而是获取字母

Python2.7无法索引行,而是获取字母,python,python-2.7,indexing,Python,Python 2.7,Indexing,当我这样做的时候 打印匹配的\u行 我明白了 dhcp 1已启用 hsrp_发动机1已启用 接口vlan 1已启用 已启用lacp 1 ntp 1已启用 scpServer 1已启用 已启用sshServer 1 vpc 1已启用 但是如果我尝试索引一行 打印匹配的\u行[0] 我明白了 d 但我期待着第一次见面 dhcp 1已启用 请让我知道我必须做些什么来修复此问题。根据我的评论: 匹配的\u行='\n'。连接(匹配的\u行)这将从列表中生成一个字符串。不要这样做,你可以得到单独的行。你如何

当我这样做的时候
打印匹配的\u行
我明白了

dhcp 1已启用
hsrp_发动机1已启用
接口vlan 1已启用
已启用lacp 1
ntp 1已启用
scpServer 1已启用
已启用sshServer 1
vpc 1已启用

但是如果我尝试索引一行

打印匹配的\u行[0]

我明白了

d

但我期待着第一次见面

dhcp 1已启用

请让我知道我必须做些什么来修复此问题。

根据我的评论:


匹配的\u行='\n'。连接(匹配的\u行)这将从列表中生成一个字符串。不要这样做,你可以得到单独的行。

你如何生成
匹配的行
?是
匹配的行
列表还是元组?你只有一行,如果你在上面使用
[0]
,它将返回第一个字符。@marcin我已经更新了问题,以显示我是如何得到匹配的行的。
匹配的行='\n'。加入(匹配的_行)
这将从列表中生成一个字符串。不要这样做,您可以得到单独的行。
fromStr = 'start of show feature'
toString = 'end of show feature'

with open(filepath) as myFile:
    for num, line in enumerate(myFile, 1):
        if fromStr in line:
            fromline = num

with open(filepath) as myFile:
    for num, line in enumerate(myFile, 1):
        if toString in line:
            toline = num

fromline = fromline+1

f = open(filepath)
lines=f.readlines()
store =  lines[fromline:toline-1]
store1 = '\n'.join(store)
text = "\n".join([ll.rstrip() for ll in store1.splitlines() if ll.strip()])  # remove blank lines

string = 'enabled'

matched_lines = [line for line in text.split('\n') if string in line] #get matched lines
#matched_lines = list(set(matched_lines))  #get unique items only or remove duplicates (the result will be unordered for many items)
matched_lines = '\n'.join(matched_lines) #rearrange the lines in order