简体   繁体   中英

Write numpy arrays to files using csv

I have a list of .asc files with geographic coordinates that I would like to load into a .csv file for further analysis in Excel. Each .asc file contains approximately 2000 rows.

list_files = ['./p2c0765f029.asc',..., './p2c0781f029.asc']


with open('output_file.csv', 'a+') as outfile:
    for file in list_files:
        file_in = loadtxt(file, usecols = (1,2), comments = "#")      
        csvwriter = csv.writer(outfile, delimiter = ' ')
        csvwriter.writerow('%s\n' % file_in)  

However this outputs a file in which each array is stored in one row (when I open the file in Excel).

[ [ " " - 8 . 7 9 8 4 5 4 5 0 e + 0 1 " " " " " " 9 . 5 5 1 5 5 4 8 0 e + 0 1 " " " " ]]

How could I implement this so each line in the numpy arrays is written in one row? Also I would like to get rid of " and [] and output only the coordinates.

Using numpy.savetxt I get the output I needed:

with open('output_file.txt', 'a+') as outfile:
     for file in list_files:
         file_in = loadtxt(file, usecols = (1,2), comments = "#")      
         np.savetxt(outfile, file_in, delimiter=' ', newline='\n')   

Not sure if it's the most pythonic way, but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM