Message Boards Message Boards

0
|
6497 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Import from a txt file?

Posted 8 years ago

Dear board members

I'm looking for a way to import data from a txt file with thermodynamic equiblibrium-data calculated with a phyton script. The content looks as follows: T(degC),H2,H2O,CO2,CO,CH4,O2,N2,C(gr),H(Joule) 4.0000e+02,4.1961e-01,7.9240e-01,1.0160e-01,4.4040e-03,8.9400e-01,2.3395e-32,5.0000e-01,0.0000e+00,-2.6108e+05

Unfortunately i'm not able to change the formatting of the text file. I already tried different import options. E.g. i tried it in this way:

EquilibriumRefGas = Take[Import["thermoData.csv"]]

But this just leads to the following:

{{"T(degC)", "H2", "H2O", "CO2", "CO", "CH4", "O2", "N2", "C(gr)", "H(Joule)"}, {400., 0.41961, 0.7924, 0.1016, 0.004404, 0.894, 2.3395*10^-32, 0.5, 0., -261080.}}

At the end i need to Select individual parts of the list. Anybody has a hint how i could manage this?

Thanks a lot! Dani

POSTED BY: Daniel Meier
4 Replies
Posted 8 years ago

Hello Dani,

from what I can see the issue is that the 1st line of of the import contains the header of the table. You can split this off by using

{hdr, data} = TakeDrop[Import["thermoData.csv"], 1]

Depending on what type of analysis you are intended to do it might be helpful to transform the import into a dataset. This can be done using

dataset = Dataset[Map[Association[Thread[hdr -> #]] &, data]]
POSTED BY: Michael Helmle
Posted 8 years ago

I used Chop to eliminate the tiny number. That may not be appropriate.

But here is one way :

In[1]:= {keys, 
   data} = {{"T(degC)", "H2", "H2O", "CO2", "CO", "CH4", "O2", "N2", 
     "C(gr)", "H(Joule)"}, {400., 0.41961, 0.7924, 0.1016, 0.004404, 
     0.894, 2.3395*10^-32, 0.5, 0., -261080.}} // Chop;

In[2]:= assn = AssociationThread[keys -> data]

Out[2]= <|"T(degC)" -> 400., "H2" -> 0.41961, "H2O" -> 0.7924, 
 "CO2" -> 0.1016, "CO" -> 0.004404, "CH4" -> 0.894, "O2" -> 0, 
 "N2" -> 0.5, "C(gr)" -> 0, "H(Joule)" -> -261080.|>

In[3]:= "T(degC)" /. assn

Out[3]= 400.

In[4]:= {"T(degC)", "CO"} /. assn

Out[4]= {400., 0.004404}
POSTED BY: David Keith
Posted 8 years ago

Dear David and Michael

Thanks for the two possible solutions! Finally I implemented it in Michaels way, because i need all the values, even if they are very small.

I wish you a good new year, Dani

POSTED BY: Daniel Meier

I know that you have already adopted a solution to your problem . However, note that any solution using rules will become expensive as the size of you file increase. Below, I provide a solution that will be faster as the size of your file increases. It is a one-liner using transpose.

Transpose[{{"T(degC)", "H2", "H2O", "CO2", "CO", "CH4", "O2", "N2", 
   "C(gr)", "H(Joule)"}, {400., 0.41961, 0.7924, 0.1016, 0.004404, 
   0.894, 2.3395*10^-32, 0.5, 0., -261080.}}]

The output looks like this

{{"T(degC)", 400.}, {"H2", 0.41961}, {"H2O", 0.7924}, {"CO2", 
  0.1016}, {"CO", 0.004404}, {"CH4", 0.894}, {"O2", 
  2.3395*10^-32}, {"N2", 0.5}, {"C(gr)", 0.}, {"H(Joule)", -261080.}}
POSTED BY: Ta'a Nwa Dombou
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract