gpt4 book ai didi

c# - 适配 csv 阅读器以读取 unicode 字符

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:02 27 4
gpt4 key购买 nike

我遇到了一个问题,csv 文件中的字符显示为带有 ?在中间。

我已经编写了解析 csv 的代码,但我不明白为什么字符串不能正确读取 unicode 字符。这可能与我的实现有关:

StreamReader readFile = new StreamReader(path)

try {
while ((line = readFile.ReadLine()) != null) {
string[] row = { "", "", "" };
int currentItem = 0;
bool inQuotes = false;
if (skippedFirst && currentItem != 3) {
for (int i = 0; i < line.Length; i++) {
if (!inQuotes) {
if (line[i] == '\"')
inQuotes = true;
else {
if (line[i] == ',')
currentItem++;
else
row[currentItem] += line[i];
}
} else {
if (line[i] == '\"')
inQuotes = false;
else
row[currentItem] += line[i];
}
}
parsedFile.Add(row);
}
skippedFirst = true;
}

最佳答案

打开文件时指定编码。

using (var sr = new StreamReader(@"c:\Temp\csvfile.csv", Encoding.UTF8)) {
}

您可能还想查看用于 CSV 解析的 Filehelpers:

https://www.filehelpers.net/quickstart/

关于c# - 适配 csv 阅读器以读取 unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11081154/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com