如何根据条件(大于或小于)在文本文件中打印出特定的 rows/lines
How to print out specific rows/lines in a text file based on a condition (greater than or less than)
我正在尝试编写一个程序,打印出特定的 rows/lines,其中一个值超过该行中的另一个值。例如,这是文本文件的一小部分:
01,test1,202,290,A,290
02,test2,303,730,A,0
03,test3,404,180,N,180
我尝试编写的程序将 select 所有包含 'A' 的行,还有 select 第 4 列所在的行(第一行 290)大于第 6 列(第一行 290),然后打印 them.So 程序应该只在 python:
上面的文本文件中打印这一行
02,test2,303,730,A,0
我能做的最好的就是使用以下方法打印所有包含 'A' 的行:
F = open("TEST.txt").read()
for line in F.split():
if 'A' in line:
Column=line.split(',')
然而,这只是 select 中包含 'A' 的行,当我尝试根据第 4 列是否大于第 6 列来过滤它时,我得到各种 errors.Can有人帮我解决这个问题吗?
你可以试试下面的代码
for line in open(filename):
if 'A' in line:
Column=line.split(',')
if Column[3] > Column[5]:
print Column
试试下面的代码:
from __future__ import print_function
def condition(cols):
return cols[4] == 'A' and cols[3] > cols[5]
with open('data.txt', 'r') as f:
data = f.readlines()
[print(line) for line in data if condition(line.split(','))]
您可以在"condition"函数中设置任意逻辑过滤条件
我想你一定要看看 pandas。
它会让一切变得更容易:
from __future__ import print_function
import pandas as pd
df = pd.read_csv('data.txt', names=['col1','col2','col3','col4','col5','col6'])
print('Given data-set')
print(df)
df['diff'] = df['col4'] - df['col6']
flt = df[(df.col5 == 'A') & (df.col4 > df.col6)]
print('Filtered data-set')
print(flt)
#print(df.sum(axis=0, numeric_only=True))
print('sum(col6) = %d' % (df.sum(axis=0, numeric_only=True)['col6']))
输出:
Given data-set
col1 col2 col3 col4 col5 col6
0 1 test1 202 290 A 290
1 2 test2 303 730 A 0
2 3 test3 404 180 N 180
Filtered data-set
col1 col2 col3 col4 col5 col6 diff
1 2 test2 303 730 A 0 730
sum(col6) = 470
csv
库将为您将文件解析为行,您也不应将数字作为字符串进行比较,因为它们将按 字典顺序 进行比较,从而给出不正确的输出,同样使用 in
将意味着您将匹配 A
中的 "Apple"
或任何其他它看起来不只是完全匹配的地方,如果您想检查特定列中的完全匹配,那么您应该这样做:
In [8]: cat test.txt
01,test1,202,290,A,290
02,test2,303,730,A,0
03,test3,404,180,N,180
In [9]: from csv import reader
In [10]: for row in reader(open("test.txt")):
if row[4] == "A" and float(row[3]) > float(row[5]):
print(row)
....:
['02', 'test2', '303', '730', 'A', '0']
为什么将数字作为字符串进行比较是个坏主意:
In [11]: "2" > "1234"
Out[11]: True
In [12]: float("2") > float("1234")
Out[12]: False
我正在尝试编写一个程序,打印出特定的 rows/lines,其中一个值超过该行中的另一个值。例如,这是文本文件的一小部分:
01,test1,202,290,A,290
02,test2,303,730,A,0
03,test3,404,180,N,180
我尝试编写的程序将 select 所有包含 'A' 的行,还有 select 第 4 列所在的行(第一行 290)大于第 6 列(第一行 290),然后打印 them.So 程序应该只在 python:
上面的文本文件中打印这一行02,test2,303,730,A,0
我能做的最好的就是使用以下方法打印所有包含 'A' 的行:
F = open("TEST.txt").read()
for line in F.split():
if 'A' in line:
Column=line.split(',')
然而,这只是 select 中包含 'A' 的行,当我尝试根据第 4 列是否大于第 6 列来过滤它时,我得到各种 errors.Can有人帮我解决这个问题吗?
你可以试试下面的代码
for line in open(filename):
if 'A' in line:
Column=line.split(',')
if Column[3] > Column[5]:
print Column
试试下面的代码:
from __future__ import print_function
def condition(cols):
return cols[4] == 'A' and cols[3] > cols[5]
with open('data.txt', 'r') as f:
data = f.readlines()
[print(line) for line in data if condition(line.split(','))]
您可以在"condition"函数中设置任意逻辑过滤条件
我想你一定要看看 pandas。
它会让一切变得更容易:
from __future__ import print_function
import pandas as pd
df = pd.read_csv('data.txt', names=['col1','col2','col3','col4','col5','col6'])
print('Given data-set')
print(df)
df['diff'] = df['col4'] - df['col6']
flt = df[(df.col5 == 'A') & (df.col4 > df.col6)]
print('Filtered data-set')
print(flt)
#print(df.sum(axis=0, numeric_only=True))
print('sum(col6) = %d' % (df.sum(axis=0, numeric_only=True)['col6']))
输出:
Given data-set
col1 col2 col3 col4 col5 col6
0 1 test1 202 290 A 290
1 2 test2 303 730 A 0
2 3 test3 404 180 N 180
Filtered data-set
col1 col2 col3 col4 col5 col6 diff
1 2 test2 303 730 A 0 730
sum(col6) = 470
csv
库将为您将文件解析为行,您也不应将数字作为字符串进行比较,因为它们将按 字典顺序 进行比较,从而给出不正确的输出,同样使用 in
将意味着您将匹配 A
中的 "Apple"
或任何其他它看起来不只是完全匹配的地方,如果您想检查特定列中的完全匹配,那么您应该这样做:
In [8]: cat test.txt
01,test1,202,290,A,290
02,test2,303,730,A,0
03,test3,404,180,N,180
In [9]: from csv import reader
In [10]: for row in reader(open("test.txt")):
if row[4] == "A" and float(row[3]) > float(row[5]):
print(row)
....:
['02', 'test2', '303', '730', 'A', '0']
为什么将数字作为字符串进行比较是个坏主意:
In [11]: "2" > "1234"
Out[11]: True
In [12]: float("2") > float("1234")
Out[12]: False