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:

Last Post!

hst337

hst337

erlport but it was a long time ago and I can’t remember anything about the library today

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36820 110
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement