nhpip

nhpip

Writing a term (nested structs) in human-readable form to a text file - is there a nicer way to do this?

Hi,

I want to write a term (nested structs) in human-readable form to a text file. This is what I’ve got, there must be a better way…

 i_am_a_term_now_a_string  = :io_lib.format("~p.", [i_am_a_term]) 
                                          |> List.flatten()   
                                          |> to_string()
File.write!("/tmp/a_term_on_your_disk", i_am_a_term_now_a_string)

Thanks

Most Liked

c4710n

c4710n

If you think quoted expression is human-readable. Try to use:

  • quote
  • Code.eval_quoted

Or, just use :erlang.term_to_binary and :erlang.binary_to_term. When you want to inspect it, use something like following code instead of reading it directly.

File.read!("path/to/file")
|> :erlang.binary_to_term()
|> IO.inspect()

I think it is better to tell us your scenario. It’s an XY problem probably.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

A bit of info will be helpful. The way to handle (most) terms in a human readable way is to use inspect to dump the data to disk, and then Code.eval_string to turn it back into a value. However this has a variety of trade offs and won’t handle arbitrary terms because things like pid values for example don’t have an output from inspect that will turn back into a pid when evaled. Whether that matters or not depends on your use case.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @nhpip did you try inspect/1 ?

i_am_a_term_now_a_string = inspect(term, limit: :infinity)
derek-zhou

derek-zhou

I’d use ~tp.~n so there is unicode characters. and a newline between terms. also you don’t need to flatten the iostring prior to write.

Most Erlang config files are written like this and read back by consult. It is very nice and simple. Or you can use other serialization format like json, or toml but they don’t support all erlang types.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

@nhpip Can you explain what this does and what you’re trying to do? Are you trying to turn that term back into an Elixir value?

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement