Python 将 CSV 文件中的数据与 JSON 中的数据进行比较不起作用



我是Python新手,想编写一个工具,通过API检索在线平台上的产品,并将其与CSV文件进行比较。如果文件中的值与在线平台上的值不匹配,则应通过API在线更新产品。

我实际上能够成功地检索我需要的值,但我未能比较它们。我的API调用返回JSON格式的响应,我导入了带有pandas的CSV文件。

我在这里打印来自API调用和CSV的零件号以测试它们:

for i in getProducts():
print (i['partNumber'])
for i in df.index:
print (df['Artikelnummer'][i])

,我得到这样的输出:

1001
1000
1001
1000

因此,当我用print测试CSV和JSON响应时,看起来它们具有相同的值。但是当我试图将它们与if进行比较时,它们总是看起来不一样。

总是返回'false'。

for product in getProducts():
for i in df.index:
if product['partNumber'] == df['Artikelnummer'][i]:
print('True')
else:
print('False')
谁能告诉我我做错了什么?

检查它们的dtypes,例如其中一个可以是字符串,另一个可以是整数。这两种情况将输出相同的输出,比较它们将给出False。

api = '1001'
other = 1001
print(api)
print(other)
print(api == other)
print('nAfter cast them to the same type:')
print(int(api) == int(other))

输出:

1001
1001
False
After cast them to the same type:
True

尝试将它们转换为相同的dtype。