逐行阅读文本文件 [英] Reading textfiles line by line

查看:68
本文介绍了逐行阅读文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我试图将文本文件读入数据表...


不知道怎么样分解信息,正则表达式或子串... ...


样本:

Col1 Col2 Col3

Col4

A0012430 REKAL电视?? TTMEDEL EKOMAX 0,5L ST 75.9000

A0012550 REKAL电视?? TTMEDEL BIOKUL ?? R 20KGST 1727.0000


通知第二行如何合并col2和col3。

没有分隔符,但根据col2中的文本,位置似乎是相同的。

原始文件

包含约。我想用更新sql表的5000行。以上

问题

出现在文本文件的许多位置。


我已成功读取文本文件中的数据使用此代码:


< code>

StreamReader sr = File.OpenText(fileName);

string line =" ;" ;;

while((line = sr.ReadLine())!= null)

{

if(line.Length> ; 17)

{

DataRow dr = m_Data.NewRow();

dr [" Col1"] = line.Substring(0 ,17).Trim();

dr [" Col2"] =?

dr [" Col3"] =?

dr [" Col4"] =?

m_Data.Rows.Add(dr);

}

}

sr.Close();


任何帮助appriciated!


/ Martin

Hi all,

I m trying to read in a text file into a datatable...

Not sure on how to split up the information though, regex or substrings...?

sample:
Col1 Col2 Col3
Col4
A0012430 REKAL TV??TTMEDEL EKOMAX 0,5L ST 75.9000
A0012550 REKAL TV??TTMEDEL BIOKUL??R 20KGST 1727.0000

Notice how the 2nd row has merged col2 and col3. There are no delimeter at
all but the position seem to be the same depending on the text in col2. The
original file
include approx. 5000 lines that I want to update a sql table with. The above
problem
occurs at many positions in the text file.

I have successfully read in data from the text file using this code:

<code>
StreamReader sr = File.OpenText(fileName);
string line = "";
while ((line = sr.ReadLine()) != null)
{
if(line.Length > 17)
{
DataRow dr = m_Data.NewRow();
dr["Col1"]= line.Substring(0, 17).Trim();
dr["Col2"] = ?
dr["Col3"] = ?
dr["Col4"] = ?
m_Data.Rows.Add(dr);
}
}
sr.Close();

Any help appriciated!

/Martin

推荐答案

不确定你的意思是这个位置似乎是相同的取决于

col2中的文字 ?

如果您尝试使用固定宽度字体显示此文件,例如

courier new,该怎么办?所有字段是否对齐?


对我来说,它看起来像是一个固定宽度的文件。每列总是使用

每行相同的字符范围(但是当使用比例字体时,它可能不会立即显示

)。

Patrice


-


" jamait" < JA **** @ discussions.microsoft.com> écritdansle message de

news:8E ********************************* * @ microsof t.com ...
Not sure what you meant by "the position seem to be the same depending on
the text in col2" ?

What if you try to display this file using a fixed width font such as
courier new ? Are all fields aligned ?

To me it looks like this is a fixed width file. Each column uses always the
same range of characters on each line (but it may not be visible immediately
when using a proportional font).

Patrice

--

"jamait" <ja****@discussions.microsoft.com> a écrit dans le message de
news:8E**********************************@microsof t.com...
大家好,

我试图将文本文件读入数据表...
不确定如何拆分信息,正则表达式或
子串......?
样本:
Col1 Col2 Col3
Col4
A0012430 REKAL TV?TTMEDEL EKOMAX 0,5L ST 75.9000
A0012550 REKAL TV?TTMEDEL BIOKUL?R 20KGST 1727.0000

请注意第2行是如何合并col2和col3的。除了位置似乎是相同的,取决于col2中的文本,
上没有任何分隔符。
原始文件
包括约。我想用更新sql表的5000行。上面的
问题
发生在文本文件的许多位置。

我已经使用以下代码成功读取了文本文件中的数据:

< code>
StreamReader sr = File.OpenText(fileName);
string line ="" ;;
while((line = sr.ReadLine())!= null)
{
if(line.Length> 17)
{DataRow dr = m_Data.NewRow();
dr [" Col1"] = line.Substring (0,17).Trim();
dr [" Col2"] =?
dr [" Col3"] =?
dr [" Col4"] =?
m_Data.Rows.Add(dr);
}
} sr.Close();

任何有用的帮助!

/ Martin
Hi all,

I m trying to read in a text file into a datatable...

Not sure on how to split up the information though, regex or substrings...?
sample:
Col1 Col2 Col3
Col4
A0012430 REKAL TV?TTMEDEL EKOMAX 0,5L ST 75.9000
A0012550 REKAL TV?TTMEDEL BIOKUL?R 20KGST 1727.0000

Notice how the 2nd row has merged col2 and col3. There are no delimeter at
all but the position seem to be the same depending on the text in col2. The original file
include approx. 5000 lines that I want to update a sql table with. The above problem
occurs at many positions in the text file.

I have successfully read in data from the text file using this code:

<code>
StreamReader sr = File.OpenText(fileName);
string line = "";
while ((line = sr.ReadLine()) != null)
{
if(line.Length > 17)
{
DataRow dr = m_Data.NewRow();
dr["Col1"]= line.Substring(0, 17).Trim();
dr["Col2"] = ?
dr["Col3"] = ?
dr["Col4"] = ?
m_Data.Rows.Add(dr);
}
}
sr.Close();

Any help appriciated!

/Martin



>原始文件包括约。 5000行


这是一次性操作吗?如果您正在使用SqlServer,您可以编写一个简单的DTS包来进行转换,或者只需在企业管理器中使用

导入数据命令。


-

Dave Sexton
dave @ www..jwaonline..com

------------------------------------------------ -----------------------

" Patrice" <无**** @ nowhere.com>在消息新闻中写道:un ************* @ TK2MSFTNGP12.phx.gbl ...
> The original file include approx. 5000 lines

Is this a one-time operation? If you are using SqlServer you can write a simple DTS package to do the transformation or just use
the Import Data command in Enterprise Manager.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Patrice" <no****@nowhere.com> wrote in message news:un*************@TK2MSFTNGP12.phx.gbl...
不确定你的意思是这个位置似乎是同样取决于col2中的文字 ?

如果您尝试使用固定宽度字体(如
courier new)显示此文件,该怎么办?所有字段是否对齐?

对我来说,它看起来像是一个固定宽度的文件。每列始终使用每行上相同范围的字符(但在使用比例字体时可能无法立即显示)。

Patrice

-

" jamait" < JA **** @ discussions.microsoft.com> écritdansle message de
新闻:8E ********************************** @ microsof t .com ...
Not sure what you meant by "the position seem to be the same depending on
the text in col2" ?

What if you try to display this file using a fixed width font such as
courier new ? Are all fields aligned ?

To me it looks like this is a fixed width file. Each column uses always the
same range of characters on each line (but it may not be visible immediately
when using a proportional font).

Patrice

--

"jamait" <ja****@discussions.microsoft.com> a écrit dans le message de
news:8E**********************************@microsof t.com...
大家好,

我试图将文本文件读入数据表...

不确定如何拆分信息,正则表达式或
Hi all,

I m trying to read in a text file into a datatable...

Not sure on how to split up the information though, regex or


子串...?


substrings...?


示例:
Col1 Col2 Col3
Col4 < br> A0012430 REKAL TV?TTMEDEL EKOMAX 0,5L ST 75.9000
A0012550 REKAL TV?TTMEDEL BIOKUL?R 20KGST 1727.0000

请注意第2行是如何合并col2和col3的。除了位置似乎是相同的,取决于col2中的文本。

sample:
Col1 Col2 Col3
Col4
A0012430 REKAL TV?TTMEDEL EKOMAX 0,5L ST 75.9000
A0012550 REKAL TV?TTMEDEL BIOKUL?R 20KGST 1727.0000

Notice how the 2nd row has merged col2 and col3. There are no delimeter at
all but the position seem to be the same depending on the text in col2.


原始文件
包括大约。我想用更新sql表的5000行。
original file
include approx. 5000 lines that I want to update a sql table with. The

上面的
问题
发生在文本文件的许多位置。

我已成功读取文本文件中的数据使用此代码:

< code>
StreamReader sr = File.OpenText(fileName);
string line ="" ;;
while((line = sr.ReadLine())!= null)
{
if(line.Length> 17)
{DataRow dr = m_Data.NewRow();
dr [" Col1"] = line.Substring(0,17).Trim();
dr [" Col2"] =?
dr [" Col3"] =?
dr [" Col4"] =?
m_Data.Rows.Add(dr);
}
}
sr.Close();

任何有用的帮助!

/ Martin
problem
occurs at many positions in the text file.

I have successfully read in data from the text file using this code:

<code>
StreamReader sr = File.OpenText(fileName);
string line = "";
while ((line = sr.ReadLine()) != null)
{
if(line.Length > 17)
{
DataRow dr = m_Data.NewRow();
dr["Col1"]= line.Substring(0, 17).Trim();
dr["Col2"] = ?
dr["Col3"] = ?
dr["Col4"] = ?
m_Data.Rows.Add(dr);
}
}
sr.Close();

Any help appriciated!

/Martin




再次


这不是一次性操作,我真正想做的是将每个

线分成4个部分...


第一位是ID字段,secon d字段描述,第三个是单位,

是已知字符数组的格式。最后一列是

产品的价格。

不幸的是打开这个以纯文本打开的文件并逐行阅读

第二列和第三列以某种方式合并在一些行...

合并仅发生在描述字段足够长且

可能位于最长位置的位置文件中的描述...

的单位然后将该行附加到没有任何分隔符的行,而主要的

问题。

文件中的其他列之间隔着超过1个空格




我正在考虑使用某种正则表达式并提取

信息需要一行一行,但不知道如何拆分第二和第三

栏。

A0012430 REKAL TV ?? TTMEDEL EKOMAX 0, 5L ST 75.9000

A0012550 REKAL TV ?? TTMEDEL BIOKUL ?? R 20KGST 1727.0000


也更新了St reamReader使用默认编码正确显示

瑞典字符...

帮助...


/ Martin < br>
Hi again,

It is not a one time operation and what I really want to do is to split each
line into 4 parts...

First bit is a ID field, second field a description, third is the unit and
is in the format of an known char array. The last column is the price of the
product.
Unfortunately when opening this file which is opened as plain text and read
line by line the 2nd and 3rd column somehow merges at some of the rows...
The merging only occurs where the description field is long enough and
probably at the position of the longest description in the file...The unit of
the line is then appended to the line without any delimeter whereas the main
problem.
The other columns in the file are separated by more than 1 white space
between them.

I am thinking of using some kind of regular expression and extracting the
information wanted line by line but not sure how to split the 2nd and 3rd
column.
A0012430 REKAL TV??TTMEDEL EKOMAX 0,5L ST 75.9000
A0012550 REKAL TV??TTMEDEL BIOKUL??R 20KGST 1727.0000

Also updated the StreamReader to use the default encoding to display the
swedish characters properly...
Help...

/Martin
StreamReader sr = new StreamReader(file,System.Text.Encoding.Default);
字符串行="" ;;
while((line = sr.ReadLine())!= null)
{
if(line.Length> 17)
{DataRow dr = m_Data.NewRow();
dr [" Col1"] = line.Substring(0,17).Trim();
dr [" Col2"] =?
dr [" Col3"] =?
dr [" Col4"] =?
m_Data.Rows.Add(dr);
}
}
sr.Close();
StreamReader sr = new StreamReader(file, System.Text.Encoding.Default);
string line = "";
while ((line = sr.ReadLine()) != null)
{
if(line.Length > 17)
{
DataRow dr = m_Data.NewRow();
dr["Col1"]= line.Substring(0, 17).Trim();
dr["Col2"] = ?
dr["Col3"] = ?
dr["Col4"] = ?
m_Data.Rows.Add(dr);
}
}
sr.Close();




" Dave"写道:

原始文件包括约。 5000行



"Dave" wrote:
The original file include approx. 5000 lines



这是一次性操作吗?如果您正在使用SqlServer,您可以编写一个简单的DTS包来进行转换,或者只使用企业管理器中的导入数据命令。

-
Dave Sexton dave @ www..jwaonline..com
-------------- -------------------------------------------------- -------
Patrice <无**** @ nowhere.com>在消息新闻中写道:un ************* @ TK2MSFTNGP12.phx.gbl ...



Is this a one-time operation? If you are using SqlServer you can write a simple DTS package to do the transformation or just use
the Import Data command in Enterprise Manager.

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Patrice" <no****@nowhere.com> wrote in message news:un*************@TK2MSFTNGP12.phx.gbl...

不确定你的意思是这个位置似乎是同样取决于col2中的文字 ?

如果您尝试使用固定宽度字体(如
courier new)显示此文件,该怎么办?所有字段是否对齐?

对我来说,它看起来像是一个固定宽度的文件。每列始终使用每行上相同范围的字符(但在使用比例字体时可能无法立即显示)。

Patrice

-

" jamait" < JA **** @ discussions.microsoft.com> a ??? crit dans le message de
新闻:8E ********************************** @ microsof t.com ...
Not sure what you meant by "the position seem to be the same depending on
the text in col2" ?

What if you try to display this file using a fixed width font such as
courier new ? Are all fields aligned ?

To me it looks like this is a fixed width file. Each column uses always the
same range of characters on each line (but it may not be visible immediately
when using a proportional font).

Patrice

--

"jamait" <ja****@discussions.microsoft.com> a ??crit dans le message de
news:8E**********************************@microsof t.com...
大家好,

我试图将文本文件读入数据表...

确定如何拆分信息,正则表达式或
Hi all,

I m trying to read in a text file into a datatable...

Not sure on how to split up the information though, regex or


子串...?


substrings...?


示例:
Col1 Col2 Col3
Col4
A0012430 REKAL TV ?? TTMEDEL EKOMAX 0,5L ST 75.9000
A0012550 REKAL TV ?? TTMEDEL BIOKUL ?? R 20KGST 1727.0000

请注意第2行是如何合并col2和COL3。除了位置似乎是相同的,取决于col2中的文本。

sample:
Col1 Col2 Col3
Col4
A0012430 REKAL TV??TTMEDEL EKOMAX 0,5L ST 75.9000
A0012550 REKAL TV??TTMEDEL BIOKUL??R 20KGST 1727.0000

Notice how the 2nd row has merged col2 and col3. There are no delimeter at
all but the position seem to be the same depending on the text in col2.


原始文件
包括大约。我想用更新sql表的5000行。
original file
include approx. 5000 lines that I want to update a sql table with. The

上面的
问题
发生在文本文件的许多位置。

我已成功读取文本文件中的数据使用此代码:

< code>
StreamReader sr = File.OpenText(fileName);
string line ="" ;;
while((line = sr.ReadLine())!= null)
{
if(line.Length> 17)
{DataRow dr = m_Data.NewRow();
dr [" Col1"] = line.Substring(0,17).Trim();
dr [" Col2"] =?
dr [" Col3"] =?
dr [" Col4"] =?
m_Data.Rows.Add(dr);
}
}
sr.Close();

任何帮助appriciated!

/ Martin
problem
occurs at many positions in the text file.

I have successfully read in data from the text file using this code:

<code>
StreamReader sr = File.OpenText(fileName);
string line = "";
while ((line = sr.ReadLine()) != null)
{
if(line.Length > 17)
{
DataRow dr = m_Data.NewRow();
dr["Col1"]= line.Substring(0, 17).Trim();
dr["Col2"] = ?
dr["Col3"] = ?
dr["Col4"] = ?
m_Data.Rows.Add(dr);
}
}
sr.Close();

Any help appriciated!

/Martin





这篇关于逐行阅读文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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