我有两个坐标变量经度和纬度
(array([-50.940605, -37.424145, -41.501717, -37.98916 , -60.632664,
-38.158283, -58.0372 , -39.06596 , -60.815792, -49.303684,
-38.46074 , -50.979015, -38.479317, -58.656593, -38.40213 ,
-38.007423, -43.785126, -57.75844 , -42.781937, -74.90217 ,
-34.498913, -49.227158, -96.485504, -72.128716, -39.414085],
dtype=float32), array([-15.90009 , -15.913551 , -15.7658 , -16.47591 ,
-4.1940403 , -14.563205 , -6.032389 , -14.852597 ,
-4.24735 , 15.848547 , -14.71412 , -17.052591 ,
-14.079368 , -5.9365387 , -14.50551 , -16.48979 ,
-16.616753 , -0.23096395, -15.882113 , -5.0795455],
dtype=float32))
字符串
我想缩短它们并保存为.csv文件,如下所示:
-50.940605,-15.90009
-37.424145,-15.913551
-41.501717-15.7658
-37.98916,-16.47591
-60.632664-4.1940403
-38.158283-14.563205
.... and so on
型
我在用命令
coord_file = (flag_lon.data[0:100], flag_lat.data[0:100]) # they are variables extracted from a NetCDF fila
np.savetxt('coord_file.csv', coord_file, delimiter=",", fmt="%s")
print(open("coord_file.csv").read())
型
但它返回:
-50.940605,-37.424145,-41.501717,-37.98916,-60.632664,-38.158283,-58.0372,-39.06596,-60.815792,-49.303684,-38.46074,-50.979015,-38.479317,-58.656593,-38.40213,-38.007423,-43.785126
-15.90009,-15.913551,-15.7658,-16.47591,-4.1940403,-14.563205,-6.032389,-14.852597,-4.24735,15.848547,-14.71412,-17.052591,-14.079368,-5.9365387,-14.50551,-16.48979,-16.616753,-0.23096395,-15.882113,-5.0795455,-20.313402,15.771035,-7.3693175,-7.8538485,-14.27069,-16.976015,
型
我怎样才能把它们排列成两个一长一短的呢?
3条答案
按热度按时间exdqitrt1#
你可以这样试试
字符串
cvxl0en22#
发生错误的原因是经度和纬度数组的长度不同:经度数组有25个元素,而纬度数组只有20个元素。若要创建具有坐标对的有效CSV文件,两个数组的长度必须相同。
fumotvh33#
你可以考虑和pandas和numpy一起工作。
numpy是为处理数组和矩阵而设计的库,pandas是为数据分析和操作而设计的
正如Marcin Orlowski所说,两个数组需要具有相同的长度。
然后你可以使用这样的东西:
字符串
这就是我想要的,希望这对你有帮助。