我有一个问题,希望你能帮我解决!我有一个看起来像的文本:
A100 1960
3 5
6 7
8 9
10 11
12 13
A200 1960
3 5
6 7
8 9
10 11
12 13
14 15
A300 1960
3 5
6 7
8 9
10 11
12 13
14 15
16 17
我想搜索示例A200 1960
的关键字。现在,我想在A200 1960
中保存A300 1960
和array(x, y)
之间的所有数字。
我设法跳到了我想要的路线。现在的问题是写入数组直到行A300 1960
。此外,数组的长度也可以变化。
def readnumbers(filename,number,year):
number_year = np.asarray([])
f = open(filename)
lines = f.readlines()
f.close()
strings_number = 'A'+ str(number)
strings_year = ' ' + str(year)
STARTER = strings_number+ '' + strings_year
start_seen = False
for line in lines:
if line.strip() == STARTER:
start_seen = True
continue
if start_seen == True:
parts = line.split()
答案 0 :(得分:3)
在设置list
后,使用标准start_seen
存储每一行。并检查每一行是否为“STOPPER”行,以便您可以停止迭代文件。
此外:
with
语句打开文件,因此您无需手动关闭它。readlines()
,而是直接迭代文件句柄。if variable == True:
,只需检查if variable:
def readnumbers(filename,number,year):
strings_number = 'A'+ str(number)
strings_year = ' ' + str(year)
STARTER = strings_number + '' + strings_year
result = []
with open(filename) as f:
start_seen = False
# iterate the file handle
for line in f:
# only check the line for STARTER if you haven’t seen it yet
if not start_seen and line.strip() == STARTER:
start_seen = True
# otherwise, check if it’s a stopper which starts with `A`
elif start_seen and line.startswith('A'):
break
# otherwise, if we’ve seen the start line, we need to capture the result
elif start_seen:
parts = line.split()
# append to the result list
result.append(parts)
# finally, return the result
return result
答案 1 :(得分:2)
filename = 'test.txt'
def fill_array():
f = open(filename)
lines = f.readlines()
f.close()
lines = [x.strip() for x in lines]
search_term = 'A200 1960'
read = False
array = []
for index in xrange(0,len(lines)):
if lines[index] == search_term:
read = True
elif read:
data = [x.strip() for x in lines[index].split(' ') if x]
if str(data[0]).isdigit():
array.append(data)
else:
read = False
print array
数组将包含您要保存的数字
输出
[['3', '5'], ['6', '7'], ['8', '9'], ['10', '11'], ['12', '13'], ['14', '15']]
答案 2 :(得分:0)
试试这个:
import sys
start = sys.argv[2] # 'A200 1960'
end = sys.argv[3] # 'A300 1960'
coords = [] # collected (x, y) tuples
start_seen = False # Have we seen start yet?
end_seen = False # Have we seen end yet?
with open(sys.argv[1]) as f:
for row in f:
if row.startswith(start):
start_seen = True
continue
if row.startswith(end):
end_seen = True
continue
if start_seen and not end_seen:
# Split the line and convert each part into an int.
x, y = [int(s) for s in row.split()]
coords.append((x, y))
print(coords)
用法:
$ python3.4 so.py data.csv 'A200 1960' 'A300 1960'
输出:
[(3, 5), (6, 7), (8, 9), (10, 11), (12, 13), (14, 15)]