Q:

Comparing second column word with other text file

i want to only display the second column ,
this is my code
with open('try.txt', 'rb') as file1:
    file1_data = dict(line.split(None, 2)[1] for line in file1 if line.strip())
print file1_data

This is the text file
E5 Bat One 
E5 BALL Two 
E5 CRICKET Three
E5 Bat03 Four
E5 Bat133 Five

Than i have to find the Bat which is in second column of textfile1 in second text file
Secondtextfile
?Bat03|Batsman
This is the goods of cricket
Usually cricketers use it
it is game
?Balt|Bowler
Both can be use by batsman and bowler
?Bat133|Swimmer
Swiming is the best exercise

So the Bat which is in second column of textfile1 we goto second text file and find Bat03 and Bat133 and ignore value after | , if Bat in second text file before | found than display it
with open('second.txt', 'rb') as file2, open('output.txt', 'wb') as outputfile:
    output = csv.writer(outputfile, delimiter='|')
    for line in file2:
        if line[:1] == '?':
            row = line.strip().split('|')
            key = row[0][1:]
            if key in file1_data:
                output.writerow(row + [file1_data])
        else:
            outputfile.write(line)

Required output
?Bat03|Batsman
        This is the goods of cricket
        This is the goods of cricket
        Usually cricketers use it
        it is game
?Bat133|Swimmer
        Swiming is the best exercise

A:

You have two separate issues.  The first is the one in your title - how to get just the second column's value.
Retrieving the second column's value
The reason your line.split(None, 2)[1:] is returning both the second and third items in the lines is because it's being told to do so.  Using [1:] at the end means return the second item in the list and all subsequent items (because of the : and list slicing.  If you just want to return the second item in the list, use line.split(None, 2)[1].
Testing lines in another file
Your second issue is how to use those values from the first file to test the values in the second file.  Your current code seems to do this for the most part, but you don't have any code in place to capture what might be called the "description" line.  Also, it's not clear why we should be using csv.writer to create a file that doesn't appear to be in .csv format.
with open('second.txt', 'rb') as file2, open('output.txt', 'wb') as outputfile:
    output = ""
    file2lines = file2.readlines()
    for i in range(len(file2lines)):
        line = file2lines[i]
        if line[0] == '?':
            row = line.strip().split('|')
            key = row[0][1:]
            if key in file1_data:
                output += line + "\t" + file2lines[i+1]
    outputfile.write(output)

