使用python将字符串和浮点数写入ASCII文件

时间:2018-12-18 14:07:21

标签: python list numpy arcgis arcpy

我一直试图连接两个数组(字符串数组和浮点数组),然后将最终数组写入CSV文件。但是,我一直面临很多问题来解决它。我已经在互联网上尝试了很多有关此主题的教程,但是没有人适合我。

我正在编写的代码使用python自动执行ArcGIS中的一系列任务。代码运行并获得所有平均值后,我需要将栅格文件中的平均值与两个字符串连接在一起。

我已经尝试将我需要的所有信息合并到一个数组中,但是当我尝试将其保存到CSV文件时会遇到问题。

代码如下。

socketConnection

我从此代码中获得的错误消息如下

  

运行时错误   追溯(最近一次通话):     文件“”,第50行,在     保存为txt的文件“ C:\ Python27 \ ArcGIS10.3 \ lib \ site-packages \ numpy \ lib \ npyio.py”,行1031       引发错误   ValueError:fmt的%格式数量错误:%10s%10.3f

由于我进行了许多尝试,因此代码中有很多注释。

我想要得到的是一个具有3列和许多行的CSV文件,行数将根据要插值的数据量而变化。一个例子如下:

#Interpolation
        arcpy.gp.Idw_sa(out_Layer_shp, i_dia, arcpy.env.workspace + "\\Raster\\" + out_Layer_tif + ".tif", tamanho_celula, potencia_idw, "VARIABLE 12 12", "") 


        #Deleting the shape file
        arcpy.Delete_management(out_Layer_shp, "") 



        lista_final = np.array([], dtype = object)  #Final list
        contador = 0 #Counter 


        for mascara in bacias: #Importing shapefiles to clip the interpolated raster
            importar_camada = arcpy.env.workspace + "\\Shapefile\\Bacias\\" + mascara + ".shp"  
            arcpy.MakeFeatureLayer_management(importar_camada, mascara)  



            #Variable to set the name of the layers that will be created and then clipped
            camada_para_recortar = out_Layer_tif + ".tif"
            camada_resultante = out_Layer + "_recortada"
            nome_do_raster = camada_resultante + "_" + mascara + ".tif" 

            #Code to interpolate
            arcpy.Clip_management(camada_para_recortar, "", arcpy.env.workspace  + "\\Raster\\Recortes\\" + camada_resultante + "_" + mascara + ".tif", mascara, "-3,402823e+038", "ClippingGeometry", "NO_MAINTAIN_EXTENT")

            #Getting the mean value from the raster clipped
            media = arcpy.GetRasterProperties_management (nome_do_raster, "MEAN", "") 


            lista_strings = np.array([out_Layer, mascara]) #array string
            lista_medias = np.array([media]) #float string 

            arquivo_com_as_medias = "medias 01" #Name of the file to save the means values

            lista_numpy_temporaria = np.array([out_Layer, mascara, media], dtype = object) #Temporary list to save the data before
                                                                                                                        #it be added to the final array
            #lista_numpy_temporaria = lista_numpy_temporaria.reshape(1,3)

            lista_final = np.concatenate((lista_final, lista_numpy_temporaria)) #Concatenating the two arrays

            contador = contador + 1 #Incrementing the counter

        lista_final = lista_final.reshape(contador,3)    #reshape the final array   


        np.savetxt(arcpy.env.workspace  + "\\Dados\\Exportados\\" + arquivo_com_as_medias + ".csv", lista_final, fmt="%10s %10.3f", delimiter=";", header = "#1, #2")




        #Attempt to write the arrays based in a tutorial that I found, but it didn't work
        """         
        ab = np.zeros(lista_numpy_temporaria.size, dtype=[('var1', 'U6'), ('var2', float)])
        ab['var1'] = lista_strings
        ab['var2'] = lista_medias

        np.savetxt(arcpy.env.workspace  + "\\Dados\\Exportados\\" + arquivo_com_as_medias + ".csv", ab, fmt="%10s %10.3f")
        """


        print(lista_final)

谢谢。

编辑1

解决方案建议后的新消息错误如下:

  

运行时错误回溯(最近一次调用最近):文件“”,   文件中的第210行   “ C:\ Python27 \ ArcGIS10.3 \ lib \ site-packages \ numpy \ lib \ npyio.py”,行   1047,在savetxt中       fh.write(asbytes(format%tuple(row)+ newline))TypeError:需要float参数,而不是Result

