读取文本文件并在达到一定长度后获取值/数据。 [英] Read a text file and get the value/data after a certain length is reached.

查看:38
本文介绍了读取文本文件并在达到一定长度后获取值/数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

请协助。我在一个文件夹中有超过100个文本文件。每个文本文件包含超过一百万个文本。对于每个文件,我想读取数据并在字符位置或每个字符的计数为1200时拉出字符串。我几乎已经完成了这个项目,但是我无法理解这一点,所以我80%通过我的项目。请协助代码。



E.g澄清。

说我在文件中有以下文字。我希望在达到10的计数/长度时得到字符串。

Peter192019_Test_File1088-202000001009289271

130000001230WINALITE TST-TEHA S GHAGHSDGAHT

HGHAGSHGHAGSH HAGSHJAGDJGAHFDTFH EFAFGDFAG



所以从上面我的系统将拉弦如下:

Peter19201

9_Test_Fil

e1088-2020

0000100928

9271130000

等..

请注意,对于第五个字符串,接下来的6个字符来自第二行。不一定是6但它将取决于制作下一个10个字符所需的字符。



请协助如何做到这一点。

提前感谢您的任何帮助。

Hello,
Please assist. I have more than 100 text files in a folder. Each of the text file contains more than a million texts. For each file I want to read the data and pull out the strings when the character position or the count of each character is 1200th. I have almost completed this project but couldn''t get the hang of this so I''m 80% through my project. Please assist with the code for this.

E.g to clarify.
Say I have the following texts in the file. I want to get the string when a count/length of 10 is reached.
Peter192019_Test_File1088-202000001009289271
130000001230WINALITE TST-TEHA S GHAGHSDGAHT
HGHAGSHGHAGSH HAGSHJAGDJGAHFDTFH EFAFGDFAG

So from the above my system will pull the strings as follows:
Peter19201
9_Test_Fil
e1088-2020
0000100928
9271130000
etc..
Note that for the fifth string above the next 6 characters are from teh 2nd line. Not necessarily would be 6 but it will depend on the characters required to make the next 10 charactered word.

Please assist as to how to do this.
Thanks in advance for any assistance.

推荐答案

我不确定我是否完全理解您的目标,但假设它是要解析文本文件到固定长度的字符串,这样的东西可能对你有用。



此代码创建一个包含文本的内存流,用于模拟从文本文件中读取。



I am not sure that I fully understand you goals, but assuming that that it is to parse a text file into strings of a fixed length, then something like this may work for you.

This code creates a memorystream containing text that is used to simulate reading from a text file.

Private ms As New IO.MemoryStream
Private strings As New List(Of String)
Sub test()
   makedata() '' makes a file stream to simulate reading from a text file
   MakeStrings() '' extract strings of a set length
   For Each s As String In strings
      Console.WriteLine(s)
   Next
End Sub

Private Sub makedata()
   Dim sw As New IO.StreamWriter(ms)
   With sw
      .Write("0123456789")
      .Write("0123456789")
      .Write("0123456789")
      .Write("0123456789")
      .WriteLine("0123")
      .Write("456789")
      .Write("0123456789")
      .Write("0123456789")
      .Write("0123456789")
      .Write("0123456789")
      .Write("0123456789")
      .Write("01234567")
      .WriteLine("89")
      .Write("0123456789")
      .Write("0123456789")
      .Write("0123456789")
      .Write("0123456789")
      .Write("0123456789")
      .Write("0123456789")
      .Write("0123456789")
      .WriteLine("01")
      .Flush() '' force writing out to stream
   End With
End Sub

Private Sub MakeStrings()

   Const strlen As Int32 = 10 '' length ofstrings to create
   ms.Position = 0

   Dim sr As New IO.StreamReader(ms)

   Dim sb As New System.Text.StringBuilder(strlen) '' temporary string storage
   Dim lineposition As Int32

   Do While sr.Peek <> -1
      lineposition = 0
      Dim line() As Char = sr.ReadLine.ToCharArray()
      Do While lineposition < line.Length
         If sb.Length < strlen Then
            sb.Append(line(lineposition))
            lineposition += 1
         Else
            strings.Add(sb.ToString())
            Debug.WriteLine(sb.ToString)

            sb.Length = 0
         End If
      Loop
   Loop

   If sb.Length > 0 Then strings.Add(sb.ToString) '' this will be a partial string
   sr.Close()

End Sub


这篇关于读取文本文件并在达到一定长度后获取值/数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