I have been a number of files, each contains one list of integers. I would just like to read in the contents into an array in Julia. Here is an example file:
What is that format supposed to be? How is it produced?
If you know what format it is supposed to be in then use a reader for that format. If you don’t know then it seems to be json compatible and I’ll just read it as json. In any case, never use include for this.
The lists look exactly like those produced from Python but I think they in fact came from the output of a piece of software called Gap. In any case, I need to read in the files as they are given to me. The main problem is just the carriage returns I think.
Well, readdlm clearly won’t work, and the new line isn’t the problem. You must first identify what format you are dealing with. If you don’t know or if the producer doesn’t specify, then as I said json should work. In any case, just don’t use include.
I agree with this, even though I proposed the above “solution”. If you’re using this for anything more than a toy, do find a saner way to parse and do input verification.
I think one way in this case is to just input the entire thing as a single string and parse the string yourself. Probably not too bad because you can just split it into chunks by commas, discard the brackets, trim whitespace, and then parse to integer. Some regex wiz can probably do it in 2-3 lines of code.
Thanks. Now I see that JSON.parsefile can’t cope with the large integers. It gives 0 for all the numbers which are too big. This is even before attempting to convert the vector. This file shows the problem Ubuntu Pastebin . Maybe I should ask a separate question for this aspect?
Ah, I didn’t look at the sample file and didn’t notice you needed that big numbers. It probably makes sense to write your own little parser function then like
function read_data(s)
joined = join(split(s))
no_brackets = SubString(joined, nextind(joined, 1), prevind(joined, lastindex(joined)))
return parse.(BigInt, split(no_brackets, ','))
end