저는 파이썬에서 CSV 파일의 데이터를 액세스, 검색 및 입력 (추가) 할 수있는 프로그램을 만들려고했습니다. 파일을 추가하고 인쇄 할 수 있지만 외부 파일을 검색하는 데 문제가 있습니다. 난 그냥 이름하여 파일에서 학생을 검색 할 수 있음을 나의 def s():
지금 할 모르겠어요.CSV 파일 검색
def a():
# knows to use the file (getting something that's made)
import csv
myfile = open("student01.csv", "a")
student_num = input("Enter the student number: ")
name = input("Enter student's name: ")
tutor_group = input("Enter the tutor group: ")
gender = input("Enter M or F: ")
new_record =student_num+","+name+","+tutor_group+","+gender
myfile.write(str(new_record))
myfile.write("\n")
myfile.close()
def d():
# knows to use the file (getting something that's made)
import csv
myfile = open("student01.csv", "rb")
reader = csv.reader(myfile)
for row in myfile:
print(row)
myfile.close()
def menu():
print("Welcome to Student.csv\nWhat would you like to do?")
print()
print("1. Add to the file")
print("2. Display all the data from the file")
print("3. Search for particular data")
enter = input("Enter 1, 2 or 3: ")
enter = enter.upper()
if enter == "1":
a()
elif enter == "2":
d()
else:
s()
menu()
:
이 내 코드는 지금까지 모습입니다 , 번호 또는 교사 그룹.
누구에게 아이디어가 있습니까?
로트
: 파일의 각 행에 대해 행에 원하는 문자열이 포함되어 있는지 확인하십시오. 그렇다면 해당 행을 표시하십시오. 그렇지 않으면 다음 단계로 진행합니다. –
감사합니다. 나는 당신이 말하는 것을 이해하고 있다고 생각합니다. @ 폴 콜링우드 – Lotz