Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 29th, 2012, 10:21 PM
ufloridagator ufloridagator is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 ufloridagator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 10 sec
Reputation 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.

Reply With Quote
  #2  
Old November 30th, 2012, 03:22 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 22 m 15 sec
Reputation Power: 383
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

Quote:
Originally Posted by a input
2theta
a b c d e f
g h i j

Quote:
Originally Posted by b.txt input
I have the same row count as the other file
has lines following the line with '2theta'.

Quote:
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

Quote:
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)
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : November 30th, 2012 at 03:24 PM.

Reply With Quote
  #3  
Old December 1st, 2012, 03:09 PM
ufloridagator ufloridagator is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 8 ufloridagator User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 4 m 10 sec
Reputation 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

Reply With Quote
  #4  
Old December 1st, 2012, 03:46 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 22 m 15 sec
Reputation Power: 383
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python CSV module, add column to the side, not the bottom

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap