# Recommended way to open a text file with unknown encoding in v0.7

**URL:** <https://discourse.julialang.org/t/recommended-way-to-open-a-text-file-with-unknown-encoding-in-v0-7/12030>\
**Category:** General Usage\
**Created:** [June 28, 2018, 12:42pm UTC](https://discourse.julialang.org/t/recommended-way-to-open-a-text-file-with-unknown-encoding-in-v0-7/12030 "2018-06-28T12:42:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cstook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstook/32/3731_2.png) [@cstook](https://discourse.julialang.org/u/cstook)\
**Post date:** [June 28, 2018, 12:42pm UTC](https://discourse.julialang.org/t/recommended-way-to-open-a-text-file-with-unknown-encoding-in-v0-7/12030/1 "2018-06-28T12:42:23Z")

</div>

I need to open a text file which can be encoded either UTF-16LE or UTF-8. The first line of text is known.

The following worked on v0.6, but now throws a StringIndexError on the `if firstline[1:8] == "Circuit:"` line.

```julia

"""
    PossibleEncodings

**fields**
- `encodings` -- Array of encodings to try
- `iscorrectencoding` -- callable object
- `lastcorrectencoding` -- index of correct encoding last time open was called
"""
mutable struct PossibleEncodings
  encodings :: Array{StringEncodings.Encodings.Encoding,1}
  iscorrectencoding
  lastcorrectencoding :: Int
  io :: IO
  PossibleEncodings(enc,ice) = new(enc,ice,0,IOStream(""))
end

function iscorrectencoding_logfile(io)
  firstline = readline(io)
  if firstline[1:8] == "Circuit:"
    seekstart(io.stream) # good idea?
    return true
  else
    return false
  end
end

function tryopen!(fname::AbstractString, enc::PossibleEncodings, i)
  try_io = try open(fname,enc.encodings[i]) end
  if try_io!=nothing
    if enc.iscorrectencoding(try_io)
      enc.io = try_io
      return true
    else
      close(try_io.stream) # ???
    end
  end
  return false
end

function Base.open(fname::AbstractString, enc::PossibleEncodings)
  if enc.lastcorrectencoding != 0 &&
     tryopen!(fname,enc,enc.lastcorrectencoding)
    return enc.io
  end
  for i in eachindex(enc.encodings)
    if i!=enc.lastcorrectencoding && tryopen!(fname,enc,i)
      enc.lastcorrectencoding = i
      return enc.io
    end
  end
  throw(ErrorException("no valid encoding found for $fname"))
end

```

I’d like to rewrite this section. What is the best way?
