2017-12-06 5 views
0

그래서 제안 된대로 코드를 수정했습니다. 루프에 들어갈 수 있지만 정확한 이름을 입력하면 나올 수 없습니다. 어떤 제안? 여기에 내가 가진 무엇 : 참고하시기 바랍니다 : 내가 입력을 줄 때while 루프가 올바른 데이터를 출력하지 않습니다.

import csv 

full_name = input('Enter your full name: ').lower() 

with open('Report1.csv') as csvfile: 
    hour_summation = {} 
    read_csv = csv.reader(csvfile, delimiter=',') 
    for row in read_csv: 
     while (' '.join((row[0], row[1]))).lower() != full_name.strip().lower(): 
      print('Name is not in system') 
      full_name = input('Enter your full name: ').lower() 
     if(' '.join((row[0], row[1]))).lower() == full_name.strip().lower(): 
      hour_summation[row[2]] = hour_summation.get(row[2], 0) + int(float(row[3])) 
print('This is {} full hours report:'.format(full_name)) 
for k, v in hour_summation.items(): 
    print(k + ': ' + str(v) + ' hours') 

여기 결과입니다. Steve Miller는 csv 파일에 없으므로 첫 번째 응답이 정확합니다. 그러나 Sri Mantri는 파일에 있으며 계속해서 그녀의 이름으로 모든 목록을 인쇄해야합니다.

Enter your full name: Steve Miller 
Name is not in system 
Enter your full name: Sri Mantri 
Name is not in system 

다음은 코드가 실행될 때의 출력입니다.

Enter your full name: Sri mantri 
This is sri mantri full hours report: 
Beeline Blank: 28 hours 
SRV-0001 Service Requests for Base and Direct Services: 4 hours 
SUP-0001 Support Requests with a CISM Ticket: 129 hours 
SUP-2503 Web Application Maintenance & Support: 72 hours 
0026184229 Margin Controlling Java Rewrite: 4 hours 
0033472751 PRE-AFE 2017 - CMS Enhancements: 2 hours 
0033472863 PRE-AFE 2017 - BPM Enhancements: 67 hours 
APP-10008 Pre-Series (Non-Mainframe): 4 hours 
APP-10146 Logistics (Non-Mainframe): 3 hours 
APP-10195 Vehicle Labor System (Mainframe): 3 hours 
APP-10354 Web PartsPro (Non-Mainframe): 1 hours 
APP-10431 VIPService (Non-Mainframe): 1 hours 
APP-10432 VIPService (Mainframe): 3 hours 
APP-10536 Truck Invoice Adjustments (Mainframe): 2 hours 

과 CSV는 다음과 같습니다

First Name Last Name Activity Hours 
Sri Mantri SUP-2503 Web Application Maintenance & Support 11 
Sri Mantri SUP-2503 Web Application Maintenance & Support 3 
Sri Mantri SUP-2503 Web Application Maintenance & Support 5 
Sri Mantri SUP-2503 Web Application Maintenance & Support 2 
Jeff Moore SUP-2503 Web Application Maintenance & Support 3 
David Ayers SUP-2507 NAFTA MFTS OS Support 10 
Prasanth Musunuru 0020826809 Vertex 6.0 at the NDC 4 
Prasanth Musunuru 0020826809 Vertex 6.0 at the NDC 3 
Prasanth Musunuru 0020826809 Vertex 6.0 at the NDC 1 
Prasanth Musunuru 0020826809 Vertex 6.0 at the NDC 1 
Jeff Moore 0024480049 Fuel Tanks (infrastructure) - time tracking 1 
Jeff Moore 0024480049 Fuel Tanks (infrastructure) - time tracking 1 
Jeff Moore 0024480049 Fuel Tanks (infrastructure) - time tracking 4 
+0

