School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,212 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,566 people online right now. Registration is fast and FREE... Join Now!




Converting a multiline string into variables...

 

Converting a multiline string into variables..., Python

brianmen

21 Oct, 2009 - 01:36 PM
Post #1

New D.I.C Head
*

Joined: 14 Jun, 2009
Posts: 14



Thanked: 1 times
My Contributions
Hi I have a list of some information that I would like to separate into variables and values. For example, lets say I had a variable containing the string:

"Pairs of Shoes 423,233,134
Golden Watches 32,424,123
Awesome Sunglasses 2,242"

I want to take that string and convert it so I have 3 variables with their values:

pairofshoes=423233134
goldenwatches=32424123
awesomesunglasses=2242

I sense that I should use a "for" blank "in" blank loop and the find function to go through the string but I am unable to figure out how to deal with those commas and spaces. Could you walk me through how to do this or at least give me some tips on using the "find" function effectively? Thank you very much!

User is offlineProfile CardPM
+Quote Post


code_m

RE: Converting A Multiline String Into Variables...

23 Oct, 2009 - 08:34 AM
Post #2

D.I.C Head
**

Joined: 21 Apr, 2009
Posts: 124



Thanked: 8 times
My Contributions
first split the string at new lines:

var = var.split("\n")

get new vars and split at whitespace:

shoes = var[0].split() (repeat as nessary)

You only need the last index position since that is your numbers:

shoes = shoes[-1]

then split at the commas since they are unneeded:

shoes.split(",")

Rejoin string and convert to int:

shoes = int("".join(shoes))


Of course you can combine this into one or two steps, but I split it up for clarity.
User is offlineProfile CardPM
+Quote Post

brianmen

RE: Converting A Multiline String Into Variables...

23 Oct, 2009 - 10:22 AM
Post #3

New D.I.C Head
*

Joined: 14 Jun, 2009
Posts: 14



Thanked: 1 times
My Contributions
QUOTE(code_m @ 23 Oct, 2009 - 08:34 AM) *

first split the string at new lines:

var = var.split("\n")

get new vars and split at whitespace:

shoes = var[0].split() (repeat as nessary)

You only need the last index position since that is your numbers:

shoes = shoes[-1]

then split at the commas since they are unneeded:

shoes.split(",")

Rejoin string and convert to int:

shoes = int("".join(shoes))


Of course you can combine this into one or two steps, but I split it up for clarity.



Oh wow that is incredible! I never thought it could be done in only a few lines!!!! Thank you thank you thank you!!!!!!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 09:48PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month