Page 156 -
P. 156

data in files and arrays


           Split each line as you read it


           Each line in the for loop represents a single string containing two
           pieces of information:
                                              Johnny        8.65
                                              Juan          9.12
                                              Joseph        8.45
                                              Stacey        7.81
                                              Aideen        8.05
                                              Zack          7.21
                                              Aaron         8.31


                                              The for loop shredder TM
                                                                        Each line contains a name and a number,
                                              Brad          4.03
                                                                        as a string.
                                              Jim            7.91


                                              Janet            7.49


                                                                        T o isolate the score, you need to
                                                                        cut each line in two.





          You need to somehow extract the score from the string. In each
           line, there is a name, followed by a space, followed by the score. You
           already know how to extract one string from another; you did it for
           Starbuzz back in Chapter 2. And you could do something similar
           here using the find() method and index manipulation, searching
           for the position of a space (' ') character in each line and then
           extracting the substring that follows it.
           Programmers often have to deal with data in strings that contain
           several pieces of data separated by spaces. It’s so common, in fact,   And you'll find that other
           that Python provides a special string method to perform the cutting   programming languages have very
           you need: split().                                          similar mechanisms for breaking up
                                                                       strings.
           Python strings have a built-in split() method.



                                                                                       you are here 4    121
   151   152   153   154   155   156   157   158   159   160   161