用不同的字符串定界数组 [英] Delimit array with different strings

查看:90
本文介绍了用不同的字符串定界数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,其中包含3列有用的数据,我希望能够使用numpy在python中提取这些数据.该文件类型是* .nc,不是是netCDF4文件类型.它是CNC机器的标准文件输出类型.就我而言,它是一种CMM(坐标测量机).格式如下:

I have a text file that contains 3 columns of useful data that I would like to be able to extract in python using numpy. The file type is a *.nc and is NOT a netCDF4 filetype. It is a standard file output type for CNC machines. In my case it is sort of a CMM (coordinate measurement machine). The format goes something like this:

X0.8523542Y0.0000000Z0.5312869

X0.8523542Y0.0000000Z0.5312869

X,Y和Z是机器上的坐标轴.我的问题是,我可以用多个定界符来定界一个数组吗?在这种情况下:"X","Y"和"Z".

The X,Y, and Z are the coordinate axes on the machine. My question is, can I delimit an array with multiple delimiters? In this case: "X","Y", and "Z".

推荐答案

您可以使用熊猫

import pandas as pd
from io import StringIO

#Create a mock file
ncfile = StringIO("""X0.8523542Y0.0000000Z0.5312869
X0.7523542Y1.0000000Z0.5312869
X0.6523542Y2.0000000Z0.5312869
X0.5523542Y3.0000000Z0.5312869""")

df  = pd.read_csv(ncfile,header=None)

#Use regex with split to define delimiters as X, Y, Z.
df_out = df[0].str.split(r'X|Y|Z', expand=True)

df_out.set_axis(['index','X','Y','Z'], axis=1, inplace=False)

输出:

  index          X          Y          Z
0        0.8523542  0.0000000  0.5312869
1        0.7523542  1.0000000  0.5312869
2        0.6523542  2.0000000  0.5312869
3        0.5523542  3.0000000  0.5312869

这篇关于用不同的字符串定界数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