C# 读取txt文件并获取特定文本
读取*.txt文件并获取文本的特定区域的最佳做法是什么 我的*.txt文件如下所示:C# 读取txt文件并获取特定文本,c#,asp.net,C#,Asp.net,读取*.txt文件并获取文本的特定区域的最佳做法是什么 我的*.txt文件如下所示: [Product code] MYPRODUCT-CODE123 [List price] 28.10 [Price] 20.30 [Weight] 10 [Quantity] 1 [Min quantity] 1 [Shipping freight] N [Free shipping] N [Product name] My product name 目前我正在读取txt文件,如下所示:
[Product code]
MYPRODUCT-CODE123
[List price]
28.10
[Price]
20.30
[Weight]
10
[Quantity]
1
[Min quantity]
1
[Shipping freight]
N
[Free shipping]
N
[Product name]
My product name
目前我正在读取txt文件,如下所示:
String[] allfiles = System.IO.Directory.GetFiles(_path, "*.txt", System.IO.SearchOption.AllDirectories);
foreach (string filePath in allfiles) {
using (StreamReader sr = File.OpenText(filePath))
{
string s = sr.ReadToEnd();
}
}
Key=[Quantity]; Value=1
我如何从我的txt文件中获得[Product code]附近的文本,等等其他“关键术语”因此您有了字符串
s
。让我们从那里开始
在新行拆分,将对放入字典,获取项目:
var lines = s.Split(
new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries)
.ToArray();
// pairing thanks to http://stackoverflow.com/questions/1624341/
var dictionary = lines.Where((x, i) => i < lines.Length)
.Select((x, i) =>
new KeyValuePair<string, string>(
x.Trim('[', ']'), // get rid of brackets
lines[i + 1]))
.ToDictionary(x => x.Key, x => x.Value);
var productCode = dictionary["Product code"];
var行=s.拆分(
新建[]{Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries)
.ToArray();
//配对感谢http://stackoverflow.com/questions/1624341/
变量字典=行。其中((x,i)=>i
新的KeyValuePair(
x、 修剪(“[”,“]”),//去掉括号
行[i+1]))
.ToDictionary(x=>x.Key,x=>x.Value);
var productCode=字典[“产品代码”];
所以你有你的字符串s
。让我们从那里开始
在新行拆分,将对放入字典,获取项目:
var lines = s.Split(
new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries)
.ToArray();
// pairing thanks to http://stackoverflow.com/questions/1624341/
var dictionary = lines.Where((x, i) => i < lines.Length)
.Select((x, i) =>
new KeyValuePair<string, string>(
x.Trim('[', ']'), // get rid of brackets
lines[i + 1]))
.ToDictionary(x => x.Key, x => x.Value);
var productCode = dictionary["Product code"];
var行=s.拆分(
新建[]{Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries)
.ToArray();
//配对感谢http://stackoverflow.com/questions/1624341/
变量字典=行。其中((x,i)=>i
新的KeyValuePair(
x、 修剪(“[”,“]”),//去掉括号
行[i+1]))
.ToDictionary(x=>x.Key,x=>x.Value);
var productCode=字典[“产品代码”];
所以你有你的字符串s
。让我们从那里开始
在新行拆分,将对放入字典,获取项目:
var lines = s.Split(
new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries)
.ToArray();
// pairing thanks to http://stackoverflow.com/questions/1624341/
var dictionary = lines.Where((x, i) => i < lines.Length)
.Select((x, i) =>
new KeyValuePair<string, string>(
x.Trim('[', ']'), // get rid of brackets
lines[i + 1]))
.ToDictionary(x => x.Key, x => x.Value);
var productCode = dictionary["Product code"];
var行=s.拆分(
新建[]{Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries)
.ToArray();
//配对感谢http://stackoverflow.com/questions/1624341/
变量字典=行。其中((x,i)=>i
新的KeyValuePair(
x、 修剪(“[”,“]”),//去掉括号
行[i+1]))
.ToDictionary(x=>x.Key,x=>x.Value);
var productCode=字典[“产品代码”];
所以你有你的字符串s
。让我们从那里开始
在新行拆分,将对放入字典,获取项目:
var lines = s.Split(
new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries)
.ToArray();
// pairing thanks to http://stackoverflow.com/questions/1624341/
var dictionary = lines.Where((x, i) => i < lines.Length)
.Select((x, i) =>
new KeyValuePair<string, string>(
x.Trim('[', ']'), // get rid of brackets
lines[i + 1]))
.ToDictionary(x => x.Key, x => x.Value);
var productCode = dictionary["Product code"];
var行=s.拆分(
新建[]{Environment.NewLine},
StringSplitOptions.RemoveEmptyEntries)
.ToArray();
//配对感谢http://stackoverflow.com/questions/1624341/
变量字典=行。其中((x,i)=>i
新的KeyValuePair(
x、 修剪(“[”,“]”),//去掉括号
行[i+1]))
.ToDictionary(x=>x.Key,x=>x.Value);
var productCode=字典[“产品代码”];
如果需要,可以在字典中保存密钥时删除括号
如果需要,可以在字典中保存密钥时删除括号
如果需要,可以在字典中保存密钥时删除括号
如果需要,可以在字典中保存密钥时删除括号。我只需使用带有捕获组的正则表达式来获取对,然后将它们加载到字典中:
var dict = Regex
.Matches(str, @"\[([^\]]+)\]([^\[]+)")
.Cast<Match>()
.ToDictionary(match => match.Groups[1].ToString(),
match => match.Groups[2].ToString().Trim());
//dict = { [Product Code, MYPRODUCT-CODE123], [List Price, 28.10], [Price, 20.30] ...}
var dict=Regex
.Matches(str,@“\[([^\]]+)\]([^\[]+)”)
.Cast()
.ToDictionary(match=>match.Groups[1].ToString(),
match=>match.Groups[2].ToString().Trim();
//dict={[Product Code,MYPRODUCT-CODE123],[List Price,28.10],[Price,20.30]…}
如果您将数据全部保存在文本文件中,我强烈建议您将数据存储为XML格式。这样以后您就不会有麻烦了。我只需使用带有捕获组的正则表达式来获取对,然后将它们加载到字典中:
var dict = Regex
.Matches(str, @"\[([^\]]+)\]([^\[]+)")
.Cast<Match>()
.ToDictionary(match => match.Groups[1].ToString(),
match => match.Groups[2].ToString().Trim());
//dict = { [Product Code, MYPRODUCT-CODE123], [List Price, 28.10], [Price, 20.30] ...}
var dict=Regex
.Matches(str,@“\[([^\]]+)\]([^\[]+)”)
.Cast()
.ToDictionary(match=>match.Groups[1].ToString(),
match=>match.Groups[2].ToString().Trim();
//dict={[Product Code,MYPRODUCT-CODE123],[List Price,28.10],[Price,20.30]…}
如果您将数据全部保存在文本文件中,我强烈建议您将数据存储为XML格式。这样以后您就不会有麻烦了。我只需使用带有捕获组的正则表达式来获取对,然后将它们加载到字典中:
var dict = Regex
.Matches(str, @"\[([^\]]+)\]([^\[]+)")
.Cast<Match>()
.ToDictionary(match => match.Groups[1].ToString(),
match => match.Groups[2].ToString().Trim());
//dict = { [Product Code, MYPRODUCT-CODE123], [List Price, 28.10], [Price, 20.30] ...}
var dict=Regex
.Matches(str,@“\[([^\]]+)\]([^\[]+)”)
.Cast()
.ToDictionary(match=>match.Groups[1].ToString(),
match=>match.Groups[2].ToString().Trim();
//dict={[Product Code,MYPRODUCT-CODE123],[List Price,28.10],[Price,20.30]…}
如果您将数据全部保存在文本文件中,我强烈建议您将数据存储为XML格式。这样以后您就不会有麻烦了。我只需使用带有捕获组的正则表达式来获取对,然后将它们加载到字典中:
var dict = Regex
.Matches(str, @"\[([^\]]+)\]([^\[]+)")
.Cast<Match>()
.ToDictionary(match => match.Groups[1].ToString(),
match => match.Groups[2].ToString().Trim());
//dict = { [Product Code, MYPRODUCT-CODE123], [List Price, 28.10], [Price, 20.30] ...}
var dict=Regex
.Matches(str,@“\[([^\]]+)\]([^\[]+)”)
.Cast()
.ToDictionary(match=>match.Groups[1].ToString(),
match=>match.Groups[2].ToString().Trim();
//dict={[Product Code,MYPRODUCT-CODE123],[List Price,28.10],[Price,20.30]…}
我强烈建议,如果您将所有数据都保存在文本文件中,则应以XML格式存储数据。这样以后您就不会再遇到这些麻烦了。