[英]How to extract/filter rows of a csv file based on some particular keywords?
You could maybe do: 您可以这样做:
f = open("myfile.csv", "r")
romance_mov = []
for line in f:
if "romance" in line.split(",")[4].lower():
romance_mov.append(line)
f.close()
Which would give you a list romance_mov
with all the lines that are of genre 'romance'. 这会给您一个清单
romance_mov
,其中包含所有类型为“浪漫”的行。
EDIT: For sorting the lines based on the value in hitFlop, you could then do: 编辑:为了基于hitFlop中的值对行进行排序,您可以然后执行以下操作:
import numpy as np
# Extract the hitFlop value for each row
hitFlop = []
for item in romance_mov:
hitFlop.append(int(item.split(",")[-1]))
# Obtain the sorted indexes
idx_sorted = np.argsort(hitFlop)
# Sort the romance movies
romance_mov_sorted = np.asarray(romance_mov)[idx_sorted]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.