By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,479 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,479 IT Pros & Developers. It's quick & easy.

How do I split a column of a csv file in excel using python 3.3?

gesqo
P: 7
I have a column of some hundreds rows, obtained by a python program,each cell containing 4 unequal (in length) strings,like:
adfhyhh ihhgfdtykk red piuol
How can I split it with python in 4 colomns,with each cell containing one string?
Thanks.
Nov 22 '12 #1
Share this Question
Share on Google+
6 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
Perhaps I don't understand your question. It is somewhat vague.
Expand|Select|Wrap|Line Numbers
  1. >>> cell = "adfhyhh ihhgfdtykk red piuol"
  2. >>> columns = cell.split()
  3. >>> columns
  4. ['adfhyhh', 'ihhgfdtykk', 'red', 'piuol']
  5. >>> 
Nov 22 '12 #2

gesqo
P: 7
This is a better illustration of what I need:
A B C D E
1 adfhyhh*ihhgfdtykk*red*piuol adfhyhh ihhgfdt red piuol
2 ehethe tthy tytuiuu aaaaaaa ehethe tthy tytu aaaaa


Attached Images
File Type: jpg ScreenShotexcel.jpg (10.2 KB, 2561 views)
Nov 22 '12 #3

bvdet
Expert Mod 2.5K+
P: 2,851
You can directly read the Excel workbook contents with xlrd, and the data is formatted so you can access the cell elements. What does your data look like after is is read by "a python program"?
Nov 23 '12 #4

gesqo
P: 7
I didn't say «read by a python program»,so I don't undrestand well your question.
If you meant how I created the cv file:with «f.write('file.csv','w')».
I expect a solution smth like «column.split».
Nov 23 '12 #5

bvdet
Expert Mod 2.5K+
P: 2,851
"What does your data look like?" Since you don't understand me and I don't understand you, I'll take a guess.
Expand|Select|Wrap|Line Numbers
  1. data = """3d3cd455 c6dd 4390 08712b657ec7
  2. 6b9754c7 761f 4b76 fe0c41e1fecb
  3. 05a9bd30 69c3 4631 73916e968dfe
  4. 047db4fd 3bb3 4bec 719313b2928b
  5. db9e189c 0c8b 4fb0 b1c1d20e8d49
  6. 3427aeca d649 4286 4e2df66de67b
  7. 037189cd 63ec 4d5c bb7b8255c169
  8. e95a4329 98d8 4ab3 1f4e902c147e
  9. 376773f8 a7da 4915 ec20af56c02b"""
You can create 4 columns in each row by replacing the space character with a comma.
Expand|Select|Wrap|Line Numbers
  1. data = data.replace(" ", ",")
If you want to do it in a more complicated way:
Expand|Select|Wrap|Line Numbers
  1. "\n".join([",".join(item.split()) for item in data.split("\n")])
Nov 23 '12 #6

gesqo
P: 7
Thank you.Seems working.
Nov 23 '12 #7

Post your reply

Sign in to post your reply or Sign up for a free account.