LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-06-2010, 12:54 PM   #1
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Rep: Reputation: 15
Using python to merge two text files line for line


Hi

I'm a bit new to Python programming and hoped that someone might be able to help with a problem I'm having. What I essentially want to do is to combine two text files line for line. I know how to do this in a bash script so to give you a better idea here's the code for that:

Code:
paste -d ';' file1 file2 > merged_file1_and_file2
This is basically for adding on values to the end of a CSV file that uses ';' as the delimiter. So say file1 said:

dog
cat
sheep

and file2 said:

bark
meow
baa

then running this command would create merged_file1_and_file2 which would be:

dog;bark
cat;meow
sheep;baa

The code I'm using at the moment is:

Code:
    
csvraw = open("csvfile.txt", "r")
    stamps = open("delimited.txt", "r")
    
    for line in csvraw:
        print(line.rstrip())
        for line in stamps:
            print(line.rstrip())
As I'm sure any experienced python programmer will see, this prints out the first line of the file "csvraw" and then all of the lines of "stamps" and then the remainder of "csvraw".

What I'd like to do is something like: (pseudo code, I know it's not python ;-))

Code:
for each line in csvraw and stamps:
    print both lines on the same line
Is this possible? I've tried googling and my Python Pocket Reference hasn't been much help. I've looked at pickling but that doesn't seem appropriate.

TIA, Simplified.
 
Old 07-06-2010, 01:09 PM   #2
simplified
Member
 
Registered: May 2007
Location: London, UK
Distribution: Kubuntu 9.04 x64 / Ubuntu Server 9.04 x64
Posts: 50

Original Poster
Rep: Reputation: 15
Sorted!

Just had an epiphany...

Code:
    csvraw = open("csvfile.txt", "r")
    stamps = open("delimited.txt", "r")
    
    for line in csvraw:
        print(line.rstrip() + stamps.readline().strip())
I hope this helps you out if you're stuck!
 
  


Reply

Tags
loop, merge, paste, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
join 2 text files based on first number present in every line of the 2 text files markraem Linux - Software 4 01-25-2010 06:26 AM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM
Python: find defined text string in a file, and replace the whole line Dark Carnival Programming 6 05-22-2007 06:02 AM


All times are GMT -5. The time now is 03:02 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration