I use the hsparql library to run a query that returns german text; and thus iso-8859-1 special characters are returned.
I wrote the result of the query to a file using writeFile, but the special characters are not correctly shown. (when viewing the file with emacs)
When I instead write the output of the show function to a file, I get the following output:
["B\195\188ro", ...]
Printing out the special character it would mean: ["Büro", ....]
How can I write special characters correctly to a file? (e.g. “Büro” is correctly show in the file output.)
EDIT:
I know that show writes the escaped characters. Using writeFile directly doesn’t work, I have to check the link given in hammer’s answer to find a fix..
EDIT2:
removed, was the wrong approach.
EDIT3:
Hammers answer was right on the point. It took only 10 minutes to find the solution, but I needed to be fit and concentrated.
I looked up IO in link
The solution was (literate haskell):
> writeAllLabels = do
Running my Query (not shown, accesses the RDF TrippleStore):
> res <- (selectStr33 (unlines qAllLabels))
> outh <- openFile "/tmp/haskell_output.txt" WriteMode
this is the important line. If I would write "utf8" her instead of "latin1", I would get the wrong result again, i.e. as before asking the question...
> hSetEncoding outh latin1
> hPutStrLn outh res
> hClose outh
Don’t use
showif you don’t want things escaped. It’s meant for lightweight serialization and will escape a number of special characters as well as characters outside the ASCII range. If you usewriteFiledirectly, it should work with the default encoding for your current locale.For more fine-grained control over encodings, see the System.IO documentation.