簡體   English   中英

C# 中的西班牙語文件的編碼問題

[英]Encoding issue with spanish file in C#

我在西班牙語的 azure blob 存儲中有一個在線文件存儲。 有些單詞有特殊字符(例如:Almacén) 當我在 notepad++ 中打開文件時,編碼是 ANSI。

所以現在我嘗試使用代碼讀取文件:

        using StreamReader reader = new StreamReader(Stream, Encoding.UTF8);
        blobStream.Seek(0, SeekOrigin.Begin);
        var allLines = await reader.ReadToEndAsync();

問題是“allLines”不是正確的編碼,我有一些問題,例如:Almac�n

我嘗試了一些類似的解決方案: C# Convert string from UTF-8 to ISO-8859-1 (Latin1) H

但仍然無法正常工作

(the final goal is to "merge" two csv so I read the stream of both, remove the header and concatenate the string to push it again. If there is a better solution to merge csv in c# that can skip this encoding issue I am也對它開放)

您正在嘗試讀取非 UTF8 編碼的文件,就好像它是 UTF8 編碼的一樣。 我可以復制這個問題

var s = "Almacén";
using var memStream = new MemoryStream(Encoding.GetEncoding(28591).GetBytes(s));

using var reader = new StreamReader(memStream, Encoding.UTF8);
var allLines = await reader.ReadToEndAsync();

Console.WriteLine(allLines); // writes "Almac�n" to console

您應該嘗試使用編碼為 iso-8859-1 "Western European (ISO)" 的文件來讀取文件,即代碼頁 28591。

using var reader = new StreamReader(Stream, Encoding.GetEncoding(28591));
var allLines = await reader.ReadToEndAsync();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM