简体   繁体   English

CSV 文件。 连续包含 2 个不同的值。 如何将它们分开?

[英]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将第一列转换为您的xy值。

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.

相关问题 如何跳过解析不同格式的行。 Fasterxml csv解析 - How to skip parsing different format row. Fasterxml csv parsing 解析一个非常大的CSV文件。 需要将一个字段分成许多较小的行并在每行中保留ID。 - Parsing a very large CSV file. Need to break apart one field into lots of smaller rows & keep ID in each row. 无法将列表的所有值写入 CSV 行。 值从页面中提取并存储在变量中但在文件中丢失 - Unable to write all values of list to CSV row. values are extracting from page and store in variables but missing in File 如何匹配 x 和 y 值,然后用逗号分隔它们,并将输出转到新的 csv 文件? - How to match up x and y values then separate them by a comma, and have the output go to a new csv file? 如何将csv行导出到单独的.txt文件中 - How to export csv row into separate .txt file 如何创建日志`csv`文件。 其中包含与链接相对应的文件名 - How to create log `csv` file. Which contain a filename corresponding with link 读取csv文件。 未检测到不同的变量 - reading csv file. different variables not detected 如何在不同目录中搜索相同模式的 .csv 文件,并将它们组合成一个 csv 文件,并在 shell 脚本中使用单行标题 - How to search same pattern .csv files in different directory and combine them into one csv file with single row of header in shell script 如何使用PowerShell基于包含CSV文件中同一行中的多个值的单个单元格将一行分成多行 - How to separate one row into multiple rows based on a single cell containing multiple values in the same row in a CSV file using PowerShell Python - 从 csv 中选择不同的行值并将它们组合到新的 csv 中 - Python - Select different row values from csv and combine them in new csv
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM