Add a new column to comma delimited text file.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • osj104117
    New Member
    • Mar 2018
    • 1

    Add a new column to comma delimited text file.

    Hi.. I have a file with two columns separated by comma with numerical values (sample) -
    2305,11770
    2306,12017
    2307,9416.6
    . , .
    . , .
    . , .

    I willing to have an output file somewhat like this -
    bfe,2305,hgen,, 11770
    bfe,2306,hgen,, 12017
    bfe,2307,hgen,, 9416.6
    and so on...

    Thank you in advance...
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    There are a number of string manipulation methods that you can use in Python. Check out this quick reference for String Manipulation in Python.

    What you could do is use the split method on each string you get from the file on the , character which would give you an array of 2 words.

    Then you would make a new string by concatenating the values into the right positions.

    For example:
    Code:
    oldString = "2305,11770"
    oldStringParts = oldString.Split(',')
    newString = "bfe," + oldStringParts[0] + ",hgen,," + olStringParts[1]
    There is probably a more elegant way of doing this with string replacement but I think this is the easiest to understand if you are new to programming.

    Comment

    Working...