Mathematica - 导入 CSV 并处理列?

发布于 2024-08-26 08:00:03 字数 499 浏览 6 评论 0原文

我有一个 CSV 文件,其格式如下:

0.0023709,8.5752e-007,4.847e-008

我想将其导入 Mathematica,然后将每一列分成一个列表,以便我可以对所选列进行一些数学运算。

我知道我可以使用以下方式导入数据:

Import["data.csv"]

然后我可以用以下方式分隔列:

StringSplit[data[[1, 1]], ","]

这给出了:

{"0.0023709", "8.5752e-007", "4.847e-008"}

现在的问题是我不知道如何将数据放入单独的列表中,而且 Mathematica 不接受以下形式的科学记数法8.5e-007。

任何有关如何将数据分成列和格式化科学记数法的帮助都会很棒。

提前致谢。

I have a CSV file that is formatted like:

0.0023709,8.5752e-007,4.847e-008

and I would like to import it into Mathematica and then have each column separated into a list so I can do some math on the selected column.

I know I can import the data with:

Import["data.csv"]

then I can separate the columns with this:

StringSplit[data[[1, 1]], ","]

which gives:

{"0.0023709", "8.5752e-007", "4.847e-008"}

The problem now is that I don't know how to get the data into individual lists and also Mathematica does not accept scientific notation in the form 8.5e-007.

Any help in how to break the data into columns and format the scientific notation would be great.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

z祗昰~ 2024-09-02 08:00:03

肯尼TM是正确的。

data = Import["data.csv", "CSV"];
column1 = data[[All,1]]
column2 = data[[All,2]]
...

KennyTM is correct.

data = Import["data.csv", "CSV"];
column1 = data[[All,1]]
column2 = data[[All,2]]
...
熟人话多 2024-09-02 08:00:03

如果您需要导入整个 CSV 文件作为数组。但是,如果您需要从 C/Fortran 样式指数表示法转换单个字符串,则可以使用 ImportString 以及不同的格式参数。例如,

In[1]:= ImportString["1.0e6", "List"]
Out[1]= {1.*^6}

*^ 运算符相当于 Mathematica 中的 e。请注意,这也是拆分 CSV 形式的字符串的好方法:

In[2]:= ImportString["1.0e6,3.2,foo", "CSV"] 
Out[2]= {{1.*10^6,3.2,foo}}

在这两种情况下,您都会将答案包含在额外级别的列表结构中,这非常容易处理。但是,如果您确实确定只有或想要一个数字,则可以将字符串转换为流并使用Read。然而,它太麻烦了,我会坚持使用 ImportString

In[3]:= Module[{stream = StringToStream["1.0e6"], number},
          number = Read[stream, "Number"];
          Close[stream];
          number]
Out[3]= 1.*10^6

Davorak's answer is the correct one if you need to import a whole CSV file as an array. However, if you have a single string that you need to convert from the C/Fortran-style exponential notation, you can use ImportString with different arguments for the format. As an example, there's

In[1]:= ImportString["1.0e6", "List"]
Out[1]= {1.*^6}

The *^ operator is Mathematica's equivalent of the e. Note this is also a good way to split apart strings that are in CSV form:

In[2]:= ImportString["1.0e6,3.2,foo", "CSV"] 
Out[2]= {{1.*10^6,3.2,foo}}

In both cases, you'll get your answer wrapped up in an extra level of list structure, which is pretty easy to deal with. However, if you're really sure you only have or want a single number, you can turn the string into a stream and use Read. It's cumbersome enough that I'd stick to ImportString, however:

In[3]:= Module[{stream = StringToStream["1.0e6"], number},
          number = Read[stream, "Number"];
          Close[stream];
          number]
Out[3]= 1.*10^6
夜空下最亮的亮点 2024-09-02 08:00:03

您可以使用 StringReplace[]

In[1]: aa = {"0.0023709", "8.5752e-007", "4.847e-008"};

In[2]: ToExpression[
          StringReplace[
             #,
             RegularExpression@"(^\d+\.\d+)e([+-]\d+)" -> "$1*10^$2"
          ]
       ] & @ aa

Out[2]: {0.0023709, 8.5752*10^-7, 4.847*10^-8}

您可以将整个数据数组放在 aa 的位置,以便使用带有 ToExpression[...] 的单行程序一次处理所有内容,

{col1,col2,col3} = ToExpression[...] & @ Transpose[Import["data.csv", "CSV"]];

如上所述。

You can fix the notation by using StringReplace[].

In[1]: aa = {"0.0023709", "8.5752e-007", "4.847e-008"};

In[2]: ToExpression[
          StringReplace[
             #,
             RegularExpression@"(^\d+\.\d+)e([+-]\d+)" -> "$1*10^$2"
          ]
       ] & @ aa

Out[2]: {0.0023709, 8.5752*10^-7, 4.847*10^-8}

You can put the entire data array in place of aa to process is all at once with a one liner

{col1,col2,col3} = ToExpression[...] & @ Transpose[Import["data.csv", "CSV"]];

with ToExpression[...] as above.

邮友 2024-09-02 08:00:03

在 MMA7 中,我使用“元素”参数。事实上,如果不指定元素,我什至无法导入 .csv 文件:

aa=Import["data.csv","Data"]

执行此操作时,所有字符串都会自动转换为表达式: Head/@Flatten@ aa 是{实数,实数,....}。另外,“8.5752e-007”变成8.5752*10^7,一个合法的MMA表达式。

导入的结果是一个 1xn 列表 {{ ... }}。

因此,Transpose@aa 给出 nx1 列表 {{.},{.}, .... }。

我想这就是你想要的格式。

In MMA7, I use the "elements" argument. In fact, I can't Import even a .csv file without specifying the element:

aa=Import["data.csv","Data"]

When you do this, all strings are automatically converted to expressions: Head/@Flatten@aa is {Real, Real, ....}. Also, "8.5752e-007" becomes 8.5752*10^7, a legal MMA expression.

The result of the Import is a 1xn list {{ ... }}.

So, Transpose@aa gives the nx1 list {{.},{.}, .... }.

I think this is the format you wanted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文