0

how can I separate and store strings from a CSV line for example;

I have this string:

data = "product name, description, weight, shipping dest"

and I want to save into python variables like this:

a = "product name"
b = "description"
c = "weight"
d = "shipping dest"

Thanks in advance!

3
  • 1
    If its a csv file you should use the csv module. There are probably a ton of SO answers showing how to do this, so I wont repeat them. Commented Mar 9, 2017 at 1:19
  • Sorry, Its not accutaly a CSV file, just a string received from a barcode seperated by commas. Commented Mar 9, 2017 at 1:21
  • Please read How to Ask and minimal reproducible example. Commented Mar 11, 2017 at 21:48

1 Answer 1

1

Use the string's split function:

data = "product name, description, weight, shipping dest"
a, b, c, d = data.split(', ')

Here's the Python 2.7 reference for it: https://docs.python.org/2/library/stdtypes.html#str.split

And here's the reference for Python 3: https://docs.python.org/3.5/library/stdtypes.html#str.split

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! just what I was looking for , some type of delimiter in python.
Awesome, glad I could help. If it solves your problem, could you accept it as the answer please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.