2014-10-08 90 views
0

我試圖將多行從文件保存到數組中。我只希望行的幾節雖然,這裏是例如幾行:閱讀新行的特定部分

SAM_2216_geotag.JPG -81.5285781 41.0382292 418.13 279.04 -0.01 6.96 
SAM_2217_geotag.JPG -81.5290933 41.0382309 418.94 279.34 -1.08 6.03 
SAM_2218_geotag.JPG -81.5296101 41.0382294 419.31 287.49 -0.01 4.79 

我想所有的第一組數字保存在自己的陣列和第二組數字。

Array1= [-81.5285781, -81.5290933, -81.5296101] 
Array2= [41.03822292, 41.0382309, 41.0382294] 

到目前爲止,我能夠將每一行都保存到一個數組中,但我無法擺脫不需要的數據。這裏是我當前陣列的一個元素:

SAM_2216_geotag.JPG\t-81.5285781\t41.0382292\t418.13\t279.04\t-0.01\t6.96\n' 

如果任何人都可以幫助我得到數組,我希望這會是一個很大的幫助。

回答

0

您需要拆分數據,以便您可以使用各個位。嘗試類似

columns = line.split() 

documentation on string.split

然後你可以把它們放到數組,只要你喜歡。例如(使用循環):

array1 = [] 
array2 = [] 
for line in lines: 
    columns = line.split() 
    array1.append(columns[1]) 
    array2.append(columns[2]) 
0

下面是使用re.search(https://docs.python.org/2/library/re.html#re.search),分裂(https://docs.python.org/2/library/stdtypes.html#str.split),郵編(https://docs.python.org/2/library/functions.html#zip)和圖(https://docs.python.org/2/library/functions.html#map

import re 
out=[] 

#Sample lines, you can just get these from file 
line1 = "SAM_2216_geotag.JPG -81.5285781 41.0382292 418.13 279.04 -0.01 6.96" 
line2 ="SAM_2217_geotag.JPG -81.5290933 41.0382309 418.94 279.34 -1.08 6.03" 
line3 = "SAM_2218_geotag.JPG -81.5296101 41.0382294 419.31 287.49 -0.01 4.79" 


#Create an array that has only the values you need. 
#You can replace the for below by using 'with open(file) as fH: and for line in fH' 
for line in (line1, line2, line3): 
    #line1 output: ['-81.5285781', '41.0382292', '418.13', '279.04', '-0.01', '6.96'] 
    out.append([x.strip() for x in line.split() if re.search('^[\d.-]+$', x.strip())]) 
#zip all the lists of out into list of tuples and then convert them to list of lists 
#Please note that if your array lengths are uneven, you will get the shortest length output 
print map (list, zip(*out)) 

輸出單程:

[['-81.5285781', '-81.5290933', '-81.5296101'], 
['41.0382292', '41.0382309', '41.0382294'], 
['418.13', '418.94', '419.31'], 
['279.04', '279.34', '287.49'], 
['-0.01', '-1.08', '-0.01'], 
['6.96', '6.03', '4.79'] 
]