0

I have a problem , i wanna to search a data with python from csv file

my code like this

#search process area
area_proses = []
sg1 = []
sg2 = []
sg3 = []
avg = []

#input number you want to search
number = raw_input('Masukan id Spesific Goal\n')

#read csv, and split on "," the line
csv_file = csv.reader(open('C:/xampp_2/htdocs/SkripsiV2/fuzzy/download.csv', "rb"), delimiter=",")

#loop through csv list
for row in csv_file:
    area_proses.append(row[1])
    sg1.append(row[2])
    sg2.append(row[3])
    sg3.append(row[4])
    avg.append(row[5])
    #if current rows 1nd value is equal to input, print that row
    if number == row[0]:
        #masukan data
         print(area_proses,sg1,sg2,sg3,avg)

my problem is when i search with id 11 the output is like this:

(['area_proses', 'Service Delivery'], ['sg1', '3.71'], ['sg2', '3.48'], ['sg3',
'3.30'], ['avg', '3.50'])

but when i search id 12 the output is like :

 (['area_proses', 'Service Delivery', 'Incident Resolution and Prevention'], ['sg
    1', '3.71', '3.83'], ['sg2', '3.48', '3.65'], ['sg3', '3.30', '3.70'], ['avg', '
    3.50', '3.73'])

How i can solved this problem?

Download.csv

"id","area_proses","sg1","sg2","sg3","avg","fuzzy",
"11","Service Delivery","3.71","3.48","3.30","3.50","0.00000000000",
"12","Incident Resolution and Prevention","3.83","3.65","3.70","3.73","0.00000000000",
"13","Service System Development","3.93","3.29","3.26","3.49","0.00000000000",
"14","Service System Transition","3.00","3.43","0.00","3.22","0.00000000000",
"15","Strategic Service Management","3.48","3.86","0.00","3.67","0.00000000000",
"16","Configuration Management","3.14","3.57","0.00","3.36","0.00000000000",
"17","Measurement and Analysis","2.93","3.18","0.00","3.06","0.00000000000",
3
  • please post a sample of the content in download.csv Commented Oct 26, 2017 at 9:17
  • Why don't you import this as a pandas Dataframe and work on that? Commented Oct 26, 2017 at 9:17
  • @VanPeer i have add a sample from download.csv Commented Oct 26, 2017 at 9:19

2 Answers 2

1

Try using the pandas library. Install it, then do:

import pandas as pd
df = pd.read_csv('csv_file.csv')

df[df['id'] == number]
Sign up to request clarification or add additional context in comments.

1 Comment

Corrected it. Earlier I had referenced df[0], but this is called df['id'] since your columns have names now.
0

Just change 'rb' to 'r'

fopn = open(file_loc, "r")
   csv_file = csv.reader(fopn)
   for row in csv_file:
       if number == row[0]:
          print(row)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.