splitting a long string into a list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ronrsr

    #1

    splitting a long string into a list

    I have a single long string - I'd like to split it into a list of
    unique keywords. Sadly, the database wasn't designed to do this, so I
    must do this in Python - I'm having some trouble using the .split()
    function, it doesn't seem to do what I want it to - any ideas?

    thanks very much for your help.

    r-sr-


    longstring = 'Agricultural subsidies; Foreign aidAgriculture;
    Sustainable Agriculture - Support; Organic Agriculture; Pesticides, US,
    Childhood Development, Birth Defects; Toxic ChemicalsAntibi otics,
    AnimalsAgricult ural Subsidies, Global TradeAgricultur al
    SubsidiesBiodiv ersityCitizen ActivismCommuni ty
    GardensCooperat ivesDietingAgri culture, CottonAgricultu re, Global
    TradePesticides , MonsantoAgricul ture, SeedCoffee, HungerPollution ,
    Water, FeedlotsFood PricesAgricultu re, WorkersAnimal Feed, Corn,
    PesticidesAquac ultureChemical
    WarfareCompostD ebtConsumerismF earPesticides, US, Childhood Development,
    Birth DefectsCorporat e Reform, Personhood (Dem. Book)Corporate Reform,
    Personhood, Farming (Dem. Book)Crime Rates, Legislation,
    EducationDebt, Credit CardsDemocracyP opulation, WorldIncomeDemo cracy,
    Corporate Personhood, Porter Township (Dem. Book)Disaster
    ReliefDwellings , SlumsEconomics, MexicoEconomy, LocalEducation,
    ProtestsEndange red Habitat, RainforestEndan gered SpeciesEndanger ed
    Species, Extinctionantib iotics, livestockAgricu ltural subsidies;
    Foreign aid;Agriculture ; Sustainable Agriculture - Support; Organic
    Agriculture; Pesticides, US, Childhood Development, Birth Defects;
    Toxic Chemicals;Antib iotics, Animals;Agricul tural Subsidies, Global
    Trade;Agricultu ral Subsidies;Biodi versity;Citizen Activism;Commun ity
    Gardens;Coopera tives;Dieting;A griculture, Cotton;Agricult ure, Global
    Trade;Pesticide s, Monsanto;Agricu lture, Seed;Coffee, Hunger;Pollutio n,
    Water, Feedlots;Food Prices;Agricult ure, Workers;Animal Feed, Corn,
    Pesticides;Aqua culture;Chemica l
    Warfare;Compost ;Debt;Consumeri sm;Fear;Pestici des, US, Childhood
    Development, Birth Defects;Corpora te Reform, Personhood (Dem.
    Book);Corporate Reform, Personhood, Farming (Dem. Book);Crime Rates,
    Legislation, Education;Debt, Credit Cards;Democracy ;Population,
    World;Income;De mocracy, Corporate Personhood, Porter Township (Dem.
    Book);Disaster Relief;Dwelling s, Slums;Economics , Mexico;Economy,
    Local;Education , Protests;Endang ered Habitat, Rainforest;Enda ngered
    Species;Endange red Species, Extinction;anti biotics,
    livestock;Pesti cides, Water;Environme nt, Environmentalis t;Food, Hunger,
    Agriculture, Aid, World, Development;Agr iculture, Cotton
    Trade;Agricultu re, Cotton, Africa;Environm ent, Energy;Fair Trade (Dem.
    Book);Farmland, Sprawl;Fast Food, Globalization, Mapping;depress ion,
    mental illness, mood disorders;Econo mic Democracy, Corporate
    Personhood;Braz il, citizen activism, hope, inspiration, labor
    issues;citizen activism, advice, hope;Pharmaceut icals, Medicine,
    Drugs;Community Investing;Envir onment, Consumer Waste Reduction,
    Consumer Behavior and Taxes;Hunger, US, Poverty;FERTILI TY,
    Women;Agricultu ral subsidies; Foreign aid;Agriculture ; Sustainable
    Agriculture - Support; Organic Agriculture; Pesticides, US, Childhood
    Development, Birth Defects; Toxic Chemicals;Antib iotics,
    Animals;Agricul tural Subsidies, Global Trade;Agricultu ral
    Subsidies;Biodi versity;Citizen Activism;Commun ity
    Gardens;Coopera tives;Dieting;A gricultural subsidies; Foreign
    aid;Agriculture ; Sustainable Agriculture - Support; Organic
    Agriculture; Pesticides, US, Childhood Development, Birth Defects;
    Toxic Chemicals;Antib iotics, Animals;Agricul tural Subsidies, Global
    Trade;Agricultu ral Subsidies;Biodi versity;Citizen Activism;Commun ity
    Gardens;Coopera tives;Dieting;A griculture, Cotton;Agricult ure, Global
    Trade;Pesticide s, Monsanto;Agricu lture, Seed;Coffee, Hunger;Pollutio n,
    Water, Feedlots;Food Prices;Agricult ure, Workers;Animal Feed, Corn,
    Pesticides;Aqua culture;Chemica l
    Warfare;Compost ;Debt;Consumeri sm;Fear;Pestici des, US, Childhood
    Development, Birth Defects;Corpora te Reform, Personhood (Dem.
    Book);Corporate Reform, Personhood, Farming (Dem. Book);Crime Rates,
    Legislation, Education;Debt, Credit Cards;'

  • Cameron Walsh

    #2
    Re: splitting a long string into a list

    ronrsr wrote:
    I have a single long string - I'd like to split it into a list of
    unique keywords. Sadly, the database wasn't designed to do this, so I
    must do this in Python - I'm having some trouble using the .split()
    function, it doesn't seem to do what I want it to - any ideas?
    >
    thanks very much for your help.
    >
    r-sr-
    >
    >
    longstring = '<SNIP rather long string>'
    What do you want it to do? Split on each semicolon?

    a = longstring.spli t(";")
    for element in a:
    print element

    Agricultural subsidies
    Foreign aidAgriculture
    Sustainable Agriculture - Support
    Organic Agriculture
    Pesticides, US,Childhood Development, Birth Defects
    Toxic ChemicalsAntibi otics,AnimalsAg ricultural Subsidies, Global
    TradeAgricultur alSubsidiesBiod iversityCitizen
    ActivismCommuni tyGardensCooper ativesDietingAg riculture,
    CottonAgricultu re, GlobalTradePest icides, MonsantoAgricul ture,
    SeedCoffee, HungerPollution ,Water, FeedlotsFood PricesAgricultu re,
    WorkersAnimal Feed,
    Corn,Pesticides AquacultureChem icalWarfareComp ostDebtConsumer ismFearPesticid es,
    US, Childhood Development,Bir th DefectsCorporat e Reform, Personhood
    (Dem. Book)Corporate Reform, Personhood, Farming (Dem. Book)Crime Rates,
    Legislation,Edu cationDebt, Credit CardsDemocracyP opulation,
    WorldIncomeDemo cracy,Corporate Personhood, Porter Township (Dem.
    Book)DisasterRe liefDwellings, SlumsEconomics, MexicoEconomy,
    LocalEducation, ProtestsEndange red Habitat, RainforestEndan gered
    SpeciesEndanger edSpecies, Extinctionantib iotics, livestockAgricu ltural
    subsidies
    Foreign aid
    Agriculture
    Sustainable Agriculture - Support
    OrganicAgricult ure
    Pesticides, US, Childhood Development, Birth Defects
    Toxic Chemicals
    <etc.>

    I think the problem arises because your string has the following problems:

    1.) Inconsistent spaces between words (some are non-existent)
    2.) Inconsistent separators between elements (sometimes semi-colons,
    sometimes commas, but commas appear to belong to elements, sometimes no
    clear separator at all)

    Basically, this problem is not solvable by computer with currently
    available resources. There is no way Python or anything else can know
    which words are meant to be together and which are not, when there are
    no separators between elements and no separators between words within
    those elements.

    You need to find a new way of generating the string, or do it by hand.

    How did you get the string?

    Cameron.

    Comment

    Working...