-4

I have tuples( one in each line) in a text file names as listCls.txt . They look like this

(10.0, 0.0, 0.0)

(9.297883, 3.680947, 0.0)

(7.071068, 7.071068, 0.0)

I want to import this text file as a list in my python script as following.

list = [(10.00 , 0.00 , 0.00),( 9.30,3.68 , 0.00) , (7.07, 7.07, 0.00)]

I also want to round them off to two digit places. Thank you

zee
  • 17
  • 2
  • Those numbers don't match up at all. The last two tuples seem like they are rearranged but slightly different. Definitely not the same as your output. Whats happening – jamylak Jan 10 '15 at 14:25
  • @jamylak . Yes, I corrected it . I was a copying mistake. thank you for pointing it out. – zee Jan 10 '15 at 14:29
  • I still don't get how you round to `9.28` – jamylak Jan 10 '15 at 15:00

2 Answers2

1

Not gonna solve it for you, since you didn't try anything.

Guide

  1. Open the file (open('file.txt', r)), and
  2. For each line you read:
    1. remove the parens
    2. split by comma
    3. trim the pieces (hint: .strip())
    4. convert them to numbers
    5. do your rounding
    6. Compose tuple of those numbers
MightyPork
  • 18,270
  • 10
  • 79
  • 133
  • Thank you. I just started learning python 2 weeks ago. It is very fascinating. Previously I imported the tuples into a list using a loop. When I try to print the first entry of the first element e.g element[1][0] which should give me the X-coordinate, instead it only gives me the first number of the coordinate. It somehow imports the tuples as strings not numbers. – zee Jan 10 '15 at 14:25
  • If you add your code to the question, someone (perhaps me) can help fix it. I really don't know what you're doing wrong rn. – MightyPork Jan 11 '15 at 09:58
1

I'm pretty sure that there is a way to do this with existing Grasshopper components, but I felt like having a go at it just to see.

This bit generates some data that matches yours:

import random

f = open('listCls.txt','w')
for i in range(100):
    a = random.uniform(0, 10)
    b = random.uniform(0, 10)
    c = random.uniform(0, 10)
    f.write('({}, {}, {})\n\n'.format(a,b,c))
f.close()

Then this bit reads it:

f = open('listCls.txt','r')

list =[]

for line in f:
    if line[0]=="(":
        tup = eval(line)
        list.append(tup)

print list

I'm sure that someone will say that using eval is evil, but if you've generated the data and you haven't slipped in a call to startGlobalWar() somewhere in the middle then this is pretty much what eval was made for.

If you don't trust the data source (and if you didn't make it, then you probably shouldn't) then making a string discombobulator as the MightyPork has suggested.

I've left out the part about rounding. Rounding is for humans. If the data has decimal places, hang onto them until you absolutely need to. Otherwise you'll just get cumulative inacuaracy, which if you need to take this into the real world is the difference between it going together first time and having to stay up all night sanding things to make them fit.

Community
  • 1
  • 1
Ben
  • 12,614
  • 4
  • 37
  • 69