从两个元素之间的另一个数组创建一个数组

Creating an array out of another array between 2 elements

我正在使用 readlines() python 读取文件

lines = f.readlines()

如何添加出现在 2 个特定字符之间的行中的所有组件,例如:

lines = [rose, 1 , 2 , 4 , 5, 6], garden, plants ]

我想创建一个数组,这样:

array = [1,2,3,4,5,6]

我该怎么做?

试试这个:

with open('Path/to/file', 'r') as f:
    content = f.readlines()
    data = content[8][7:].split(",")

以下内容应该有所帮助:

# Open File
with open('../input_file.txt') as f:
    lines = f.readlines()

# Find the required attribute
for line in lines:
    if line[:4] == 'data':
        data = line.split(':')[1].strip()
        break

# Split the content to make a list of INTEGERS
python_list = map(lambda x : int(x.strip()),data[1:-1].split(','))

它提供了一个整数列表,因为数据是 numerical.Thanks。

#Read File
file = open("testFile.txt", "r")
f_str=file.read()
# Find positions of  [] in String
begin_pos= f_str.find('[')+1
end_pos= f_str.find(']')
# Get Subset of String and Split it by ',' in a Str List
f_str=f_str[begin_pos:end_pos].split(',')
#Str List to Int List
plist=list(map(int, f_str))
#Test it
print(plist)
print(type(plist[1]))