2017-08-25 76 views
0

我有我通過符合我的Python代碼讀取行的TXT文件:的Python閱讀txt文件,並創建數組列表

a = open("data.txt","r") 
inputF = a.readlines() 
for line in inputF: 
    print(line) 

我的txt文件是這樣的:

Data: 7_8_2014 
Name: Road 
488597.653655, 4134910.76248 
488813.848952, 4134609.01192 
488904.303214, 4134480.54842 
488938.462756, 4134422.3471 
Name: Street 
496198.193041, 4134565.19994 
496312.413827, 4134568.14182 
496433.652036, 4134568.08923 
496559.933558, 4134547.91561 
496782.196397, 4134527.70636 
496923.636101, 4134512.56252 

我想讀取此文件txt並創建一個列表數組,如下所示:

coordsList = [[Road, 488597.653655, 4134910.76248], 
      [Road, 488813.848952, 4134609.01192], 
      [Road, 488904.303214, 4134480.54842], 
      [Road, 488938.462756, 4134422.3471], 
      [Street, 496198.193041, 4134565.19994], 
      [Street, 496312.413827, 4134568.14182], 
      [Street, 496433.652036, 4134568.08923], 
      [Street, 496559.933558, 4134547.91561], 
      [Street, 496782.196397, 4134527.70636], 
      [Street, 496923.636101, 4134512.56252]] 

爲每個標籤「Name」轉換爲txt文件。

與您(安東VBR)的幫助,我有這樣更新我的代碼:

import arcpy 
import os, sys 
import io 

with open('C:/Users/fdivito/Desktop/FinalProjectData/7_8_2014.txt', 'r') as content_file: 
content = content_file.read() 
i=0 
output = [] 


for row in io.StringIO(content).readlines()[1:]: # skips first row 
if row.startswith("Name"): 
    #i = row.split(":")[1].strip() 
    i+=1 
else: 
    output.append([i]+[float(item.strip()) for item in row.split(",")]) 

print(output) 

,但我有此錯誤: 爲行io.StringIO(內容).readlines()[1: ]:#跳過第一行 類型錯誤:initial_value必須是Unicode或無,不str的

+0

你需要檢查線路是否包含文本名稱,然後採取相應的行動。您可以使用行語言結構中的「名稱」。 – Jiri

+0

列表中的這些數字是什麼? – pooya

+0

您確實知道您示例文件中的數據與您在列表中要求的數據無關。這可能會導致一些混淆。 –

回答

3

與名稱更新並轉換爲浮動

怎麼這樣呢?

import io 

string = u"""Data: 7_8_2014 
Name: Road 
488597.653655, 4134910.76248 
488813.848952, 4134609.01192 
488904.303214, 4134480.54842 
488938.462756, 4134422.3471 
Name: Street 
496198.193041, 4134565.19994 
496312.413827, 4134568.14182 
496433.652036, 4134568.08923 
496559.933558, 4134547.91561 
496782.196397, 4134527.70636 
496923.636101, 4134512.56252""" 

output = [] 

#with open("pathtofile.txt") as file: 
# for row in file.readlines()[1:] 
    #code here 

for row in io.StringIO(string).readlines()[1:]: # skips first row 
    if row.startswith("Name"): 
     i = row.split(":")[1].strip() 
    else: 
     output.append([i]+[float(item.strip()) for item in row.split(",")]) 

output 

返回:

[['Road', 488597.653655, 4134910.76248], 
['Road', 488813.848952, 4134609.01192], 
['Road', 488904.303214, 4134480.54842], 
['Road', 488938.462756, 4134422.3471], 
['Street', 496198.193041, 4134565.19994], 
['Street', 496312.413827, 4134568.14182], 
['Street', 496433.652036, 4134568.08923], 
['Street', 496559.933558, 4134547.91561], 
['Street', 496782.196397, 4134527.70636], 
['Street', 496923.636101, 4134512.56252]] 
+0

是完美的,但我也會拿標籤「Name」的價值 – APPGIS

+0

@APPGIS更新。 –

+0

我用你的幫助更新了我的代碼,但是我的錯誤發佈在我原來的代碼 – APPGIS

1

python3解決方案:

`result_list = [] 
with open(your_file, 'r') as file_: 
    file_.readline() # skip the header, apparently you don't want it. 

    for line in file_: 
     if line.startswith('Name'): 
      current_tag = line.strip().split()[-1] # assume that the tag as no space 
      # else use split(':')[-1].strip() 
      continue 
     result_list.append([current_tag] + line.strip().split(',')) 

`