本文是小编为大家收集整理的关于[Q] 如何忽略从文件中读取的文本的第一行的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
你好,
我是 Python 新手,有一个我找不到的简单问题
一个满意的解决方案.
我想从文本文件中逐行读取文本,但想忽略
只有第一行.我知道如何在 Java 中做到这一点(Java 一直是我的
过去几年的主要语言)以及以下是我
在Python中有,但我不喜欢它,想学习更好的方法
这样做.
文件 = 打开(文件名,''r'')
行号 = 0
对于文件中的行:
如果 lineNumber == 0:
线号 = 线号 + 1
其他:
线号 = 线号 + 1
打印线
谁能告诉我如何更好地完成这种任务?
提前致谢.
推荐答案
2008 年 8 月 27 日星期三 21:11:26 -0700,yo**************@gmail.com 写道:我想从文本文件中逐行读取文本,但想忽略input_file = 打开(文件名)
只有第一行.我知道如何在 Java 中做到这一点(Java 一直是我的
过去几年的主要语言)以及以下是我
在Python中有,但我不喜欢它,想学习更好的方法
这样做.
文件 = 打开(文件名,''r'')
行号 = 0
对于文件中的行:
如果 lineNumber == 0:
线号 = 线号 + 1
其他:
线号 = 线号 + 1
打印线
谁能告诉我更好地完成这种任务?
行 = iter(input_file)
lines.next() # 跳线.
换行:
打印线
input_file.close()
巧,
Marc ''BlackJack'' Rintsch
8 月 28 日上午 6:11*,"youngjin.mich...@gmail.com"
<youngjin.mich...@gmail.com 写道:你好,fileInput = open(filename, ''r'')
我是 Python 新手,有一个我找不到的简单问题
一个满意的解决方案.
我想从文本文件中逐行读取文本,但想忽略
只有第一行.我知道如何在 Java 中做到这一点(Java 一直是我的
过去几年的主要语言)以及以下是我
在Python中有,但我不喜欢它,想学习更好的方法
这样做.
文件 = 打开(文件名,''r'')
行号 = 0
对于文件中的行:
* * 如果 lineNumber == 0:
* * * * lineNumber = lineNumber + 1
* * 其他:
* * * * lineNumber = lineNumber + 1
* * * * 打印线
谁能告诉我如何更好地完成这种任务?
提前致谢.
对于 lnNum,enumerate(fileInput) 中的行:
如果不是 lnNum:
继续
print line
我想从文本文件中逐行读取文本,但想忽略只有第一行.我知道如何在 Java 中做到这一点(Java 一直是我的为什么不在处理前阅读并丢弃第一行
过去几年的主要语言)以及以下是我
在Python中有,但我不喜欢它,想学习更好的方法
做它.
文件的其余部分?
文件 = 打开(文件名,''r'')
file.readline()
对于文件中的行:打印行,
(有效).
问题描述
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.
推荐答案
On Wed, 27 Aug 2008 21:11:26 -0700, yo**************@gmail.com wrote:
I want to read text line-by-line from a text file, but want to ignoreinput_file = open(filename)
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?
lines = iter(input_file)
lines.next() # Skip line.
for line in lines:
print line
input_file.close()
Ciao,
Marc ''BlackJack'' Rintsch
On Aug 28, 6:11*am, "youngjin.mich...@gmail.com"
<youngjin.mich...@gmail.comwrote:Hello,fileInput = open(filename, ''r'')
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.
for lnNum, line in enumerate(fileInput):
if not lnNum:
continue
print line
I want to read text line-by-line from a text file, but want to ignoreonly the first line. I know how to do it in Java (Java has been myWhy don''t you read and discard the first line before processing the
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.
rest of the file?
file = open(filename, ''r'')
file.readline()
for line in file: print line,
(It works).