Convert SubString{ASCIIString} to String

1,512 views
Skip to first unread message

Charles Novaes de Santana

unread,
Dec 4, 2015, 5:48:12 PM12/4/15
to julia...@googlegroups.com
Hi people,

Maybe it is a trivial question for most of you, but I really could not find a way to solve my problem.

I am using the function quandlget(id::ASCIIString) from the library https://github.com/milktrader/Quandl.jl (a great contribution, by the way!)

Everything works fine when I use it in a straightforward way:

julia> mydat = quandl("GOOG/NASDAQ_GOOG",rows=100,format="DataFrame")
100x6 DataFrames.DataFrame
| Row | Date       | Open   | High   | Low    | Close  | Volume    |
|-----|------------|--------|--------|--------|--------|-----------|
| 1   | 2015-07-08 | 521.05 | 522.73 | 516.11 | 516.83 | 1.2967e6  |
| 2   | 2015-07-09 | 523.12 | 523.77 | 520.35 | 520.68 | 1.84235e6 |
| 3   | 2015-07-10 | 526.29 | 532.56 | 525.55 | 530.13 | 1.95668e6 |


or when I do:

julia> myid = "GOOG/NASDAQ_GOOG"
"GOOG/NASDAQ_GOOG"

julia> typeof(myid)
ASCIIString

julia> mydat = quandl(myid,rows=100,format="DataFrame")
100x6 DataFrames.DataFrame
| Row | Date       | Open   | High   | Low    | Close  | Volume    |
|-----|------------|--------|--------|--------|--------|-----------|
| 1   | 2015-07-08 | 521.05 | 522.73 | 516.11 | 516.83 | 1.2967e6  |
| 2   | 2015-07-09 | 523.12 | 523.77 | 520.35 | 520.68 | 1.84235e6 |
| 3   | 2015-07-10 | 526.29 | 532.56 | 525.55 | 530.13 | 1.95668e6 |


However, I get an error when I read my data from an external file. Assume I have an ascii file containing only one line:

$ echo "GOOG/NASDAQ_GOOG" > portfolio.txt

$ cat portfolio.txt
GOOG/NASDAQ_GOOG


I just read the content of this file by using readdlm and try to use it to call the same function quandl, but it does not work.

julia> myportfolio = readdlm("./portfolio.txt",'\n')
1x1 Array{Any,2}:
 "GOOG/NASDAQ_GOOG"

julia> typeof(myportfolio[1])
SubString{ASCIIString}

julia> mydat = quandl(myportfolio[1],rows=100,format="DataFrame")
ERROR: MethodError: `quandlget` has no method matching quandlget(::SubString{ASCIIString})


I suppose the easiest way to solve this problem is to convert my SubString{ASCIIString} variable to ASCIIString. Am I right here? How can I do it?

Does any of you have another suggestion? May be I could read my data in a different way instead of using readdlm?

Thanks for any tip!

best,

Charles
--
Um axé! :)

--
Charles Novaes de Santana, PhD
http://www.imedea.uib-csic.es/~charles

Seth

unread,
Dec 4, 2015, 5:53:12 PM12/4/15
to julia-users
Maybe this will work for you?

julia> a = "foobarbaz"
"foobarbaz"

julia> b = SubString(a,3,6)
"obar"

julia> typeof(b)
SubString{ASCIIString}

julia> c = ASCIIString(b)
"obar"

julia> typeof(c)
ASCIIString

Charles Novaes de Santana

unread,
Dec 4, 2015, 5:59:41 PM12/4/15
to julia...@googlegroups.com
wow! That simple! Yes it works!

Thanks, Seth!

Charles

Eric Forgy

unread,
Dec 4, 2015, 7:38:05 PM12/4/15
to julia-users
Hi Charles,

myportfolio is a Matrix, i.e. Array{Any,2}, so you need two indices to access it:

julia> myportfolio
1x1 Array{Any,2}:
 
"GOOG/NASDAQ_GOOD"

julia
> myportfolio[1,1]
"GOOG/NASDAQ_GOOD"

julia> typeof(myportfolio[1,1])
ASCIIString

Best regards,
Eric

Eric Forgy

unread,
Dec 4, 2015, 7:46:01 PM12/4/15
to julia-users
It's early and I didn't finish my first cup of coffee yet, so forgive the typo :)

julia> myportfolio
1x1 Array{Any,2}:

 
"GOOG/NASDAQ_GOOG"

julia
> myportfolio[1,1]
"GOOG/NASDAQ_GOOG"


julia
> typeof(myportfolio[1,1])
ASCIIString

Charles Novaes de Santana

unread,
Dec 4, 2015, 8:57:32 PM12/4/15
to julia...@googlegroups.com
Here it is late, so of course I forgive you the typo! :) But many thanks for the TIP! :)

However, it doesn't seem to work here. My problem was that my matrix was composed by SubString{ASCIIString}. So, if I call typeof(myportfolio[1]) I get:


        julia> typeof(myportfolio[1])
        SubString{ASCIIString}
 
And the same if I call typeof(myportfolio[1,1]):

        julia> typeof(myportfolio[1,1])
        SubString{ASCIIString}

Could you please send the result of typeof(myportfolio[1]) in your example? I suppose it would be an ASCIIString.

Seth's suggestion worked fine for me. To use a cast to ASCIIString to convert myportfolio[1], like this:

        julia> typeof(ASCIIString(myportfolio[1]))
        ASCIIString

But thanks anyway!

Best,

Charles

Eric Forgy

unread,
Dec 4, 2015, 9:58:16 PM12/4/15
to julia-users
Hi Charles,

Sorry for the bad advice :)

I think you might want to step back one step. This command:

julia> myportfolio = readdlm("./portfolio.txt",'\n')

might not be what you want. Look at 

julia> which(readdlm,(ASCIIString,Char))
readdlm
(input, dlm::Char) at datafmt.jl:37

It looks like you are making '\n' a delimiter, when it should probably just be the end of line (eol). You might try readcsv or maybe even readtable instead. I don't think you want to end up with a matrix of SubStrings. If you do, then Seth's solution is perfect :)

Milan Bouchet-Valat

unread,
Dec 5, 2015, 5:07:44 PM12/5/15
to julia...@googlegroups.com
Le vendredi 04 décembre 2015 à 23:47 +0100, Charles Novaes de Santana a
écrit :
Though the other posts give good solutions to your problem, you could
also file an issue against Quandl.jl to change quandlget() to accept
any AbstractString, and not only ASCIIString. That would be helpful in
legitimate cases.


Regards

Charles Novaes de Santana

unread,
Dec 6, 2015, 12:34:02 PM12/6/15
to julia...@googlegroups.com
Hi, thanks for all your suggestions!

@Eric: unfortunately readcsv and readtable give me the same situation as readdlm.

@Milan: thanks for the suggestion. Just created it: https://github.com/milktrader/Quandl.jl/issues/91

Best,

Charles

milktrader

unread,
Dec 7, 2015, 3:22:48 PM12/7/15
to julia-users
Can you post the first 10 lines or so of the file you'd like to parse?

Dan

milktrader

unread,
Dec 7, 2015, 3:56:03 PM12/7/15
to julia-users
Actually, I've closed the issue you created with a new commit so once 0.5.1 is available on METADATA you should get this resolved with a Pkg.update().
Reply all
Reply to author
Forward
0 new messages