简体   繁体   English

如何根据某些特定关键字提取/过滤csv文件的行?

[英]How to extract/filter rows of a csv file based on some particular keywords?

I have the data in the form like 我有这样的形式的数据

How do I separate or filter the rows based on the keywords of the genre(horror, thriller, etc) and store them for further processing(sorting)? 如何根据流派(恐怖,惊悚片等)的关键字分隔或过滤行,并将其存储以进行进一步处理(排序)? 在此处输入图片说明

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM