# Metaprogramming process text to Dictionary

**URL:** <https://discourse.julialang.org/t/metaprogramming-process-text-to-dictionary/31777>\
**Category:** New to Julia\
**Tags:** metaprogramming\
**Created:** [December 3, 2019, 12:08am UTC](https://discourse.julialang.org/t/metaprogramming-process-text-to-dictionary/31777 "2019-12-03T00:08:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)\
**Post date:** [December 3, 2019, 12:08am UTC](https://discourse.julialang.org/t/metaprogramming-process-text-to-dictionary/31777/1 "2019-12-03T00:08:39Z")

</div>

Hello guys,

I never use metaprogramming before. I imagined it makes a string to code? (it is probably wrong understanding at all) So I was thinking whether I can make use of it to process one of my text file. The file is like that

```julia
o = open("text.txt", "w")
println(o, "A\t1; B\t\"abc\"; C\t\"efg\";")
println(o, "A\t20; B\t\"ijkl\"; D\t\"mno\";")
close(o)

```

‘;’ is used to defined each field. Each field includes a tab delineate of key and value pair. I would like to process it into a dictionary per line. For example line 1 to be the dictionary

Dict((“A”=\>1, “B”=\>“abc”, “C”=\>“efg”))

I could do it by split and split. Since the raw string is already structured like dictionary definition, is it possible to directly proceed it into a dictionary? I know it is odd to do it like that. What’s the pros and cons of doing so?

Thank you very much!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 3, 2019, 12:20am UTC](https://discourse.julialang.org/t/metaprogramming-process-text-to-dictionary/31777/2 "2019-12-03T00:20:05Z")

</div>

No. Metaprogramming is for processing Julia syntax/generating julia code at compile time and it almost never involve parsing strings.

It’s not designed to do what you want and you shouldn’t use it. You should just treat this like a normal string parsing problem and find the right tool for that.

---

<div class="post-metadata">

**Author:** ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)\
**Post date:** [December 3, 2019, 12:32am UTC](https://discourse.julialang.org/t/metaprogramming-process-text-to-dictionary/31777/3 "2019-12-03T00:32:45Z")

</div>

Okay I see. So even though I have a text file has the julia code as following each line. I cannot do that because when it see’s the file the program already running, not at compile time?

:A=\>1, :B=\>“abc”, :C=\>“efg”  
:A=\>2, :B=\>“abcd”, :C=\>“efgs”  
…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 3, 2019, 12:57am UTC](https://discourse.julialang.org/t/metaprogramming-process-text-to-dictionary/31777/4 "2019-12-03T00:57:49Z")

</div>

Kind of. A few points,

1. Your data file is data, it’s not code. If all what you need is processing some data you should use the correct tool rather than converting it to code.
2. There are many features in the language. If you invoke the whole language, the features you don’t need will make it very slow (since it’ll do unnecessary work) and very dangerous (since your data can basically run arbitrary code).

Another way to view this is that this is the difference between `JSON.parse` and `eval` in js.

---

<div class="post-metadata">

**Author:** ![Donut\_Meepo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donut_meepo/32/9085_2.png) [@Donut\_Meepo](https://discourse.julialang.org/u/Donut_Meepo)\
**Post date:** [December 3, 2019, 1:08am UTC](https://discourse.julialang.org/t/metaprogramming-process-text-to-dictionary/31777/5 "2019-12-03T01:08:24Z")

</div>

Thank you for the explanations.

---

<div class="post-metadata">

**Author:** ![Duane\_Wilson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/duane_wilson/32/9506_2.png) [@Duane\_Wilson](https://discourse.julialang.org/u/Duane_Wilson)\
**Post date:** [December 3, 2019, 5:52pm UTC](https://discourse.julialang.org/t/metaprogramming-process-text-to-dictionary/31777/6 "2019-12-03T17:52:38Z")

</div>

Just on the topic of metaprogramming I found this video to be extremely good for explaining when to consider using metaprogramming for things and why it is an easy “trap” to get caught in for things.

[![](https://global.discourse-cdn.com/julialang/original/3X/7/5/759062729d830f12cdfa7374e0be51cc40e42a54.jpeg "JuliaCon 2019 | Keynote: Professor Steven G. Johnson") ](https://www.youtube.com/watch?v=mSgXWpvQEHE)

I think probably most telling bit is just how few instances there are of metaprogramming in the Julia standard library.
