import sys import collections user = collections.namedtuple("User", "username midlename surename id") def main(): if len(sys.argv) == 1 or sys.argv[1] in {"-h", "--help"}: print("usage: {0} file11 [file2 [... fileN]]".format(sys.argv[0])) sys.exit() usernames = set() users = {} for filename in sys.argv[1:]: for line in open(filename, encoding = 'Utf-8'): line = line.rstrip() if line: user = process_line(line, usernames) users[(user.surname.lower(), user.forename.lower(), user.id)] = user print_users(users) def process_line(line, usernames): fields = [0, 1, 2, 3, 4] n = 0 for b in line.split(":"): fields[n] = b n += 1 username = generate_username(fields, usernames) user = User(username, fields[1], fields[2], fields[3], fields[0]) return user def generate_username(fields, usernames): username = ((fields[1][0] + fields[2][:1] + fields[3]).replace("-", "").replace("'", "")) username = original_name = username[:8].lower() count = 1 while username in usernames: username = "{0}{1}".format(original_name, count) count += 1 usernames.add(username) return username def print_users(users): namewidth = 32 usernamewidth = 9 print("{0:{nw}} {1:^6} {2:{uw}}".format("Name", "ID", "Username", nw = namewidth, uw=usernamewidth)) print("{0:-<{nw}} {0:-<6} {0:-{uw}}".format("", nw=namewidth, uw=usernamewidth)) for key in sorted(users): user = users[key] initial = "" if user.middlename: initial = " " + user.middlename[0] name = "{0.surname}, {0.forename}{1}".format(user, initial) print("{0:.<nw}} ({1.id:4}) {1.username:{uw}}".format(name, user, nw= namewidth, uw=usernamewidth)) main() 

Here is the text of the Data.txt file:

  1289:Albert:Lukas:Montgomery:legal 1234:Bob:Marly:Montgomery:sing 1256:Andru:Martin:Landail:programs 1235:Alex:Luter:King:legal 1756:Djoan:Katy:Rouling:sing 

The program gives an error:

 Traceback (most recent call last): File "D:\programs\learning_program\generate_users_names\generate_usernames.py", line 67, in <module> main() File "D:\programs\learning_program\generate_users_names\generate_usernames.py", line 17, in main user = process_line(line, usernames) File "D:\programs\learning_program\generate_users_names\generate_usernames.py", line 30, in process_line user = User(username, fields[1], fields[2], NameError: name 'User' is not defined 

Why the system does not see the name of the unique User list, which I defined from the very beginning, please tell me?

  • where do you call main() ? - MaxU
  • OOO !! Exactly !!!!! - Broneslav
  • Thank you very much - Broneslav
  • THESE MESSAGES REFER TO THE PREVIOUS QUESTION !!! - Broneslav

1 answer 1

In Python, the register of the names of objects (variables, functions, classes, etc.) matters!

You have declared a user , and trying to use User - such an object does not exist.

Respectively or declare User :

 User = collections.namedtuple("User", "username midlename surename id") 

or use user :

 usr = user(...) # далее используем `usr` 
  • And then what function does the name of the named list ("User") perform? And where can I use it? - Broneslav
  • @Broneslav, the name in quotes is used as the class name when translating an object into a string view (for example, when outputting via print): ideone.com/Vp1vfX - insolor