如何连接两个字符串并将结果添加到python中的行尾

时间:2013-11-05 12:11:16

标签: python

我有一个包含制表符分隔字符串的文件......

string_one    string_two

我想将文件作为输入,并在每行末尾以新的制表符分隔值返回它,其中包含两个字符串的串联。

到目前为止,我有这个

#concatenate.py

from sys import argv

scriptname, filename = argv

with open(filename) as f:
    for line in f:
        #take the first word
        #take the second word
        #concatenate them and add them to the end of line

我试过

for word in line

获取每个单词但它获取每个单词,如何指定(标记化)每个单词

3 个答案:

答案 0 :(得分:2)

像这样使用splitjoin

with open("Input.txt") as f:
    for line in f:
        print line, "".join(line.split()[:2])

这将打印

string_one    string_two string_onestring_two

编辑:如果文件不是很大,可以执行此操作

lines = []
with open("Input.txt", "r") as f:
    lines = f.readlines()
with open("Input.txt", "w") as f:
    for line in lines:
        line = line.strip()
        f.write(line + "".join(line.split()[:2]) + "\n")

答案 1 :(得分:1)

要将字符串拆分为单词,您可以使用字符串split method

'To split string into words you can use string\'s split method'.split() # returns ['To', 'split', 'string', 'into', 'words', 'you', 'can', 'use', "string's", 'split', 'method']

要连接使用,可以使用+join方法:

line = 'one ' + 'two' # line is 'one two' 
line = ' '.join(['one', 'two']) # line is 'one two' 

答案 2 :(得分:0)

line = line.strip() + '\t' + ''.join(line.split())