StackOverflow에 오신 것을 환영합니다. 도움말 설명서의 게시 지침을 읽고 따르십시오. [최소한의 완전하고 검증 가능한 예제] (http://stackoverflow.com/help/mcve)가 여기에 적용됩니다. MCVE 코드를 게시하고 문제를 정확하게 설명하기 전까지는 효과적으로 도움을 드릴 수 없습니다. 게시 된 코드를 텍스트 파일에 붙여넣고 설명한 문제를 재현 할 수 있어야합니다. 이 경우 데이터 파일을 제공하고 검색 대상 이름을 하드 코딩하십시오. – Prune

+0

'print()'를 사용하여 변수에 값을 표시하면 값이 다른지 아닌지를 알 수 있습니다. – furas

+0

왜'name'을 사용하여 두 번째'input()'을 얻고'full_name'을 체크합니까? 'full_name' 만 사용해야합니다. – furas

답변

1

당신이 이름을 가져 name를 사용하지만 나중에 full_name

while (' '.join((row[0], row[1]))).lower() != full_name.strip(): 
     print('Name is not in system') 
     name = input('Enter your full name: ') 

당신은 full_name을 사용해야합니다 사용이 코드 내부 (IT 및 need lower())

while (' '.join((row[0], row[1]))).lower() != full_name.strip(): 
     print('Name is not in system') 
     full_name = input('Enter your full name: ').lower() # <-- full_name 

또는 당신은 FULL_NAME에 이름을 연결하여 while 루프에서 선이 누락 name

while (' '.join((row[0], row[1]))).lower() != full_name.strip(): 
     print('Name is not in system') 
     name = input('Enter your full name: ') 
     full_name = name.lower() # <-- full_name 
+0

내가 만든 변경 사항을 살펴보십시오. – stevenmiller

1

full_name에 변환 : 바로 입력 아래,

full_name = name.lower() 

이 while 루프이 추가하기() 호출, 파일의 맨 위쪽에있는 것처럼. 당신의 while 루프 내부

, 그것을 말해야한다 :

name = input('Enter your full name: ') 
full_name = name.lower() 

실행하는 프로그램을 얻을 것이다, 또한, 적어도!

또한 프로그램의 논리에 결함이있는 것 같습니다 ... 사용자 이름을 확인하면서 CSV의 각 행을 단계별로 실행합니다. 즉, CSV 파일에 항목이 두 개 이상있는 경우 (즉, CSV 파일에 한 사람 이상의 정보가 저장되어있는 경우) 목록의 첫 번째 항목에만 실제로 액세스 할 수 있습니다. 사용자 이름을 묻는 메시지가 나타나면 CSV의 각 행을 실행하여 일치하는지 확인해야합니다. CSV 전체에 일치하는 항목이없는 경우에만 다른 이름을 물어봐도됩니다. 그냥보세요 ...

1

실제로 다른 솔루션에 관심이 있으시면 다른 답변으로 이동하십시오. 팬더를 사용하면 필요를 쉽게 해결할 수 있습니다.

그래서 CSV 파일 ("이름.

import pandas 

if __name__ == "__main__": 
    csv = pandas.read_csv("names.csv") 
    name = input("Enter your name: ") 
    if (csv["Name"]==name).any(): 
     print("Name Present") 
     correct_name = csv.loc[csv["Name"] == name] 
     # print everything 
     print(csv) 
     print() 
     #print correct name 
     print(correct_name) 
     print() # for clear display 
     # get individual things 
     print("Correct Inidividual Values") 
     print(correct_name.values) 
    else: 
     print("Name not there") 

샘플 입출력 : 그것은이 값 CSV ") 여기서

Name Hours Sri Mati 1 Some Name 2 

코드이다

Enter your name: Steve Miller 
Name not there 

다음 실행

Enter your name: Sri Mati 
Name Present 
     Name Hours 
0 Sri Mati  1 
1 Some Name  2 

     Name Hours 
0 Sri Mati  1 

Correct Inidividual Values 
[['Sri Mati' 1]]