0

I am trying to write a program that calculates a table using arcpy and then converts the table into a specific format for another program to read. The format I am getting is listed at the bottom, enumerated and comma seperated. I would like the format to only have the 2nd 2 fields seperated by a space e.g. 89.99 90.35 My formatting attermpts have thus far been unsuccessful, can anybody point me in the right direction? Many thanks

import arcpy,csv

table = "TABLE_OUTPUT2"
outfile = "TXT-TABLE"

fields = arcpy.ListFields(table)
field_names = [field.name for field in fields]

with open(outfile,'wb') as f:
    w = csv.writer(f)


for row in arcpy.SearchCursor(table):
    field_vals = [row.getValue(field.name) for field in fields]
    print field_vals
    w.writerow(field_vals)
del row

[1, 89.99999999446867, 90.3567070001462]

[2, 88.99999999460778, 89.83622323918551]

[3, 87.99999999448423, 89.1722770229037]

2 Answers2

1
field_vals = [[1, 89.99999999446867, 90.3567070001462],
              [2, 88.99999999460778, 89.83622323918551], 
              [3, 87.99999999448423, 89.1722770229037]]
for field in field_vals:
    _, b, c = field
    print '{:.2f} {:.2f}'.format(b, c)


90.00 90.36
89.00 89.84
88.00 89.17

Replace the variable names b and c with names that better represent their contents.

If you really want to round down the results (so you get 89.99 for the first number instead of 90.00), see this answer.

Community
  • 1
  • 1
BioGeek
  • 21,897
  • 23
  • 83
  • 145
0

I ended up using the following code which does what I asked. However I have a strange error in that, all of the text prints to the screen in the IDE but is missing 60ish values in the text file i.e the text file is being truncated - is very strange indeed..

for row in arcpy.SearchCursor(table):
    field_vals = [row.getValue(field.name) for field in fields] #index the fields
    newlist2 = [field_vals[1], field_vals[2]] # reurn the required fields for azimuth and zenith
    newlist = str(newlist2[0]) + " " + str(newlist2[1]) + "\n" # format for trimble sodtware

    print newlist # Print and write the info
    outfile.write(newlist)
del row