0

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.

4
  • You have numpy and you want to do further anaysis in excel? Gee... Let me translate how it sounds. I have an automatic machine gun, I'd like to hunting with a spear. Commented Jul 22, 2014 at 9:08
  • 2
    Does docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html not do what you want? Commented Jul 22, 2014 at 9:10
  • @Oz123 think of it more as a Bayonet :). But seriously: I guess if you are trained in using pivot tables, there is a lot of analysis you can do faster and easier in excel. Commented Jul 22, 2014 at 9:30
  • Ok. Thanks for pointing in the right direction @EdChum. Commented Jul 22, 2014 at 9:35

1 Answer 1

3

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.

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

2 Comments

To me pythonic is not an issue here, numpy has provided a method for loading text data and provided a method for saving it, therefore this would be the preferred method. Incidentally Pandas is much faster at loading csv data and probably writing it out, it has a friendlier api than numpy
Yes, using the api is always pythonic :).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.