Papillon6814

Papillon6814

How to convert charlist to string?

Hello. I’m working on running python file by elixir.
The python file returns dict, and elixir parses it as map. But its keys are charlists like this:

%{'graph' => {:"$erlport.opaque", :python, <<128, 2, 99, 110, 101, 111, 52, 106, 46, 103, 114, 97, 112, 104, 10, 71, 114, 97, 112, 104, 10, 113, 0, 41, 129, 113, 1, 125, 113, 2, 40, 88, 6, 0, 0, 0, 95, 110, 111, 100, 101, 115, 113, 3, 125, 113, ...>>}, 'id' => 7, 'labels' => ...

I have never seen a map which has charlist keys, so I’d like to convert the key to string, but I don’t still find a good way to do it. Do you have an idea for it? Thank you :smile:

Marked As Solved

hst337

hst337

What library do use for interactions with Python? I think it might have an option to return binaries instead of charlists

But until then you can just charlist = 'abc'; string = "#{charlist}" or use to_string. To traverse maps, you can use Map.new()

So, to convert one map, you should write something like

Map.new(old_map, fn
  {list, value} when is_list(list) ->
    key =
      try do
        "#{list}"
      except
        # In case it's not a charlist
        _ -> list
      end

    {key, value}

  other ->
    other
end)

Also Liked

adamu

adamu

This code is pretty defensive. I’d probably do:

 Map.new(map, fn {k, v} -> {List.to_string(k), v} end)

And let List.to_string/1 handle the edge-cases.

BartOtten

BartOtten

Post removed. @hst337 was faster :slight_smile:

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

Other popular topics Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
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

We're in Beta

About us Mission Statement