用不同的字符串定界数组

编程入门 行业动态 更新时间:2024-10-28 10:28:51
本文介绍了用不同的字符串定界数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个文本文件,其中包含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

更多推荐

用不同的字符串定界数组

本文发布于:2023-07-22 22:08:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1191457.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   定界

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!