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]

3
  • what is the result in your file so far ? Commented Dec 4, 2013 at 18:07
  • w is more for coma separated values, and also it only exists in the open block Commented Dec 4, 2013 at 18:07
  • see below for my eventual solution. Thanks for the help Commented Dec 4, 2013 at 19:43

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Truncation was due to file not being properly closed. fixed by using outfile.close()

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.