Mathematica - 导入 CSV 并处理列?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
肯尼TM是正确的。
KennyTM is correct.
如果您需要导入整个 CSV 文件作为数组。但是,如果您需要从 C/Fortran 样式指数表示法转换单个字符串,则可以使用
ImportString
以及不同的格式参数。例如,*^
运算符相当于 Mathematica 中的e
。请注意,这也是拆分 CSV 形式的字符串的好方法:在这两种情况下,您都会将答案包含在额外级别的列表结构中,这非常容易处理。但是,如果您确实确定只有或想要一个数字,则可以将字符串转换为流并使用
Read
。然而,它太麻烦了,我会坚持使用ImportString
: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'sThe
*^
operator is Mathematica's equivalent of thee
. Note this is also a good way to split apart strings that are in CSV form: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 toImportString
, however:您可以使用
StringReplace[]
。您可以将整个数据数组放在 aa 的位置,以便使用带有
ToExpression[...]
的单行程序一次处理所有内容,如上所述。
You can fix the notation by using
StringReplace[]
.You can put the entire data array in place of aa to process is all at once with a one liner
with
ToExpression[...]
as above.在 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.