Python: Writing lists to .csv file -


i’m teaching myself programming, using python initial weapon of choice.

i have learnt few basics , decided set myself challenge of asking user list of names, adding names list , writing names .csv file.

below code. works.

my question differently, i.e. how code improved readability , efficiency. approach situation differently, structure differently, call different functions? interested in, , appreciate great deal, feedback more experienced programmers.

in particular, find parts clunky; such having specify user required format data entry. if request data (name age location) without commas however, each record, when written .csv, end 1 record per cell (excel) – not desired result.

#requesting user input.  guestnames = input("please enter names of guests, 1 @ time.\n"\     "once have finished entering information, please type word \"done\".\n"\     "please enter names in following format (name, age, location). ").capitalize()  guestlist.append(guestnames)  while guestnames.lower() != "done".lower() :     guestnames = input("please enter name of " + guestnumber[number] + " guest: ").capitalize()     guestlist.append(guestnames)     number += 1  #sorting list.  guestlist.sort() guestlist.remove("done")  #creating .csv file.  guestfile = open("guestlist.csv","w") guestfile.close()  #writing file.  entries in guestlist :     guestfile = open("guestlist.csv","a")     guestfile.write(entries)     guestfile.write("\n")     guestfile.close() 

i try write down demands:

  1. parse input string according structure (whatever) , save results list
  2. format result csv-format string
  3. write string csv file

first of all, highly recommend read python string operation , formatting tutorial google developer tutorial. when understand basic operation, have @ official documentation see available string processing methods in python.

your logic write code right, there 2 meaningless lines:

  1. while guestnames.lower() != "done".lower()

it's not necessary lower "done" since lower-case.

  1. for entries in guestlist : guestfile = open("guestlist.csv","a")

here open , close questlist.csv every loop, useless , costly. open file @ beginning, save lines loop, , close @ end.

this sample using same logic , different input format:

print('some notification @ beginning')  while true:     guestnames = input("please enter name of " + guestnumber[number] + " guest: ").capitalize()      if guestnames == 'done':         # jump out of loop if user says done         break     else:         # assume user input 'name age location', replace space commas         guestlist.append(guestnames.replace(' ', ','))         number += 1  guestlist.sort()  # keyword close guestfile @ end open("guestlist.csv","w") guestfile:     guestfile.write('your headers\n')     entries in guestlist:         guestfile.write('%s\n' % entries) 

be aware there many ways fulfil demands, different logics , methodologies.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -