我有一个带有地理坐标的.asc文件列表,我想将其加载到.csv文件中,以便在Excel中进行进一步分析。每个.asc文件包含大约2000行。
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)
但是,这会输出一个文件,其中每个数组都存储在一行中(当我在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 " " " " ]]
我怎么能实现这一点,所以numpy数组中的每一行都写成一行?另外,我想摆脱"
和[]
并仅输出坐标。
答案 0 :(得分:3)
使用numpy.savetxt我得到了我需要的输出:
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')
不确定它是否是最pythonic方式,但它确实有效。