我发现的解决方案是将格式从fmt =“%10s%10s%10.3f”更改为此

  

RASTER FILE SUB_BASIN MEAN_VALUE out_Layer mascara 99.99

尽管它不是最佳解决方案,因为它以字符串形式返回数字。

我正在使用的完整代码如下。

fmt="%10s %10s %10s"

使用上面的代码,“ print(lista_final)”将返回以下内容:

import arcpy
import numpy  as np

from arcpy import sa 
from arcpy.sa import *
from calendar import monthrange


# Set environment settings
arcpy.env.workspace = "C:\\Projetos\\ArcGIS\\Teste9"  

arcpy.env.overwriteOutput = True 


#get the map document
mxd = arcpy.mapping.MapDocument("CURRENT")


#get the data frame
df = arcpy.mapping.ListDataFrames(mxd,"la*")[0]





#Months and years to interpolate
mes = ["2"] #Months
ano = ["1994"] #Years

#Days to interpolate based in the month lenght
coluna_interpolada_28 = ["D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15", "D16", "D17", "D18", "D19", "D20", "D21", "D22", "D23", "D24", "D25", "D26", "D27", "D28"]  
coluna_interpolada_29 = ["D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15", "D16", "D17", "D18", "D19", "D20", "D21", "D22", "D23", "D24", "D25", "D26", "D27", "D28", "D29"]
coluna_interpolada_30 = ["D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15", "D16", "D17", "D18", "D19", "D20", "D21", "D22", "D23", "D24", "D25", "D26", "D27", "D28", "D29", "D30"]
coluna_interpolada_31 = ["D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15", "D16", "D17", "D18", "D19", "D20", "D21", "D22", "D23", "D24", "D25", "D26", "D27", "D28", "D29", "D30", "D31"]



#Interpolation extent
arcpy.env.extent = arcpy.env.workspace + "\\Shapefile\\" + "PB.shp"



#Final list
lista_final = np.array([], dtype = object) 


#Counter that is going to be used to reshape the arrays
contador = 0 


