[问]如何忽略从文件中读取的文本的第一行 [英] [Q] How to ignore the first line of the text read from a file
问题描述
您好,
我是Python的新手,有一个我无法找到的简单问题
a满意的解决方案。
我想从文本文件中逐行阅读文本,但是想要忽略
只有第一行。我知道如何用Java做这件事(Java在过去几年里一直是我的主要语言),以下是我用Python获得的
,但我不知道不喜欢它,想要学习更好的方式
这样做。
file = open(fileName,''r'')
lineNumber = 0
for line in file:
if lineNumber == 0:
lineNumber = lineNumber + 1 < br $> b $ b否则:
lineNumber = lineNumber + 1
打印行
有谁能告诉我更好做这种任务?
提前致谢。
Hello,
I am new to Python and have one simple question to which I cannot find
a satisfactory solution.
I want to read text line-by-line from a text file, but want to ignore
only the first line. I know how to do it in Java (Java has been my
primary language for the last couple of years) and following is what I
have in Python, but I don''t like it and want to learn the better way
of doing it.
file = open(fileName, ''r'')
lineNumber = 0
for line in file:
if lineNumber == 0:
lineNumber = lineNumber + 1
else:
lineNumber = lineNumber + 1
print line
Can anyone show me the better of doing this kind of task?
Thanks in advance.
推荐答案
周三,2008年8月27日21:11:26 -0700,哟********* *****@gmail.com 写道:
On Wed, 27 Aug 2008 21:11:26 -0700, yo**************@gmail.com wrote:
我想从文本文件中逐行阅读文本,但想忽略
只有第一行。我知道如何用Java做这件事(Java在过去几年里一直是我的主要语言),以下是我用Python获得的
,但我不知道不喜欢它,想要学习更好的方式来实现它。
file = open(fileName,''r'')
lineNumber = 0
for line in file:
if lineNumber == 0:
lineNumber = lineNumber + 1 < br $> b $ b否则:
lineNumber = lineNumber + 1
打印行
有谁能告诉我更好做这种任务?
I want to read text line-by-line from a text file, but want to ignore
only the first line. I know how to do it in Java (Java has been my
primary language for the last couple of years) and following is what I
have in Python, but I don''t like it and want to learn the better way of
doing it.
file = open(fileName, ''r'')
lineNumber = 0
for line in file:
if lineNumber == 0:
lineNumber = lineNumber + 1
else:
lineNumber = lineNumber + 1
print line
Can anyone show me the better of doing this kind of task?
input_file = open(filename)
lines = iter(input_file)
lines.next()#跳线。
换行:
打印行
input_file.close()
Ciao,
Marc''BlackJack''Rintsch
input_file = open(filename)
lines = iter(input_file)
lines.next() # Skip line.
for line in lines:
print line
input_file.close()
Ciao,
Marc ''BlackJack'' Rintsch
8月28日,6:11 * am,youngjin.mich .. 。@ gmail.com"
< youngjin.mich ... @ gmail.comwrote:
On Aug 28, 6:11*am, "youngjin.mich...@gmail.com"
<youngjin.mich...@gmail.comwrote:
您好,
我是Python的新手,有一个我无法找到的简单问题
a满意的解决方案。
我想逐行阅读文字从文本文件行,但想忽略
只有第一行。我知道如何用Java做这件事(Java在过去几年里一直是我的主要语言),以下是我用Python获得的
,但我不知道不喜欢它,想要学习更好的方式
这样做。
file = open(fileName,''r'')
lineNumber = 0
for line in file:
* * if lineNumber == 0:
* * * * lineNumber = lineNumber + 1
* * else:
* * * * lineNumber = lineNumber + 1
* * * * print line
任何人都可以向我展示做这类任务的好处吗?
提前致谢
Hello,
I am new to Python and have one simple question to which I cannot find
a satisfactory solution.
I want to read text line-by-line from a text file, but want to ignore
only the first line. I know how to do it in Java (Java has been my
primary language for the last couple of years) and following is what I
have in Python, but I don''t like it and want to learn the better way
of doing it.
file = open(fileName, ''r'')
lineNumber = 0
for line in file:
* * if lineNumber == 0:
* * * * lineNumber = lineNumber + 1
* * else:
* * * * lineNumber = lineNumber + 1
* * * * print line
Can anyone show me the better of doing this kind of task?
Thanks in advance.
fileInput = open(filename,''r'')
for lnNum,line in enumerate(fileInput):
如果不是lnNum:
继续
打印行
fileInput = open(filename, ''r'')
for lnNum, line in enumerate(fileInput):
if not lnNum:
continue
print line
我想阅读文字line-by -line来自文本文件,但想忽略
I want to read text line-by-line from a text file, but want to ignore
只有第一行。我知道如何用Java做这件事(Java在过去几年里一直是我的主要语言),以下是我用Python获得的
,但我不知道不喜欢它,想要学习更好的方式
这样做。
only the first line. I know how to do it in Java (Java has been my
primary language for the last couple of years) and following is what I
have in Python, but I don''t like it and want to learn the better way
of doing it.
为什么在处理
其余文件之前,你没有阅读并丢弃第一行?
file = open(filename,''r'')
file.readline()
for line in file:print line,
(有效)。
Why don''t you read and discard the first line before processing the
rest of the file?
file = open(filename, ''r'')
file.readline()
for line in file: print line,
(It works).
这篇关于[问]如何忽略从文件中读取的文本的第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!