gpt4 book ai didi

c# - 使用 C# StreamReader 解析文本文件

转载 作者:IT王子 更新时间:2023-10-29 06:31:59 28 4
gpt4 key购买 nike

我有一个文本文件,其中包含一些特定格式的赞美诗。示例如下。


1 Praise to the Lord<p></p>

<p>1
Praise to the Lord, the Almighty, the King of creation!
O my soul, praise Him, for He is thy health and salvation!
All ye who hear, now to His temple draw near;
Join ye in glad adoration!</p>

<p>2
Praise to the Lord, Who o'er all things so wondrously reigneth,
Shieldeth thee under His wings, yea, so gently sustaineth!
Hast thou not seen how thy desires e'er have been
Granted in what He ordaineth?</p>

<p>3
Praise to the Lord, who doth prosper thy work and defend thee;
Surely His goodness and mercy here daily attend thee.
Ponder anew what the Almighty can do,
If with His love He befriend thee.
</p>

我想提取这些赞美诗,将它们放入对象中,然后将它们插入到 SQLite 数据库中。我正在尝试将它们相应地分开,但到目前为止我还没有取得任何进展。这是我的尝试。

主要功能

        //Fileinfo object wraps the file path.
var hymns = new FileInfo(@"C:HymnWords.txt");

//StreamReader reads from the existing file.
var reader = hymns.OpenText();

string line;
int number = 0;
var hymns = new List<Hymn>();

var check = false; //this is set to indicate that all the lines that are follwoing will be apart of the hymn.

while ((line = reader.ReadLine())!=null)
{
if (line.Any(char.IsLetter) && line.Any(char.IsDigit))
{

}

if (check)
{
if (line.Any(c => char.IsDigit(c) && c != 0) && !line.Any(char.IsLetter))
{

}

}


}

赞美诗模型

public class Hymn
{
public string Name { set; get; }
public List<String> Verses { set; get; }
}

存储经文时。我需要保留换行符。正在插入一个

/n

在将经文插入对象或数据库之前的每一行之后,最好的方法是什么?

最佳答案

这应该能让你很好地开始:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Program
{
public class Class1
{
public class Program
{
public static void Main(string[] args)
{
var hymnFiles = new List<string>()
{
@"C:\HymnWords.txt",
@"C:\HymnWords1.txt",
@"C:\HymnWords2.txt",
@"C:\HymnWords3.txt",
};
var reader = new Class1();
foreach (var hymn in reader.ReadHymnFiles(hymnFiles))
{
Console.Out.WriteLine(hymn.Title);
foreach (var verse in hymn.Verses)
{
Console.Out.WriteLine(verse.VerseNumber);
foreach (var verseLine in verse.VerseLines)
{
Console.Out.WriteLine(verseLine);
}
}
}
Console.ReadLine();
}

}
public List<Hymn> ReadHymnFiles(List<string> hymnFiles)
{
var hymns = new List<Hymn>();
foreach (var hymnFile in hymnFiles)
{
using (TextReader reader = new StreamReader(hymnFile))
{
var hymn = new Hymn();
hymns.Add(hymn);
string line;
int currentVerseNumber = 0;
while ((line = reader.ReadLine()) != null)
{
if (string.IsNullOrWhiteSpace(line))
continue;

if (line.Any(char.IsLetter) && line.Any(char.IsDigit))
{
// this must be the title
hymn.Title = line;
continue;
}

if (line.Any(c => char.IsDigit(c) && c != 0) && !line.Any(char.IsLetter))
{
//this must be the verse number
currentVerseNumber = Convert.ToInt32(line.Trim());
hymn.Verses.Add(new Verse(currentVerseNumber));
continue;
}

//get the current verse to add the next line to it
var verse = hymn.Verses.Single(v => v.VerseNumber == currentVerseNumber);
verse.VerseLines.Add(line);
}
}
}
return hymns;
}

public class Hymn
{
public Hymn()
{
Verses = new List<Verse>();
}
public string Title { set; get; }
public List<Verse> Verses { set; get; }
}

public class Verse
{
public Verse(int verseNumber)
{
VerseNumber = verseNumber;
VerseLines = new List<string>();
}
public int VerseNumber { get; private set; }
public List<string> VerseLines { set; get; }
}
}

}

请注意,这些经文在它们自己的对象中,每一行都是它自己的行。它们可能也应该以这种方式存储。

关于c# - 使用 C# StreamReader 解析文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15591170/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com