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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gesqo
    New Member
    • Nov 2012
    • 7

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

    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.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Perhaps I don't understand your question. It is somewhat vague.
    Code:
    >>> cell = "adfhyhh ihhgfdtykk red piuol"
    >>> columns = cell.split()
    >>> columns
    ['adfhyhh', 'ihhgfdtykk', 'red', 'piuol']
    >>>

    Comment

    • gesqo
      New Member
      • Nov 2012
      • 7

      #3
      More precise

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


      [imgnothumb]http://bytes.com/attachments/attachment/6736d1353625474/screenshotexcel .jpg[/imgnothumb]
      Attached Files
      Last edited by zmbd; Nov 23 '12, 05:12 AM. Reason: [Z{Made Image Visable in post}]

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        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"?

        Comment

        • gesqo
          New Member
          • Nov 2012
          • 7

          #5
          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».

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

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

            Comment

            • gesqo
              New Member
              • Nov 2012
              • 7

              #7
              Thank you.Seems working.

              Comment

              Working...