0

This code creates a file output.txt which contains a set of data for each line.

This is a line example:

1   1   ['PINKwA', 'GB', 'PINK', 'TUwA'][ 0.23994351  0.61419796 0.00956974  0.1362888 ]

I'd like to output my values without the brackets [ and ] the commas , to have them just separated by a comma , a tab or even a space, but I cannot find out where all these extra elements are coming from...

Basically I'd like to have this kind of output instead:

1,  1,  'PINKwA', 'GB', 'PINK', 'TUwA', 0.23994351,  0.61419796, 0.00956974,  0.1362888

or even this:

1 1 'PINKwA' 'GB' 'PINK' 'TUwA' 0.23994351 0.61419796 0.00956974 0.1362888

I am not sure this part of the code is responsible for these brackets and stuff... Do you think it can be solved there??

Thanks

import tetgen, geometry
from pprint import pprint
import random, csv
import numpy as np
from pprint import pprint

all_colors = [(name, float(X), float(Y), float(Z))
              for name, X, Y, Z in csv.reader(open('colors.csv'))]

# background is marked SUPPORT
support_i = [i for i, color in enumerate(all_colors) if color[0] == 'SUPPORT']
if len(support_i)>0:
    support = np.array(all_colors[support_i[0]][1:])
    del all_colors[support_i[0]]
else:
    support = None

tg, hull_i = geometry.tetgen_of_hull([(X,Y,Z) for name, X, Y, Z in all_colors])
colors = [all_colors[i] for i in hull_i]

print ("thrown out: "
       + ", ".join(set(zip(*all_colors)[0]).difference(zip(*colors)[0])))

targets = [(name, float(X), float(Y), float(Z), float(BG))
           for name, X, Y, Z, BG in csv.reader(open('targets.csv'))]

for target in targets:
    name, X, Y, Z, BG = target
    target_point = support + (np.array([X,Y,Z]) - support)/(1-BG)
    tet_i, bcoords = geometry.containing_tet(tg, target_point)

    output = open('output.txt','a')
    if tet_i == None:
        output.write(str(target[0]))
        output.write(str('\r\n'))
        output.close()

    else:
        names = [colors[i][0] for i in tg.tets[tet_i]]
        output.write(str(target[0]))
        output.write(str(names))
        output.write(str(bcoords))
        output.write(str('\r\n'))
        output.close()
4

1 回答 1

1

output.write(str(bcoords[0]) + ", " + str(bcoords[1])

也许还有其他更pythonic的东西?

于 2013-07-22T16:09:27.987 回答