python - Searching and sorting in text files -
i new code , have problem in reading text file. code need ask user type in specific name code in order proceed code. however, there various name codes user use , don't know how make if type either code in, can proceed.
for example text file looks this
john123,x,x,x
susan233,x,x,x
conor,x,x,x
what need accept name tag despite 1 , able print after. name tags in 1 column.
file = open("paintingjobs.txt","r") details = file.readlines() line in details: estimatenum = input ("please enter estimate number.") if estimatenum = line.split this code far, not know in terms of seeing if name tag valid let user proceed.
you can use module called pickle. python 3.0 internal library. in python 2.0, called: cpickle; else same in both.
be warned way you're doing not secure approach!
from pickle import dump credentials = { 'john': 1234, 'james': 4321, 'julie': 6789 } dump(credentials, open("credentials.p", "wb")) this saves file entitled credentials.p. can load follows:
from pickle import load credentials = load(open("credentials.p", "rb")) print(credentials) here couple of tests:
test_name = 'john' test_code = 1234 this amount to:
print('test: ', credentials[test_name] == test_code) which displays: {'john': 1234, 'james': 4321, 'julie': 6789}
displays: test: true
test_code = 2343 print('test:', credentials[test_name] == test_code) displays: test: false
Comments
Post a Comment