# Pass a Julia string to Python as an iterable

**URL:** <https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 4, 2016, 3:48pm UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724 "2016-12-04T15:48:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![samuelpowell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuelpowell/32/248_2.png) [@samuelpowell](https://discourse.julialang.org/u/samuelpowell)\
**Post date:** [December 4, 2016, 3:48pm UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724/1 "2016-12-04T15:48:32Z")

</div>

Is it possible to pass a Julia `String` (or alternatively a `Vector{UInt8}` or `IOBuffer` wrapping the same) to Python as an iterable object?

I have attempted to pass the data directly, and also as part of a Julia generator (e.g. `(c for c in s)`) to no avail.

The use case is calling `upload_object_via_stream` in `LibCloud.jl` without writing a file to disk.

---

<div class="post-metadata">

**Author:** ![Tem\_Pl](https://avatars.discourse-cdn.com/v4/letter/t/8dc957/32.png) [@Tem\_Pl](https://discourse.julialang.org/u/Tem_Pl)\
**Post date:** [December 4, 2016, 8:36pm UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724/2 "2016-12-04T20:36:21Z")

</div>

is there a pycall convert function you can overload?

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [December 5, 2016, 2:21am UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724/3 "2016-12-05T02:21:55Z")

</div>

Please provide an example of the issue. PyCall converts Julia strings to Python ‘str’, and ‘str’ is iterable, so the following works as expected:

```julia
pyeval("[x for x in y]", y="hello")

```

---

<div class="post-metadata">

**Author:** ![samuelpowell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuelpowell/32/248_2.png) [@samuelpowell](https://discourse.julialang.org/u/samuelpowell)\
**Post date:** [December 5, 2016, 8:06am UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724/4 "2016-12-05T08:06:21Z")

</div>

```julia
using LibCloud.Storage

storage = StorageDriver(region, aws_key, aws_secret)
bucket = get_container(storage, bucketname)
upload_object_via_stream(container, "a test string", "cloudfile")

ERROR: PyError (:PyObject_Call) <type 'exceptions.AttributeError'>
AttributeError("'str' object has no attribute 'next'",)

```

I would like to be able to pass some data in memory (in this case “a test string”) in a way over which Python would be happy to iterate. I get the impression I may misunderstand what type of object Python is expecting…

Any suggestions?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 5, 2016, 12:51pm UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724/5 "2016-12-05T12:51:09Z")

</div>

Python [strings don’t have a next attribute](http://stackoverflow.com/questions/12552986/python-3-do-strings-have-next-method). Maybe it is expecting a bytearray, which you can pass from Julia via `Vector{UInt8}("a test string")`.

---

<div class="post-metadata">

**Author:** ![samuelpowell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuelpowell/32/248_2.png) [@samuelpowell](https://discourse.julialang.org/u/samuelpowell)\
**Post date:** [December 5, 2016, 12:57pm UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724/6 "2016-12-05T12:57:59Z")

</div>

As per the original post, this also doesn’t work, failing with the same error:

```julia
upload_object_via_stream(container, Vector{UInt8}("a test string"), "cloudfile")

ERROR: PyError (:PyObject_Call) <type 'exceptions.AttributeError'>
AttributeError("'bytearray' object has no attribute 'next'",)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 5, 2016, 1:00pm UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724/7 "2016-12-05T13:00:05Z")

</div>

Well, you need to read the documentation for the Python module. Figure out what works in Python, and then translate it to the corresponding Julia code.

---

<div class="post-metadata">

**Author:** ![samuelpowell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuelpowell/32/248_2.png) [@samuelpowell](https://discourse.julialang.org/u/samuelpowell)\
**Post date:** [December 5, 2016, 1:06pm UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724/8 "2016-12-05T13:06:43Z")

</div>

According to the documentation the argument should be:

`iterator (object) – An object which implements the iterator interface.`

I will do some digging.

Thanks for your time.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [December 5, 2016, 2:37pm UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724/9 "2016-12-05T14:37:44Z")

</div>

Use [StringIO](https://docs.python.org/2/library/stringio.html).

---

<div class="post-metadata">

**Author:** ![samuelpowell](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samuelpowell/32/248_2.png) [@samuelpowell](https://discourse.julialang.org/u/samuelpowell)\
**Post date:** [December 5, 2016, 5:25pm UTC](https://discourse.julialang.org/t/pass-a-julia-string-to-python-as-an-iterable/724/10 "2016-12-05T17:25:54Z")

</div>

This works perfectly, thank you.
