python 匹配數(shù)據(jù)輸出數(shù)據(jù)
問(wèn)題描述
我現(xiàn)在有一個(gè)list,里面的內(nèi)容為re=[’test1’,’test2’,’test3’],還有一個(gè)csv或者xlsx或者txt,里面有兩列數(shù)據(jù)

我想達(dá)到的效果是如果re的元素等于文件中的A列,這輸出這個(gè)文件的AB列的那一行數(shù)據(jù),這個(gè)具體應(yīng)該怎么實(shí)現(xiàn)呢?
問(wèn)題解答
回答1:你需要先讀文件,然后結(jié)果放到數(shù)組再對(duì)數(shù)組的值進(jìn)行查找
回答2:import csvwith open('lookup.csv') as f: reader = csv.reader(f, delimiter=’,’) dict_lookup = {r[0]:r[1] for r in reader}print(dict_lookup) print(dict_lookup[’test2’])
輸出
{’test1’: ’output1’, ’test2’: ’output2’, ’test3’: ’output3’}output2
若lookup.csv內(nèi)容為
test1,output1test2,output2test3,output3
這是很基本的csv模塊使用操作,建議看一下
相關(guān)文章:
1. 關(guān)docker hub上有些鏡像的tag被標(biāo)記““This image has vulnerabilities””2. docker - 如何修改運(yùn)行中容器的配置3. docker鏡像push報(bào)錯(cuò)4. 關(guān)于docker下的nginx壓力測(cè)試5. angular.js - angularjs的自定義過(guò)濾器如何給文字加顏色?6. Docker for Mac 創(chuàng)建的dnsmasq容器連不上/不工作的問(wèn)題7. 前端 - @media query 使用出現(xiàn)的問(wèn)題?8. html5和Flash對(duì)抗是什么情況?9. java - 如何點(diǎn)擊按鈕,重新運(yùn)行(我是初學(xué)者)?10. html - css氣泡,實(shí)現(xiàn)“倒三角(不知道算不算三角了)”可透明的。

網(wǎng)公網(wǎng)安備