0

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

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

获取每个单词但获取每个字符,我如何指定(标记)每个单词

4

3 回答 3

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")
于 2013-11-05T12:12:01.563 回答
1

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

'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' 
于 2013-11-05T12:21:02.313 回答
0
line = line.strip() + '\t' + ''.join(line.split())
于 2013-11-05T12:18:05.130 回答