开发者

Haskell IO russian symbols

开发者 https://www.devze.com 2022-12-30 22:56 出处:网络
I an trying to process a file which contains russian symbols. When reading and after writing some text to the file I get something like:

I an trying to process a file which contains russian symbols. When reading and after writing some text to the file I get something like:

\160\192\231\229\240\22开发者_JAVA百科5\224\233\228\230\224\237

How can I get normal symbols?


If you are getting strings with backslashes and numbers in, then it sounds like you might be calling "print" when you want to call "putStr".


If you deal with Unicode, you might try utf8-string package

import System.IO hiding (hPutStr, hPutStrLn, hGetLine, hGetContents, putStrLn)
import System.IO.UTF8
import Codec.Binary.UTF8.String (utf8Encode)
main = System.IO.UTF8.putStrLn "Вася Пупкин"

However it didn't work well in my windows CLI garbling the output because of codepage. I expect it to work fine on other Unix-like systems if your locale is set correctly. However writing to file should be successfull on all systems.

UPDATE:

An example on encoding package usage.


I have got success.

{-# LANGUAGE ImplicitParams #-}

import Network.HTTP
import Text.HTML.TagSoup
import Data.Encoding
import Data.Encoding.CP1251
import Data.Encoding.UTF8

openURL x =  do 
        x <- simpleHTTP (getRequest x)
        fmap (decodeString CP1251) (getResponseBody x)

main :: IO ()
main = do
    tags <- fmap parseTags $ openURL "http://www.trade.su/search?ext=1"
    let TagText r  = partitions (~== "<input type=checkbox>") tags !! 1 !! 4
    appendFile "out" r
0

精彩评论

暂无评论...
验证码 换一张
取 消