从文件中读取特殊字符时出现问题 [英] Problems reading special characters from a file

查看:91
本文介绍了从文件中读取特殊字符时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我正在尝试读取包含地址列表的文件。这些

地址中的大多数都位于德国,因此有很多名称具有像慕尼黑这样的特殊字符和永远存在的Stra?e。


代码如下:

FileStream fs = new FileStream(fi.FullName,FileMode.Open);

StreamReader sr = new StreamReader(fs,Encoding.Default);


如你所见,我在这里使用Encoding.Default - 我已经尝试了所有

可通过编码获得的编码,但没有一个

按预期返回文本。


还有其他方法可以读取文件吗?我可以更改什么以确保仍然可以读取和处理这些

特殊字符?


提前致谢,


JayCee

-
http://jcsnippets.atspace .com

a收集源代码,提示和技巧

解决方案

如果这是一个常规非unicode文本文件,它可能使用拉丁语1

编码,又名ISO-8859-1,又名Windows-1252。


我认为你可以创建一个编码对象使用:new

编码(1252)

jcsnippets.atspace.com写道:

大家好,

我正在尝试读取包含地址列表的文件。这些
地址大部分位于德国,因此有许多名称具有像慕尼黑这样的特殊字符以及永远存在的Stra?e。

代码如下:
FileStream fs = new FileStream(fi.FullName,FileMode.Open);
StreamReader sr = new StreamReader(fs,Encoding.Default);

正如你所看到的,我在这里使用Encoding.Default - 我已经尝试了所有可通过编码获得的编码,但没有一个
按预期返回文本。

还有另一种方法来读取文件吗?我可以更改哪些内容以确保仍然可以阅读和处理这些
特殊字符?

提前致谢,

JayCee



G?ran Andersson< gu *** @ guffa.com>写在

新闻:#E ************** @ TK2MSFTNGP02.phx.gbl:

如果这是一个常规的非unicode文本文件,它可能使用拉丁文
1编码,又名ISO-8859-1,又名Windows-1252。

我认为你可以创建一个编码对象这个使用:新
编码(1252)




嗨G?跑,


感谢您的解释 - 我已经看过使用

编码的可能性,但是没有可用的构造函数。使用WinCV,我已经看过这个类的替代品了,但我似乎找不到任何替代品。


文本本身确实是常规的非unicode文本文件,所以您建议的代码页

应该是好的。现在我只需要找到一种实际的方法

指定这个编码...


祝你好运,


JayCee

-
http://jcsnippets.atspace。 com

a收集源代码,提示和技巧


jcsnippets.atspace.com写道:


[... snip ...]

感谢您的解释 - 我已经看过使用
编码的可能性,但是没有构造函数可用。使用WinCV,我还
看了这个类的替代品,但我似乎无法找到任何。



[... snip ...]


尝试


编码enc = Encoding.GetEncoding(" windows-1252");





编码enc = Encoding.GetEncoding(1252);


HTH

Michael


Hi everybody,

I''m trying to read a file containing a list of addresses. Most of these
addresses are based in Germany, so there are a lot of names which have
special characters like München and the ever-present "Stra?e".

The code is as follows:
FileStream fs = new FileStream(fi.FullName, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);

As you can see, I''m using Encoding.Default here - I have tried all
available encodings that are available through Encoding, but none of them
returns the text as expected.

Is there another way to read the file? What can I change to make sure these
special characters are still read and processed?

Thanks in advance,

JayCee
--
http://jcsnippets.atspace.com
a collection of source code, tips and tricks

解决方案

If this is a regular non-unicode text file, it probably uses the Latin 1
encoding, a.k.a. ISO-8859-1, a.k.a. Windows-1252.

I think that you can create an Encoding object for this using: new
Encoding(1252)

jcsnippets.atspace.com wrote:

Hi everybody,

I''m trying to read a file containing a list of addresses. Most of these
addresses are based in Germany, so there are a lot of names which have
special characters like München and the ever-present "Stra?e".

The code is as follows:
FileStream fs = new FileStream(fi.FullName, FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Default);

As you can see, I''m using Encoding.Default here - I have tried all
available encodings that are available through Encoding, but none of them
returns the text as expected.

Is there another way to read the file? What can I change to make sure these
special characters are still read and processed?

Thanks in advance,

JayCee



G?ran Andersson <gu***@guffa.com> wrote in
news:#E**************@TK2MSFTNGP02.phx.gbl:

If this is a regular non-unicode text file, it probably uses the Latin
1 encoding, a.k.a. ISO-8859-1, a.k.a. Windows-1252.

I think that you can create an Encoding object for this using: new
Encoding(1252)



Hi G?ran,

Thanks for the explanation - I''ve had a look at the possibility to use
Encoding, but there is no constructor available. Using WinCV, I''ve also had
a look at alternatives for this class, but I cannot seem to find any.

The text itself is indeed a regular non-unicode text file, so the codepages
you suggested should be allright. Now I only need to find a way to actually
specify this encoding...

Best regards,

JayCee
--
http://jcsnippets.atspace.com
a collection of source code, tips and tricks


jcsnippets.atspace.com wrote:

[...snip...]

Thanks for the explanation - I''ve had a look at the possibility to use
Encoding, but there is no constructor available. Using WinCV, I''ve also had a look at alternatives for this class, but I cannot seem to find any.


[...snip...]

Try

Encoding enc =Encoding.GetEncoding("windows-1252");

or

Encoding enc = Encoding.GetEncoding(1252);

HTH
Michael


这篇关于从文件中读取特殊字符时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