問題描述
使用文件中的類和對象獲取信息 (Get info using class and object from file)
我有這個代碼可以正常工作。但我想知道是否可以從保存在 main.py 中的 CSV 文件中為特定行提取相同的信息。例如。如果用戶想通過使用“類”來了解誰在 IT 部門工作?和“對象” 保存的 csv 文件中的方法。我們怎麼能做到這一點,甚至有可能嗎?謝謝
class person:
name = "name"
email = "email"
deptt = "deptt"
def init(self,name,email,deptt):
self.name = name
self.email = email
self.deptt = deptt
def staff(self):
return f" this person name {self.name} and id is {self.email} and she works in {self.deptt} department "
p1 = person("Hannah","hannah.jones@lycos.com", "admin")
print(p1.staff())
</code></pre>
參考解法
方法 1:
You can use pandas to read CSV files.
import pandas as pd
df = pd.read_csv('filename.csv')
(by GhostwhiteActualCompilers、david190810)
參考文件