#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0

    Python CSV module, add column to the side, not the bottom


    I am new in python, and I need some help. I made a python script that takes two columns from a file and copy them into a "new file". However, every now and then I need to add columns to the "new file". I need to add the columns on the side, not the bottom. My script adds them to the bottom. Someone suggested using CSV, and I read about it, but I can't make it in a way that it adds the new column to the side of the previous columns. Any help is highly appreciated.

    Here is the code that I wrote.

    import sys
    import re

    filetoread = sys.argv[1]
    filetowrite = sys.argv[2]

    newfile = str(filetowrite) + ".txt"

    openold = open(filetoread,"r")
    opennew = open(newfile,"a")

    rline = openold.readlines()

    number = int(len(rline))
    start = 0

    for i in range (len(rline)) :
    if "2theta" in rline[i] :
    start = i

    for line in rline[start + 1 : number] :
    words = line.split()
    word1 = words[1]
    word2 = words[2]
    opennew.write (word1 + " " + word2 + "\n")

    openold.close()
    opennew.close()


    here is the second code I wrote using CSV

    import sys
    import re
    import csv

    filetoread = sys.argv[1]
    filetowrite = sys.argv[2]

    newfile = str(filetowrite) + ".txt"

    openold = open(filetoread,"r")

    rline = openold.readlines()

    number = int(len(rline))
    start = 0

    for i in range (len(rline)) :
    if "2theta" in rline[i] :
    start = i

    words1 = []
    words2 = []

    for line in rline[start + 1 : number] :
    words = line.split()
    word1 = words[1]
    word2 = words[2]
    words1.append([word1])
    words2.append([word2])

    with open(newfile, 'wb') as file:
    writer = csv.writer(file, delimiter= "\n")
    writer.writerow(words1)
    writer.writerow(words2)


    My first script works "almost" great, except that it writes the new columns at the bottom and I need them at side of the previous columns.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    Use executable Iverson notation, J instead of python.
    Code:
       [A=: i. 4 5   NB. make an array  A
     0  1  2  3  4
     5  6  7  8  9
    10 11 12 13 14
    15 16 17 18 19
       
    
       [B=: 100 + i. 4 _3   NB. another array, B
    102 101 100
    105 104 103
    108 107 106
    111 110 109
       
    
       A , B       NB. concatenated with default fill 0
      0   1   2  3  4
      5   6   7  8  9
     10  11  12 13 14
     15  16  17 18 19
    102 101 100  0  0
    105 104 103  0  0
    108 107 106  0  0
    111 110 109  0  0
       
    
       A ,. B    NB. stitched
     0  1  2  3  4 102 101 100
     5  6  7  8  9 105 104 103
    10 11 12 13 14 108 107 106
    15 16 17 18 19 111 110 109
    Oh well, this is a python forum. Your program needs to read the data with the lines to which you want to append data. And then you need to rewrite the entire file argv[2]. Also there's a good chance I didn't understand your intent.

    command line
    Code:
    $ python p.py a b
    Originally Posted by a input
    2theta
    a b c d e f
    g h i j
    Originally Posted by b.txt input
    I have the same row count as the other file
    has lines following the line with '2theta'.
    Originally Posted by b.txt output
    I have the same row count as the other file b c
    has lines following the line with '2theta'. h i
    Originally Posted by b.txt.bak output
    I have the same row count as the other file
    has lines following the line with '2theta'.
    program
    Code:
    # the link at my signature is supposed to explain how to post python
    
    import sys
    import os
    
    filetoread = sys.argv[1]
    filetowrite = sys.argv[2]
    
    newfile = str(filetowrite) + ".txt"
    
    with open(filetoread,"r") as openold:     # context closes the file
        rline = openold.readlines()
    
    start = 0
    for (i,Line,) in enumerate(rline):        # enumerate function easier
        if "2theta" in rline[i] :
            start = i
            break
    
    # read the "newfile".  You need the lines to append data.
    with open(newfile,'r') as inf:
        change_these_rows = inf.readlines()
    
    for (i,L,) in enumerate(change_these_rows):
        # insertion is a list of words 1 and 2
        insertion = rline[start+1+i].split()[1:3]
        # remove the line end from the row, make a list of the 3 pieces, join them with space, and attach a new line end character.
        change_these_rows[i] = ' '.join([L[:-1]]+insertion)+'\n'
    
    # make a backup file!
    print('delete 1/0 if writing a new file is ok')
    1/0
    os.rename(newfile,newfile+'.bak')
    
    with open(newfile,'w') as ouf:           # again, use the file context
        ouf.writelines(change_these_rows)
    Last edited by b49P23TIvg; November 30th, 2012 at 03:24 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    8
    Rep Power
    0
    I am getting this error with the code you suggested me.

    Traceback (most recent call last):
    File "paula9.py", line 27, in <module>
    insertion = rline[(start+1)+i].split()[1:3]
    IndexError: list index out of range
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    2theta needed to appear,
    and you needed to know that LIST[0] is the first object in the list, and I didn't incorporate error tests for bad input.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo