从文本文件中提取值

时间:2011-10-15 10:16:15

标签: python

我有一个文本文件如下,有2列

44333373    -5.829738285
3007762     -5.468521083
16756295    -5.247183569
46197456    -5.216096421
46884567    -5.195179321
44333390    -5.162411562
44420579    -5.133122186
6439190     -5.028260409
...

我想提取大于-5.162411562理想输出的值

输出

44333373    -5.829738285
3007762     -5.468521083
16756295    -5.247183569
46197456    -5.216096421
46884567    -5.195179321

为了完成这个任务,我编写了简单的python脚本

f1=open("file.txt","r")
n=0
for line in f1.readlines():
     if float(n) > -5.162411562:
        print line

但它只是读取文件中的所有数据。我知道这是一项非常简单的任务,但我无法弄清楚我哪里出错了。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

嗯,您需要将n实际设置为零以外的值。怎么样:

with open('file.txt') as f1:
  for line in f1: # readlines is not necessary here
    n = float(line.split()[1]) # line.split()[0] is the first number
    if n > -5.162411562:
        print (line.rstrip('\r\n')) # rstrip to remove the existing EOL in line

答案 1 :(得分:0)

line包含44333373 -5.829738285。在循环lines时,你需要分割线和&考虑第一个要素&你不需要n。然后比较。所以代码更改为 -

f1=open("file.txt","r")
for line in f1.readlines():
     if float(line.split()[1]) > -5.162411562:
        print line

这里稍作修改。 readlines一次性将整个文件内容读入内存。如果文件太大,那么你可能会遇到问题。 python中的文件操作符是一个迭代器。多么酷啊!此外,open默认情况下会以read模式打开文件。因此代码进一步简化为 -

for line in open('file.txt'):
    if float(line.split()[1]) > -5.162411562:
        print line

希望这会有所帮助......

答案 2 :(得分:0)

您提供的代码的问题是n的值永远不会更改,因此if语句将始终评估为True,因此line将被打印:

f1=open("file.txt","r")
n=0  # the `n` is set here
for line in f1.readlines():
     if float(n) > -5.162411562:  # `n` is always `0`, so this is always `True`
        print line

您需要使用从每行第二列中提取的数字更新变量n

此外,if条件必须将其比较运算符从>(大于)更改为<(小于),因为您在输出中显示的值是值“小于-5.162411562”,而不是“大于”

此外,应注意n=0并非必需。

通过这些更改,我们得到以下代码:

f1 = open("file.txt","r")
for line in f1.readlines():
  n = line.split()[1]          # get the second column
  if float(n) < -5.162411562:  # changed the direction comparison
     print line.rstrip()       # remove the newline from the line read
                               # from the file to prevent doubling of newlines
                               # from the print statement
f1.close()                     # for completeness, close the file

结果输出为:

44333373        -5.829738285
3007762         -5.468521083
16756295        -5.247183569
46197456        -5.216096421
46884567        -5.195179321
相关问题