如何将变量与csv文件第二列中的值进行比较?

时间:2019-04-03 16:49:23

标签: python-3.x

我有一个看起来像这样的CSV文件

sr no;address;tempo;loudness

0;E:/project/song_directory/blues/Al Green - Lets Stay 
Together.mp3;95.703125;9.074317932128906

1;E:/project/song_directory/blues/Blackstreet - No Diggity ft. Dr. Dre 
Queen Pen.mp3;89.10290948275862;10.246162414550781

2;E:/project/song_directory/blues/Destiny's Child - Say My Name 
(Official Music Video).mp3;129.19921875;9.540428161621094

我想从文件中提取第二列,然后将其与变量“ songname_selected”进行比较。

import pandas as pd
import numpy as np
song = pd.read_table('E:/project/new.csv', sep=';')

song.address #  all the name
songname_selected =  'E:/project/song_directory/blues/Al Green - Lets 
Stay Together.mp3'
for row in song.address:   
    if row is songname_selected:
        print("true")
    else:
        print("false")

我期望当songname_selected与第二列中的值匹配时输出为true。但是,代码永远不会返回true。

1 个答案:

答案 0 :(得分:1)

尝试

for row in song.address:   
    if row == songname_selected: ## don't use 'is' here
        print("true")
    else:
        print("false")

输出

true
false
false

原因 Is there a difference between “==” and “is”?