#Loop to go through the time series
for i_ano in ano: #For loop with the years

    for i_mes in mes: #For loop with the months

        #Month Range
        quantidade_dias = monthrange(int(i_ano), int(i_mes))

        #If clauses to define which columns it is goin to interpolate
        if quantidade_dias == (1, 28):
            coluna_interpolada = coluna_interpolada_28
        elif quantidade_dias == (1, 29):
            coluna_interpolada = coluna_interpolada_29
        elif quantidade_dias == (1, 30):
            coluna_interpolada = coluna_interpolada_30
        else:
            coluna_interpolada = coluna_interpolada_31

        #For loop with the days
        for i_dia in coluna_interpolada:

            tabela = i_mes + "_" + i_ano   #Exemplo "2_1994"

            in_Table = arcpy.env.workspace + "\\Dados\\" + tabela + ".csv" #Exemplo "2_1994.csv"
            x_coords = "LONG"
            y_coords = "LAT"
            z_coords = "POINT_Z"
            out_Layer = "espacializacao" + "_" + tabela + "_" + i_dia #nome da camada "lyr" que vai ser criada   
                                                                                        #NOME DO ARQUIVO QUE VAI SER SALVO. Exemplo "espacializacao_2_1994_D2"
            out_Layer_shp = out_Layer + "_shp" #nome da camada "shp" que vai ser criada
            out_Layer_tif = out_Layer + "_tif"
            tamanho_celula = "0,10" #precisar por as aspas, apesar de ser um número
            potencia_idw = "2" #precisa por as aspas, apesar de ser um número
            raio_de_busca = RadiusVariable(12, 12) #Critério para fazer a interpolação, raio variando até 12 quilômetros até conseguir englobar 12 pontos




            # Set the spatial reference
            spRef = arcpy.SpatialReference("WGS 1984")

            #Create event layer
            arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, out_Layer, spRef, "")


            #Exporting event layer as shapefile
            arcpy.FeatureToPoint_management(out_Layer, arcpy.env.workspace + "\\" + "Shapefile\\Exportados\\" + out_Layer_shp + ".shp","")





            #Layer that is going to be deleted "lyr"
            lyr = arcpy.mapping.ListLayers(mxd, "espacializacao",df)



            #Deleting the layer
            for df in arcpy.mapping.ListDataFrames(mxd):
                 for lyr in arcpy.mapping.ListLayers(mxd, "", df): #O parâmetro que não foi passado foi o WildCard, não precisa
                     if lyr.name == out_Layer:
                         arcpy.mapping.RemoveLayer(df, lyr) #Removendo a camada da paleta lateral





            #Some variables to define some parameters to the software
            camada_editando = out_Layer_shp
            coluna_criada = "Media"
            tipo_campo = "FLOAT"
            precisao_campo = ""
            precisao_decimais = ""
            comprimento_campo = 50 #Tamanho qualquer suposto
            arcpy.AddField_management(camada_editando, coluna_criada, "LONG", precisao_campo,
                                          precisao_decimais, comprimento_campo, "", "NULLABLE",
                                          "NON_REQUIRED", "")   
            bacias = ["Abiai", "Camaratuba", "Curimatau", "Gramame", "Guaju", "Jacu", "Mamanguape", "Miriri", "Paraiba", "Piranhas", "Trairi"]          




            #Code to interpolate
            arcpy.gp.Idw_sa(out_Layer_shp, i_dia, arcpy.env.workspace + "\\Raster\\" + out_Layer_tif + ".tif", tamanho_celula, potencia_idw, "VARIABLE 12 12", "") 


            #Deleting shapefile
            arcpy.Delete_management(out_Layer_shp, "")  






            #For loop to clip the raster file using the shapefiles
            for mascara in bacias:
                importar_camada = arcpy.env.workspace + "\\Shapefile\\Bacias\\" + mascara + ".shp"  #Importing shapefile to clip
                arcpy.MakeFeatureLayer_management(importar_camada, mascara)  



                #Some variables defining some parameters to using in the clip function
                camada_para_recortar = out_Layer_tif + ".tif"
                camada_resultante = out_Layer + "_recortada"
                nome_do_raster = camada_resultante + "_" + mascara + ".tif" 

                #Function to clip the raster file
                arcpy.Clip_management(camada_para_recortar, "", arcpy.env.workspace  + "\\Raster\\Recortes\\" + camada_resultante + "_" + mascara + ".tif", mascara, "-3,402823e+038", "ClippingGeometry", "NO_MAINTAIN_EXTENT")
                media = arcpy.GetRasterProperties_management (nome_do_raster, "MEAN", "")


                lista_strings = np.array([out_Layer, mascara])
                lista_medias = np.array([media])


                #Name of the file to save the means values
                arquivo_com_as_medias = "medias 01" 


                lista_numpy_temporaria = np.append(lista_strings, lista_medias)
                lista_final = np.concatenate((lista_final, lista_numpy_temporaria))


                #Deleting the raster clipped
                arcpy.Delete_management(nome_do_raster, "") 

                #Counter
                contador = contador + 1 

                print(lista_final)

            #Reshaping the file 
            lista_final = lista_final.reshape(contador,3)       

            print(lista_final)

            #Saving the arrays to a CSV file
            np.savetxt(arcpy.env.workspace  + "\\Dados\\Exportados\\" + arquivo_com_as_medias + ".csv", lista_final, fmt="%10s %10s %10s", delimiter=";", header = "")




            #Deleting the original raster
            arcpy.Delete_management(out_Layer_tif + ".tif", "") 

我在想的另一件事是,这是将这些输出写入CSV文件的最佳方法吗?因为我已经看过一些使用“ CSV模块”直接将CSV文件写入的教程。但是,我已经尝试过了,但没有成功。

之所以这样说,是因为该代码将重复很多次,例如19.000次。因为我使用它来插值54年时间序列中的每日降雨量数据。因此,我认为使用这种大小的数组不是解决此问题的好方法。

再次感谢您。

编辑2

我尝试了您的建议,但仍然无法正常工作。我所做的更改是

    >>> print(lista_final)
[[u'espacializacao_2_1994_D1' u'Abiai' u'111,81740045547']
 [u'espacializacao_2_1994_D1' u'Camaratuba' u'328,11316935221']
 [u'espacializacao_2_1994_D1' u'Curimatau' u'273,3234489704']
 [u'espacializacao_2_1994_D1' u'Gramame' u'223,45285224915']
 [u'espacializacao_2_1994_D1' u'Guaju' u'393,62130737305']
 [u'espacializacao_2_1994_D1' u'Jacu' u'312,91506958008']
 [u'espacializacao_2_1994_D1' u'Mamanguape' u'289,06595204671']
 [u'espacializacao_2_1994_D1' u'Miriri' u'564,86507415771']
 [u'espacializacao_2_1994_D1' u'Paraiba' u'330,80016106998']
 [u'espacializacao_2_1994_D1' u'Piranhas' u'328,95194289264']
 [u'espacializacao_2_1994_D1' u'Trairi' u'333,04579162598']]

现在显示的错误消息如下

  

运行时错误回溯(最近一次调用最近):文件“”,   文件中的第176行   “ C:\ Python27 \ ArcGIS10.3 \ lib \ site-packages \ numpy \ core \ records.py”,行   560,在fromarrays中       引发ValueError(“数组%d中的数组形状不匹配”%k)ValueError:数组2中的数组形状不匹配

第176行的代码是

#First attempt 
one = np.array([out_Layer], dtype = object)  
two = np.array([mascara], dtype = object)    
three = np.array([media])      

rarr = np.rec.fromarrays([one, two, three])   

arquivo_com_as_medias = "medias 01" #File with the means values 
csv_directory = arcpy.env.workspace  + "\\Dados\\Exportados\\" + arquivo_com_as_medias + ".csv" 

np.savetxt(csv_directory, rarr, fmt=['%s', '%s', '%f'], delimiter=";")

2 个答案:

答案 0 :(得分:1)

至少就您得到的错误而言,它就像是说:您正在尝试输出3列,但仅提供两种格式。如果将fmt参数更改为fmt=['%10s', '%10s', '%10.3f'],则np.savetxt应该可以工作。

您可能会遇到其他错误,例如与要保存的数组的格式/内容有关的那些错误。为了避开任何此类潜在错误,您可能还需要在脚本中临时添加一行,例如:

print(lista_final[:100])

就在np.savetxt行之前。这样,您可以确保在尝试通过lista_final处理数组之前,savetxt的内容(至少在前一百行中)实际上是您期望的内容。

编辑

当您在代码末尾调用np.concatenate((lista_final, lista_numpy_temporaria))时,最终将所有3列强制放入一个标准Numpy数组中。标准数组只能有一个dtype,因此所有内容(包括最后一列中的浮点数)最终都将转换为字符串。您可以通过将列连接到记录数组(每个列可以具有不同的dtype)来避免这种情况。假设您要在脚本末尾将三个列组合在一起,就像这样:

one = np.array(["u'espacializacao_2_1994_D1'", "u'espacializacao_2_1994_D1'",
       "u'espacializacao_2_1994_D1'", "u'espacializacao_2_1994_D1'",
       "u'espacializacao_2_1994_D1'", "u'espacializacao_2_1994_D1'",
       "u'espacializacao_2_1994_D1'", "u'espacializacao_2_1994_D1'",
       "u'espacializacao_2_1994_D1'", "u'espacializacao_2_1994_D1'",
       "u'espacializacao_2_1994_D1'"], dtype=object)

two = np.array(["u'Abiai'", "u'Camaratuba'", "u'Curimatau'", "u'Gramame'",
       "u'Guaju'", "u'Jacu'", "u'Mamanguape'", "u'Miriri'", "u'Paraiba'",
       "u'Piranhas'", "u'Trairi'"], dtype=object)

three = np.array([111.81740046, 328.11316935, 273.32344897, 223.45285225,
       393.62130737, 312.91506958, 289.06595205, 564.86507416,
       330.80016107, 328.95194289, 333.04579163])

然后,将它们连接到一个记录数组中,然后将它们写到csv文件中,如下所示:

rarr = np.rec.fromarrays([one, two, three])
np.savetxt('arcgis.csv', rarr, fmt=['%s', '%s', '%f'], delimiter=";")

答案 1 :(得分:1)

我找到的解决方案是使用CSV文件读写库。

我只是在“ import csv”开头添加了库,然后在代码中添加了以下几行。

首先,在乞求中,我需要设置文件目录以保存数据。

#Opening the file to save the data
write_path = main_directory + "\\Dados\\Exportados\\" + "file_name" + ".csv"
file = open(write_path, 'a')
writer = csv.writer(file)

'a'参数允许您附加到文件末尾

然后,在我希望保存数据的步骤中,添加了以下行:

#Writing to the file
writer.writerow([mes_pelo_nome, ano_pelo_nome, dia_pelo_nome, mascara, media]) 

最后,我添加了以下行以关闭文件:

file.close()