简体   繁体   English

如何使用C#从.asc文件中的特定位置读取数据

[英]How can I read data from specific location in .asc file using c#

I have .asc file which has 1000's of rows. 我有.asc文件,其中有1000行。 Each column in row is of the fixed length and separate by one space. 行中的每一列均具有固定长度,并以一个空格分隔。 I want to read email id column which is started from 296 position and ended at 326 position in a row. 我想读取电子邮件ID列,该列从296个位置开始并连续在326个位置结束。

Is there any way to read such data from .asc file? 有什么方法可以从.asc文件中读取此类数据?

This might do the trick for you. 这可能会帮到您。 I am just reading the email ids in the file whatever extension file it might me, may it be txt or asc. 我只是在读取文件中的电子邮件ID,无论它是我的扩展名文件,可能是txt或asc。 Also it doesnt matter if the email address is locating at some other place instead of 296 or 326. 同样,电子邮件地址是否位于其他位置而不是296或326也无关紧要。

public void ExtractAllEmails()
{
    string datafrmAsc = File.ReadAllText(YourASCFile); //read File 
    Regex emailRegex = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", RegexOptions.IgnoreCase);
    MatchCollection emailMatches = emailRegex.Matches(datafrmAsc);
    StringBuilder sb = new StringBuilder();
    foreach (Match emailMatch in emailMatches)
    {
        sb.AppendLine(emailMatch.Value);
    }
    File.WriteAllText(SomeTxtFile, sb.ToString());
}

Assuming that is large text file, you can do something like this: 假设这是大型文本文件,则可以执行以下操作:

        List<string> emailsList = new List<string>();
        int startIndex = 295;
        int endIndex = 325;

        using (FileStream stream = File.Open("c:\\test.asc", FileMode.Open))
        using (StreamReader sr = new StreamReader(stream))
        {
                string line = string.Empty;
                while ((line = sr.ReadLine()) != null)
                {
                    emailsList.Add(line.Substring(startIndex, endIndex - startIndex).Trim());
                }

         }

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

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