如何计算文本文件中的行? [英] How to count lines in a text file ?

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

问题描述

大家好。


我正在尝试编写一个程序:

1)问我想要计算哪些文件的行数in,然后计算

行并写出answear。


2)我做了第一部分:

in_file = raw_input("您要打开的文件的名称是什么:")

in_file = open(" test.txt"," r")

text = in_file.read()


3)我认为我必须使用for循环(类似于:for line in text:

count + = 1)

或者我可能需要创建一个def:类似于:( def loop(line,

count)),但不确定如何正确地做到这一点。

然后可能使用readlines()函数,但又不太确定

如何做到这一点。所以你们其中一个人有个好主意。


感谢所有帮助

Hi all.

I''m trying to write a program that:
1) Ask me what file I want to count number of lines in, and then counts the
lines and writes the answear out.

2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt","r")
text = in_file.read()

3) I think that I have to use a for loop ( something like: for line in text:
count +=1)
Or maybee I have to do create a def: something like: ( def loop(line,
count)), but not sure how to do this properly.
And then perhaps use the readlines() function, but again not quite sure how
to do this. So do one of you have a good idea.

Thanks for all help

推荐答案

哦,我刚刚做到了。


刚使用过的行:


print"%d line in your choosen file" %len(open(" test.txt")。readlines())


非常感谢:)

" Ling Lee" < JA ***** @ mail.trillegaarden.dk>在消息中写道

news:41 *********************** @ nntp02.dk.telia.net ...
Oh I just did it.

Just used the line:

print "%d lines in your choosen file" % len(open("test.txt").readlines())

Thanks though :)
"Ling Lee" <ja*****@mail.trillegaarden.dk> wrote in message
news:41***********************@nntp02.dk.telia.net ...

我正在尝试编写一个程序:
1)问我想要计算哪些文件的行数,然后计算
线条并将answear写出来。

2)我做了第一部分:

in_file = raw_input("是什么名字的你要打开的文件:")
in_file = open(" test.txt"," r")
text = in_file.read()

3)我认为我必须使用for循环(类似于:for line in
text:count + = 1)
或许我必须创建一个def:类似于:(def loop(line) ,
数)),但不确定如何正确地做到这一点。
然后可能使用readlines()函数,但再次不太确定
如何做到这一点。所以你们其中一个人有个好主意。

感谢所有帮助
Hi all.

I''m trying to write a program that:
1) Ask me what file I want to count number of lines in, and then counts
the lines and writes the answear out.

2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt","r")
text = in_file.read()

3) I think that I have to use a for loop ( something like: for line in
text: count +=1)
Or maybee I have to do create a def: something like: ( def loop(line,
count)), but not sure how to do this properly.
And then perhaps use the readlines() function, but again not quite sure
how to do this. So do one of you have a good idea.

Thanks for all help



是的,你需要一个for循环和计数变量。你可以用几种方式计算。文件对象是可迭代的,它们遍历

文件中的行。 readlines()返回一个行列表,它们具有相同的

效果,但是因为它首先在内存中构建整个列表,所以它使用了更多的内存。例如:


########

filename = raw_input(''file?'')

file = open(filename)


lines = 0

for line in file:

#line在这里被忽略,但它包含文件的每一行,

#包括换行符

行+ = 1


print''% r有%r行''%(文件名,行)

########


另一种选择是使用标准posix程序wc使用

-l选项,但这不是Python。


周一,2004年9月20日下午03:18:53 +0200, Ling Lee写道:
Yes, you need a for loop, and a count variable. You can count in several
ways. File objects are iterable, and they iterate over the lines in the
file. readlines() returns a list of the lines, which will have the same
effect, but because it builds the entire list in memory first, it uses
more memory. Example:

########

filename = raw_input(''file? '')
file = open(filename)

lines = 0
for line in file:
# line is ignored here, but it contains each line of the file,
# including the newline
lines += 1

print ''%r has %r lines'' % (filename, lines)

########

another alternative is to use the standard posix program "wc" with the
-l option, but this isn''t Python.

On Mon, Sep 20, 2004 at 03:18:53PM +0200, Ling Lee wrote:
大家好。

我正在努力编写一个程序:
1)问我想要算什么文件的数量然后计算
行并写出answear。

2)我做了第一部分:

in_file = raw_input("您要打开的文件的名称是什么:")
in_file = open(" test.txt"," r")
text = in_file.read()

3)我认为我必须使用for循环(类似于:for line in text:
count + = 1)
或许我必须要创建一个def:类似于:( def loop(line,
count)),但不确定如何正确地做到这一点。
然后可能使用readlines()函数,但又不太确定如何做
这个。所以你们其中一个人有个好主意。

感谢所有帮助
Hi all.

I''m trying to write a program that:
1) Ask me what file I want to count number of lines in, and then counts the
lines and writes the answear out.

2) I made the first part like this:

in_file = raw_input("What is the name of the file you want to open: ")
in_file = open("test.txt","r")
text = in_file.read()

3) I think that I have to use a for loop ( something like: for line in text:
count +=1)
Or maybee I have to do create a def: something like: ( def loop(line,
count)), but not sure how to do this properly.
And then perhaps use the readlines() function, but again not quite sure how
to do this. So do one of you have a good idea.

Thanks for all help



Ling Lee< ja **** *@mail.trillegaarden.dk>写道:
Ling Lee <ja*****@mail.trillegaarden.dk> wrote:
哦,我刚刚做到了。

刚用过的行:

打印%d行中的选择文件" %len(open(" test.txt")。readlines())

谢谢:)
Oh I just did it.

Just used the line:

print "%d lines in your choosen file" % len(open("test.txt").readlines())

Thanks though :)




欢迎您;-)。但是,这种方法一次将所有文件读入

内存。如果你必须能够处理humungoug文件,也可以立即适应内存,请尝试以下方法:


numlines = 0
for line in open(''text.txt''):numlines + = 1

Alex



You''re welcome;-). However, this approach reads all of the file into
memory at once. If you must be able to deal with humungoug files, too
big to fit in memory at once, try something like:

numlines = 0
for line in open(''text.txt''): numlines += 1
Alex


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

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