简体   繁体   English

无论我使用哪种编码,逐行读取希伯来语文本文件都会显示乱码,为什么?

[英]Reading line-by-line Hebrew text file shows gibberish no matter which encoding I use, why?

I made the site Text-Files-Oriented. 我制作了“面向文本文件”的网站。 The site is in Hebrew, using Razor Pages, Asp.Net Core 2. 该站点位于希伯来语,使用Razor Pages,Asp.Net Core 2。
Environment: Visual Studio 2017 with all updates. 环境:Visual Studio 2017及其所有更新。

In _Layout file I have: _Layout文件中,我有:

<meta charset="utf-8" />
<meta lang="he" dir="rtl" />

also, in site.css : 另外,在site.css

body {
    background-color:black;

    padding-top: 50px;
    padding-bottom: 20px;
    direction:rtl; /*right to left*/
    font-family: 'opensanshebrew'; /*defined above it*/
    font-size:16px;
}

In a razor page Poems , I want to simply show the first line of every txt file in "Poems" folder in wwwroot . 在剃刀页Poems ,我只想显示wwwroot “ Poems”文件夹中每个txt文件的第一行。 and it goes like this: 它是这样的:

<div class="row">
    <div id="fileListArea" class="col-lg-8">
        <h2>רשימת השירים שכתבתי:</h2>

        @foreach (var p in Model.PoemsList)
        {
            <span>@p.Title</span><br />
        }
    </div>
</div>

[I'll put it on a grid later] [我稍后将其放在网格中]

in code behind: 在后面的代码中:

public void OnGet()
{
    string tpath = _env.WebRootPath + "\\Poems";
    Filelist = fileTools.GetFileList(tpath);
    PoemsList = new List<PoemCover>();
    foreach(string fn in Filelist)
    {
        PoemsList.Add(new PoemCover(fileTools.GetTitle(tpath + "\\" + fn, Encoding.ASCII), fn));
    }
}

in fileTools fileTools

public static string GetTitle(string pathWfilename,Encoding encd)
{
    string rslt;

    try
    {
        using (StreamReader strm = new StreamReader(pathWfilename, encd))
        {
            string nextLine;
            rslt = strm.ReadLine();
            nextLine = strm.ReadLine();

            if (nextLine != null)
                if (nextLine.Length >= 2)
                {
                    int didx = NthOccurence(rslt, ' ', 3);
                    if (didx < 2)
                    { rslt = (rslt.Substring(0, rslt.Length - 1)) + "..."; }
                    else { rslt = (rslt.Substring(0, didx)) + "..."; }
                }
        }
    }
    catch(IOException ex)
    {
        rslt = "Error reading Title from - " + pathWfilename + " - " + ex.Message;
        Console.WriteLine("{0}", rslt);
    }

    return rslt;
}

It works but the lines are gibberish... 它有效,但线条乱七八糟。。。
I've tried: 我试过了:

fileTools.GetTitle(tpath + "\\" + fn, Encoding.ASCII)
fileTools.GetTitle(tpath + "\\" + fn, Encoding.Unicode) 
fileTools.GetTitle(tpath + "\\" + fn, Encoding.UTF8)
fileTools.GetTitle(tpath + "\\" + fn, Encoding.UTF7) 
fileTools.GetTitle(tpath + "\\" + fn, Encoding.UTF32) 
fileTools.GetTitle(tpath + "\\" + fn, Encoding.GetEncoding("Windows-1255")) 
//which gives error of no such encoding

Some show gibberish, some shows different kinds of question marks. 有些显示乱码,有些显示不同类型的问号。 One shows some weird fonts... 一个显示了一些奇怪的字体...

How can I Read Hebrew text files? 如何阅读希伯来语文本文件?

I have no knowledge of Hebrew language but I found a string in google to work with for testing, so here it comes: 我不知道希伯来语,但是我在Google中找到一个字符串用于测试,所以它来了:

TL;TR: TL; TR:

 GetTitle(@"C:\dataUpload\test.txt",Encoding.GetEncoding("windows-1255")) ; 

print of my test: 我的测试打印:

在此处输入图片说明

I used your GetTitle method just made it simpler to serve my tests. 我使用GetTitle方法使服务测试变得更加简单。

and my test.txt file looks like this: 我的test.txt文件如下所示:

גליון_1 גליון_1

Take a note that "windows-1255" in GetEncoding starts with NON-CAPITAL letter!! 请注意,GetEncoding中的“ windows-1255”以NON-CAPITAL字母开头!

Good luck with your progress and feel free to contact me for any information. 祝您一切顺利,请随时与我联系以获取任何信息。

PS. PS。 I dont understand Hebrew so in case my answer is off provide me some Hebrew strings and the expected output to work with. 我不了解希伯来语,因此如果我的答案不正确,请提供一些希伯来语字符串和预期的输出。 Also check with what encoding you have saved your txt files. 还要检查您保存了txt文件的编码方式。 I saved my .txt file as UTF-8 and now Encoding.UFT8 works too.... 我将.txt文件保存为UTF-8,现在Encoding.UFT8也可以使用。...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM