[Tutor] split on tab and new line

kumar s ps_python at yahoo.com
Fri Sep 3 20:10:20 CEST 2004


Dear group,

I have a file1:

A 12 13 [please read one space is one tab here]
B 24 90
C 34 45

File 2:

A Apple
B Boy

Now I want to check if A in file2 is existing in
File1, if then, I want to print corresponding element
of A in File2, Apple and corresponding value of A in
File1.

Desired output:

Apple 12 13
Boy 24 90



My plan:
>f1 = open('file1.txt','r')
>f2 = open('file2.txt','r')

>x = f1.read()
>y = f2.read()

>list1 = split(x,'\t','\n')

here I want to split f1 on both tab and new line just
to get only column 1 of file1. In this case
list1 = ['A','B','C'] 


Similar operation for list2.

Now I have list1 and list2.
m=[]
n=[]
>for i in list1:
      for s in list2:
          if i == s:
            m = m.append(i)
          else:
             print "i did not find i"
      

My question:
1. Is it possinble to split a file on both tab and new
line. This is to get only column 1 of any matrix. If
there is some better method please suggest

2. Is my process good. I dont know myself. 

3. How can I append the values of A in file 1 to names
(column 2 of file 2).

Apple 12 13 ( how can I make Apple print next to
12,13)


Please help. 

Thank you. 

Kumar



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Tutor mailing list