[英]Peak detection(to find distance between two peaks) from the chart plotted in c# from a CSV data file
[英]C# to find data from CSV in a file
我有一行值作為CSV文件,我需要查找每個值是否都存在於其他文件(就此而言是XML)中。
我已經閱讀了CSV數據,拆分了值,搜索代碼有效,但輸出了錯誤的結果(“查找”不存在的值),我會尋求幫助。 謝謝沙比
我的密碼
string csvFile = @"Afile.csv";
string[] lines = System.IO.File.ReadAllLines(csvFile);
string namesValues = lines[0];
List<string> allValidNamesValues = namesValues.Split(',').ToList();
bool result = false;
string DeviceName = Phone.Tag("Name").Value();
if (allValidNamesValues.Any(vn => DeviceName.IndexOf(vn, StringComparison.OrdinalIgnoreCase) != -1))
{
Info = string.Format("The Device is supported");
return true;
}
return result;
我建議您真的想要這樣的事情:
實施
string DeviceName = Phone.Tag("Name").Value();
string csvFile = @"Afile.csv";
var result = File
.ReadLines(csvFile) // You have no need to read all the lines
.Skip(1) // skip title, comment out this if CSV has no caption
.Select(line => line.Split(',')) // providing that CSV doen't have quotations
.Select(items => items[0]) // providing that 1ast column contains names
.Any(name => String.Equals(name, DeviceName, StringComparison.OrdinalIgnoreCase));
我猜這里的主要問題是您只在處理文件的第一行( lines[0]
)。 您可以使用嵌套循環來處理所有行,也可以使用SelectMany
(LINQ)。 例如(並使用ReadLines
避免一次緩沖所有數據):
static readonly char[] comma = { ',' };
...
if(File.ReadLines(csvFile).SelectMany(x => x.Split(comma)).Any(
vn => DeviceName.IndexOf(vn, StringComparison.OrdinalIgnoreCase) != -1))
{
// whatever
}
請注意,我不相信字符串測試是正確的(對我來說,擁有vn.IndexOf(DeviceName, ...)
似乎更明顯,但是:沒有示例很難檢查它。
對CSV進行更正確的解析更加復雜; 你應該考慮像LumenWorksCsvReader這樣的庫
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.