python - How do I split a string BEFORE a certain character? -
timeline = ''' 1961 - second child, john, born. 1963 - ordained presbyterian minister. makes on-camera debut host of series of 15-minute episodes children produced in toronto. program titled "mister rogers." 1964 - returns pittsburgh , turns 15-minute show half-hour "mister rogers' neighborhood." 1968 - "mister rogers' neighborhood" debuts on pbs , wins first of several emmy awards. rogers appointed chairman of forum on mass media , child development of white house conference on youth. 1969 - wins first of 2 george foster peabody awards television excellence. fred rogers readies opening pitch start pirates' season in 1988. (post-gazette archives) 1971 - forms production company, family communications inc. 1975 - ceases production on "mister rogers' neighborhood," continues air on pbs in reruns. 1978 - creates pbs series "old friends, new friends," focusing on older people. 1979 - production resumes on "mister rogers' neighborhood." 1981 - eddie murphy plays inner-city children's show host in "mister robinson's neighborhood" sketch on "saturday night live," famous of many "mister rogers" spoofs. 1984 - smithsonian institution in washington makes fred rogers' trademark sweater part of permanent collection. rogers forces fast-food chain burger king remove ads featuring mister rogers lookalike. 1987 - appears on children's tv show in soviet union , reciprocates having soviet host appear on show later in year. 1989 - "mister rogers' neighborhood of make believe" opens attraction @ idlewild park. 1990 - sues ku klux klan , forces organization stop playing racist telephone recordings featuring imitations of fred rogers' voice, speech patterns , theme song. 1991 - tapes spots pbs on eve of gulf war reassure children right if hostilities occur. 1996 - tv guide names fred rogers 1 of 50 greatest tv stars of time. rogers makes appearance other himself when takes cameo role preacher on drama series "dr. quinn, medicine woman," says 1 of favorite shows. 1997 - honored lifetime achievement national academy of television arts , sciences (who hand out emmy awards) , television critics association. named pittsburgher of year pittsburgh magazine. 1998 - gets star on hollywood walk of fame. pittsburgh children's museum opens mister rogers exhibit. 1999 - inducted television hall of fame. 2000 - unveils planetarium show featuring "neighborhood" characters @ carnegie science center; ceases production on "mister rogers' neighborhood." 2001 - last original episodes of "mister rogers' neighborhood" airs on pbs. 2002 - in july, presented presidential medal of freedom, nation's highest civilian honor. filmed pbs public service announcements in august encouraging parents read children , giving them advice on how handle first anniversary of sept. 11, 2001, terrorist attacks. named grand marshal of tournament of roses parade in pasadena, calif., bill cosby , art linkletter. theme: "children's wishes, dreams , imagination." 2003 - dies of stomach cancer @ age 74. ''' my goal make dictionary keys years , values events on timeline. however, i'm having little trouble.
i want first split timeline list so: {key, value, key, value} , turn dict. want split string 5 or 6 characters before " - " in order accomplish this, have no idea how that. can me?
edit: came possible solution -
timeline = timeline.replace("\n","") timeline = timeline.replace("\\","") timeline = timeline.replace("19","zz19") timeline = timeline.replace("20","zz20") timeline = timeline.split(" - ") so end this:
['zz1961', 'second child, john, born. zz1963', 'is ordained presbyterian minister. makes on-camera debut host of series of 15-minute episodes children produced in toronto. program titled "mister rogers." zz1964', 'returns pittsburgh , turns 15-minute show half-hour "mister rogers\' neighborhood." zz1968', '"mister rogers\' neighborhood" debuts on pbs , wins first of several emmy awards. rogers appointed chairman of forum on mass media , child development of white house conference on youth. zz1969', "wins first of 2 george foster peabody awards television excellence. fred rogers readies opening pitch start pirates' season in zz1988. (post-gazette archives) zz1971", 'forms production company, family communications inc. zz1975', 'ceases production on "mister rogers\' neighborhood," continues air on pbs in reruns. zz1978', 'creates pbs series "old friends, new friends," focusing on older people. zz1979', 'production resumes on "mister rogers\' neighborhood." zz1981', 'eddie murphy plays inner-city children\'s show host in "mister robinson\'s neighborhood" sketch on "saturday night live," famous of many "mister rogers" spoofs. zz1984', "the smithsonian institution in washington makes fred rogers' trademark sweater part of permanent collection. rogers forces fast-food chain burger king remove ads featuring mister rogers lookalike. zz1987", "appears on children's tv show in soviet union , reciprocates having soviet host appear on show later in year. zz1989", '"mister rogers\' neighborhood of make believe" opens attraction @ idlewild park. zz1990', "sues ku klux klan , forces organization stop playing racist telephone recordings featuring imitations of fred rogers' voice, speech patterns , theme song. zz1991", 'tapes spots pbs on eve of gulf war reassure children right if hostilities occur. zz1996', 'tv guide names fred rogers 1 of 50 greatest tv stars of time. rogers makes appearance other himself when takes cameo role preacher on drama series "dr. quinn, medicine woman," says 1 of favorite shows. zz1997', 'is honored lifetime achievement national academy of television arts , sciences (who hand out emmy awards) , television critics association. named pittsburgher of year pittsburgh magazine. zz1998', "gets star on hollywood walk of fame. pittsburgh children's museum opens mister rogers exhibit. zz1999", 'is inducted television hall of fame. zz2000', 'unveils planetarium show featuring "neighborhood" characters @ carnegie science center; ceases production on "mister rogers\' neighborhood." zz2001', 'last original episodes of "mister rogers\' neighborhood" airs on pbs. zz2002', 'in july, presented presidential medal of freedom, nation\'s highest civilian honor. filmed pbs public service announcements in august encouraging parents read children , giving them advice on how handle first anniversary of sept. 11, zz2001, terrorist attacks. named grand marshal of tournament of roses parade in pasadena, calif., bill cosby , art linkletter. theme: "children\'s wishes, dreams , imagination." zz2003', 'dies of stomach cancer @ age 74.'] so want split string "zz", problem can't split twice because it's list. concatenate list, run same problem now.
tried doing this:
for in timeline: = i.split("zz") but doesn't work :(
you make dictionary this:
parts = timeline.replace('\n','').split(" - ") d = dict() x in range(len(parts) - 1): d[parts[x][-4:]] = parts[x + 1][:len(parts[x+1]) - 4] since delimiter " - " , doing have set of items like:
'1961' 'blablablablabla. 1963' 'blablablablabla. 1968' 'blablablablabla. 1969' 'blablablablabla' then need pair last 4 characters in text next text except last 4 characters. thus, simple split , slice work you.
using show result:
for in d: print(a + " " + d[a]) this got:
1975 ceases production on "mister rogers' neighborhood," continues air on pbs in reruns. 1968 "mister rogers' neighborhood" debuts on pbs , wins first of several emmy awards. rogers appointed chairman of forum on mass media , child development of white house conference on youth. 2001 last original episodes of "mister rogers' neighborhood" airs on pbs. 1990 sues ku klux klan , forces organization stop playing racist telephone recordings featuring imitations of fred rogers' voice, speech patterns , theme song. 2002 in july, presented presidential medal of freedom, nation's highest civilian honor. filmed pbs public service announcements in august encouraging parents read children , giving them advice on how handle first anniversary of sept. 11, 2001, terrorist attacks. named grand marshal of tournament of roses parade in pasadena, calif., bill cosby , art linkletter. theme: "children's wishes, dreams , imagination." 1989 "mister rogers' neighborhood of make believe" opens attraction @ idlewild park. 1996 tv guide names fred rogers 1 of 50 greatest tv stars of time. rogers makes appearance other himself when takes cameo role preacher on drama series "dr. quinn, medicine woman," says 1 of favorite shows. 1987 appears on children's tv show in soviet union , reciprocates having soviet host appear on show later in year. 1963 ordained presbyterian minister. makes on-camera debut host of series of 15-minute episodes children produced in toronto. program titled "mister rogers." 1984 smithsonian institution in washington makes fred rogers' trademark sweater part of permanent collection. rogers forces fast-food chain burger king remove ads featuring mister rogers lookalike. 1971 forms production company, family communications inc. 1991 tapes spots pbs on eve of gulf war reassure children right if hostilities occur. 1997 honored lifetime achievement national academy of television arts , sciences (who hand out emmy awards) , television critics association. named pittsburgher of year pittsburgh magazine. 1999 inducted television hall of fame. 1964 returns pittsburgh , turns 15-minute show half-hour "mister rogers' neighborhood." 2000 unveils planetarium show featuring "neighborhood" characters @ carnegie science center; ceases production on "mister rogers' neighborhood." 1981 eddie murphy plays inner-city children's show host in "mister robinson's neighborhood" sketch on "saturday night live," famous of many "mister rogers" spoofs. 1978 creates pbs series "old friends, new friends," focusing on older people. 1969 wins first of 2 george foster peabody awards television excellence. fred rogers readies opening pitch start pirates' season in 1988. (post-gazette archives) 1961 second child, john, born. 2003 dies of stomach cancer @ age 1998 gets star on hollywood walk of fame. pittsburgh children's museum opens mister rogers exhibit. 1979 production resumes on "mister rogers' neighborhood." note dictionary unordered. use ordereddict if need ordered.
the ordered dictionary done dictionary this:
import collections parts = timeline.replace('\n','').split(" - ") od = collections.ordereddict() x in range(len(parts) - 1): od[parts[x][-4:]] = parts[x + 1][:len(parts[x+1]) - 4] and using this:
for in od: print(a + " " + od[a]) the result ordered:
1961 second child, john, born. 1963 ordained presbyterian minister. makes on-camera debut host of series of 15-minute episodes children produced in toronto. program titled "mister rogers." 1964 returns pittsburgh , turns 15-minute show half-hour "mister rogers' neighborhood." 1968 "mister rogers' neighborhood" debuts on pbs , wins first of several emmy awards. rogers appointed chairman of forum on mass media , child development of white house conference on youth. 1969 wins first of 2 george foster peabody awards television excellence. fred rogers readies opening pitch start pirates' season in 1988. (post-gazette archives) 1971 forms production company, family communications inc. 1975 ceases production on "mister rogers' neighborhood," continues air on pbs in reruns. 1978 creates pbs series "old friends, new friends," focusing on older people. 1979 production resumes on "mister rogers' neighborhood." 1981 eddie murphy plays inner-city children's show host in "mister robinson's neighborhood" sketch on "saturday night live," famous of many "mister rogers" spoofs. 1984 smithsonian institution in washington makes fred rogers' trademark sweater part of permanent collection. rogers forces fast-food chain burger king remove ads featuring mister rogers lookalike. 1987 appears on children's tv show in soviet union , reciprocates having soviet host appear on show later in year. 1989 "mister rogers' neighborhood of make believe" opens attraction @ idlewild park. 1990 sues ku klux klan , forces organization stop playing racist telephone recordings featuring imitations of fred rogers' voice, speech patterns , theme song. 1991 tapes spots pbs on eve of gulf war reassure children right if hostilities occur. 1996 tv guide names fred rogers 1 of 50 greatest tv stars of time. rogers makes appearance other himself when takes cameo role preacher on drama series "dr. quinn, medicine woman," says 1 of favorite shows. 1997 honored lifetime achievement national academy of television arts , sciences (who hand out emmy awards) , television critics association. named pittsburgher of year pittsburgh magazine. 1998 gets star on hollywood walk of fame. pittsburgh children's museum opens mister rogers exhibit. 1999 inducted television hall of fame. 2000 unveils planetarium show featuring "neighborhood" characters @ carnegie science center; ceases production on "mister rogers' neighborhood." 2001 last original episodes of "mister rogers' neighborhood" airs on pbs. 2002 in july, presented presidential medal of freedom, nation's highest civilian honor. filmed pbs public service announcements in august encouraging parents read children , giving them advice on how handle first anniversary of sept. 11, 2001, terrorist attacks. named grand marshal of tournament of roses parade in pasadena, calif., bill cosby , art linkletter. theme: "children's wishes, dreams , imagination." 2003 dies of stomach cancer @ age
Comments
Post a Comment