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

时间:2018-10-02 22:15:23

标签: python arrays file readfile texas-instruments

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

lines = f.readlines()

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

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

我想创建一个行数组,例如:

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

我该怎么办?

3 个答案:

答案 0 :(得分:1)

#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]))

答案 1 :(得分:0)

尝试一下:

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

答案 2 :(得分:0)

以下应该有帮助:

# 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(','))

它提供了一个整数列表,因为数据是数字。谢谢。