[英]CSV file. contain 2 different values in a row. How to separate them?
I have a file from mass spectrum(chemistry).我有一个来自质谱(化学)的文件。 In that file there are 2 different values for each axis like x and y.
在该文件中,每个轴都有 2 个不同的值,例如 x 和 y。 but both of them are in same row.
但他们都在同一行。 so how to separate them.
那么如何将它们分开。 150-2000 m/z, 123,53,54,23,35,56,68,89,90,etc I know i can separate the values by comma into column but how to change the value 150-2000 into numbers and separate column like that 150 151 152 153. .
150-2000 m/z、123、53、54、23、35、56、68、89、90 等我知道我可以用逗号将值分隔到列中但是如何将值 150-2000 更改为数字并分隔像这样的列 150 151 152 153.. .
. .
. .
. 1999 2000
1999 2000
Thankx in advance提前谢谢
There are 2 different set of values in a row from a CSV file and I want to separate them into different columns.来自 CSV 文件的一行中有 2 组不同的值,我想将它们分成不同的列。 And one of the file is in 150-2000 range so how to convert it into simple numbers.
其中一个文件在 150-2000 范围内,因此如何将其转换为简单数字。
You could use Convert
in a ClassMap
to convert the first column into your x
and y
values.您可以在
ClassMap
中使用Convert
将第一列转换为您的x
和y
值。
void Main()
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = false
};
using (var reader = new StringReader("150-2000 m/z,123"))
using (var csv = new CsvReader(reader, config))
{
csv.Context.RegisterClassMap<FooMap>();
var records = csv.GetRecords<Foo>().Dump();
}
}
// You can define other methods, fields, classes and namespaces here
public class FooMap : ClassMap<Foo>
{
FooMap()
{
Map(m => m.X).Convert(args =>
{
var value = args.Row.GetField(0);
var beforeDash = value.Split("-")[0];
var canParse = int.TryParse(beforeDash, out int result);
if (canParse)
{
return result;
}
else
{
return 0;
}
});
Map(m => m.Y).Convert(args =>
{
var value = args.Row.GetField(0);
var afterDash = value.Split("-")[1];
var beforeSpace = afterDash.Split(" ")[0];
var canParse = int.TryParse(beforeSpace, out int result);
if (canParse)
{
return result;
}
else
{
return 0;
}
});
Map(m => m.AnotherNumber).Index(1);
}
}
public class Foo
{
public int X { get; set; }
public int Y { get; set; }
public int AnotherNumber { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.