简体   繁体   English

不同的整数? C#

[英]Different integers? c#

I'm trying to read and extract specific information from several data files in which the filename format remains the same for every one, the format of my file is, XXXXXX_XXXXXX_PCPDTB_ODT.datafile where X is a random digit. 我正在尝试从几个数据文件中读取和提取特定信息,其中每个文件名的文件名格式都保持相同,我的文件格式为XXXXXX_XXXXXX_PCPDTB_ODT.datafile,其中X是一个随机数字。

It represents year, month, day in the first 6 digits and hours, minutes and seconds in the last 6 X's, so 131005_091429_PCPDTB_ODT.datafile would be 2013, 10th month, 5th day and so on, the _PCPDTB_ODT.datafile is always there. 它代表前6位数字的年,月,日,以及后6个X的小时,分​​钟和秒,因此131005_091429_PCPDTB_ODT.datafile将是2013年,第10个月,第5天,依此类推,_PCPDTB_ODT.datafile始终存在。

I'm able to gather my desired data (extracting all information after a certain keyword, in this case '#Footer' is my keyword) from a file successfully, but I'm not sure how I'd go about this with lots of files with several changing integers? 我能够从文件中成功收集所需的数据(在某个关键字之后提取所有信息,在这种情况下为“ #Footer”是我的关键字),但是我不确定如何处理很多有几个变化的整数的文件?

Here is my attempt (although it is terrible since I have very little experience of coding), but only seem to be to input 4 digits and no more. 这是我的尝试(尽管这很糟糕,因为我几乎没有编码经验),但似乎只能输入4位数字,仅此而已。 Which would only allow to access files like XXXX_PCPDTB_ODT.datafile or 1304_PCPDTB_ODT.datafile. 仅允许访问诸如XXXX_PCPDTB_ODT.datafile或1304_PCPDTB_ODT.datafile之类的文件。

static void Main(string[] args)
    {
        var path = @"C:\Users\#####\Desktop\";
        var ext = "_PCPDTB_ODT.datafile";
        var range = Enumerable.Range(0,9);
        var filePaths = 
                from i1 in range
                from i2 in range
                from i3 in range
                from i4 in range
                let file = path + i1 + i2 + i3 + i4 + ext
                where File.Exists(file)
                select File.ReadLines(file)
                    .SkipWhile(line => !line.Contains("#Footer"))
                    .Skip(1);
        try
        {
            Console.WriteLine(String.Join(Environment.NewLine,filePaths.SelectMany(f => f)));
        }
        catch (Exception e)
        {
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
            Console.Read();
        }
    }

I've attempted adding more i values, i5, i6, i7 etc. with "_" to space the first six digits from the last 6 but it doesn't seem to do anything when using the larger i values past i4. 我试图用“ _”添加更多i值,i5,i6,i7等,以将后6个数字的前6个数字隔开,但是当使用大于i4的较大i值时,它似乎没有任何作用。

Any ideas would help greatly, please keep in mind my coding is most likely rubbish since my knowledge is very little at the moment, thanks. 任何想法都会有很大帮助,请记住我的编码很可能是垃圾,因为目前我的知识还很少,谢谢。

If you're trying to process all of the files in a directory, use Directory.EnumerateFiles . 如果您尝试处理目录中的所有文件,请使用Directory.EnumerateFiles

Forexample: 例如:

foreach (var filename in Directory.EnumerateFiles(@"c:\mydata\", "*PCPDTB_ODT.data")
{
    var lines = File.ReadLines(filename)
               .SkipWhile(line => !line.Contains("#Footer"))
               .Skip(1);
    // add the rest of your code . . .
}

Instead of trying to loop through every possible valid file name, you should just see what files are there. 与其尝试遍历所有可能的有效文件名,不如仅查看其中存在哪些文件。 Here's an example using Directory.GetFiles 这是使用Directory.GetFiles的示例

var filePaths = Directory.GetFiles(path, "*" + ext)
                  .Select(file => File.ReadLines(file)
                    .SkipWhile(line => !line.Contains("#Footer"))
                    .Skip(1));

If you need the date/time also, you can parse that out, too, with DateTime.ParseExact . 如果还需要日期/时间,则也可以使用DateTime.ParseExact将其解析出来。

it doesn't seem to do anything when using the larger i values past i4 当使用大于i4的较大i值时,它似乎没有任何作用

Proavbly because it's having to iterate through 1,000,000,000,000 different filenames? 大概是因为它必须遍历1,000,000,000,000个不同的文件名吗?

Why not just get a list of files that match the pattern? 为什么不仅仅获得与模式匹配的文件列表?

    var path = @"C:\Users\#####\Desktop\";
    var pattern = "??????_??????_PCPDTB_ODT.datafile";

     var filePaths = Directory.GetFiles(path, pattern)
                              .Select(file => File.ReadLines(file)
                              .SkipWhile(line => !line.Contains("#Footer"))
                              .Skip(1));

Try this 尝试这个

using System;

public class Test
{
public static void Main()
{
    string str = "131005_091429_PCPDTB_ODT.datafile ";
    int[] date = new int[3];
    int[] time = new int[3];

    string[] arr = str.Split('_');
    for(int i = 0;i<6;i=i+2)
    {
        date[i/2]=Convert.ToInt32(arr[0].Substring(i,2));
    }

    for(int i = 0;i<6;i=i+2)
    {
        time[i/2]=Convert.ToInt32(arr[1].Substring(i,2));
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM