Answer the question
In order to leave comments, you need to log in
How to get data by 1 parameter from CSV?
I have this csv file:
name|id|nick
Test1|1|@test1
Test2|2|@test2
Test3|3|@test3
Answer the question
In order to leave comments, you need to log in
import csv
ID = 1
with open('users.csv') as csvfile:
reader = csv.DictReader(csvfile, delimiter='|')
for row in reader:
if int(row['id']) == ID:
print(f'{row["name"]} {row["nick"]}')
Can you do it like this
import pandas as pd
data = pd.read_csv('list.csv', sep=';')
def get_user_by_id(id, data):
data = data.loc[data['id'] == id, ['nick', 'name']]
name = data['name'][0]
nick = data['nick'][0]
return {'name': name, 'nick': nick}
print(get_user_by_id(1, data))
# Out: {'name': 'Test1', 'nick': '@test1'}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question