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 ![]()
Marked As Solved
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)
2
Also Liked
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.
3
Last Post!
hst337
Popular in Questions
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
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
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
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
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
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
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
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









