0

I have a txt file that I read line by line with my python code:

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

my txt file is this:

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

i would like to read this file txt and create a list array as this:

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

for each tag "Name" into txt file.

With your (Anton vBR) help i have update my code in this way:

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)

but i have this error: for row in io.StringIO(content).readlines()[1:]: # skips first row TypeError: initial_value must be unicode or None, not str

4
  • You need to check lines if they contains text Name and then act accordingly. You can use if "Name" in line language construct. Commented Aug 25, 2017 at 10:07
  • what are these numbers in your list? Commented Aug 25, 2017 at 10:14
  • You do realise that the data you have in your example file, bears no relation to the data you say you want in your list. That may cause some confusion. Commented Aug 25, 2017 at 10:15
  • I have fixed it Commented Aug 25, 2017 at 10:24

2 Answers 2

3

updated with names and convert to float

How about something like this?

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

Returns:

 [['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]]
Sign up to request clarification or add additional context in comments.

3 Comments

Yes is perfect, but i would take also the value of tag "Name"
I have updated my code with your help, but i have error posted on my original code
@APPGIS was missing a u for unicode before the string u""" ... """, my answer is updated to work with python2 and 3 now
1

python3 solutions:

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

`

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.