Please excuse the long subject header. I have gotten pretty good at writing data out to csv files but i seem to have stumbled into this block. Anytime a ',' (comma) is used to join values, it shifts over to the next cell in the spreadsheet. What I want to accomplished is for example:
Inside a single cell, write out cat,dog,mouse
Already checked the csv module in python docs and nothing immediately jumped out at me.
any ideas?
Solved! Go to Solution.
...,
Wrap the string in quotes if it has a comma in the value.
You're faster at the typing Robert.
You could make the output tab delimited instead of comma delimited - ('\t' in python). Or maybe enclose that whole string in quotes.
If it's data derived internal to your organization, then I would suggest that you need to address the way that data is getting generated, apply some business rules to that process so that you don't run into issues just like this later on down the road.
The string will not have commas in it. I am trying to insert commas into the string to separate out the values, without shifting the values into consecutive cells. So, the string will be something like this:
e.g. catdogmouse
I'm trying to insert a comma between each word within the same cell. Make sense?
Enclosing in double quotes doesn't work? output = '"cat,dog,mouse"' or single quote double quote cat,dog,mouse double quote single quote.
Are you using python to parse?
ah...I misunderstood.
So, how do you determine what is a word?
Codewise, "catdogmouse" is a word. I'm not aware of any methods you could implement that would just pick out "cat", "dog" or "mouse" from a string. We do that as humans but programmatically I would think you'd need a library/dictionary of possible words that had those three included to test each value.
grief...read Robert's initial post
>>> import string >>> a = ' "cat, mouse, dog" ' # a double-quoted string, surrounded with single quotes...or equivalent >>> b = string.split(a,",") >>> b # yields a list with a single entry [' "cat', ' mouse', ' dog" '] >>> c = "cat, mouse, dog" >>> d = string.split(c,",") # yields a list with 3 entries >>> d ['cat', ' mouse', ' dog']
Examine the csv module, it handles these cases quite well.
I see the OP's question differently...
e.g. "catdogmouse"
I'm trying to insert a comma between each word within the same cell. Make sense?
So, how are we supposed to know what is "a word" within the string? I mean, sure it's easy to add in a "," between words, but you will have to have a dictionary of words to find within the string "catdogmouse". To the computer "catdogmouse" is a word. The computer doesn't know "dog" is a word and you will need some way to tell it what those words are.