我正在尝试编写一个使用arcpy计算表的程序,然后将表转换为另一个要读取的程序的特定格式。我得到的格式列在底部,枚举和逗号分隔。我希望格式只有一个空格分隔的第二个2个字段,例如89.99 90.35我的格式化尝试到目前为止都没有成功,有人能指出我正确的方向吗?非常感谢
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]
答案 0 :(得分: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
将变量名称b
和c
替换为更能代表其内容的名称。
如果你真的想要舍入结果(所以你得到89.99
代替90.00
),请参阅this answer。
答案 1 :(得分:0)
我最终使用以下代码执行我的要求。但是我有一个奇怪的错误,所有文本都打印到IDE的屏幕上,但在文本文件中缺少60个值,即文本文件被截断 - 确实非常奇怪..
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