用python将文件拆分成带有计数器的新行

时间:2014-11-06 01:25:52

标签: python tabs split line

我想将包含制表符分隔的单词列表的文件拆分为“list”,其中单词前面带有数字。

因此,如果输入文件包含此内容(单词之间的空格是制表符): 树车房毯子

我想要这个输出:

1 tree
2  car
3  house
4 blanket

我有这个代码可以打印出“单词列表”,但不知道如何在单词前面找到计数器:

#!/usr/bin/env python

import csv

with open ("commonwords.tsv") as file:
  for line in file:
    print line.replace("\t","\n") 

由于

3 个答案:

答案 0 :(得分:1)

您可以使用enumerate

import csv
with open("commonwords.tsv") as f:
    for line in f:
        line = line.replace("\t", "\n")
        for ind, word in enumerate(line.split(), 1):
            print "{0} {1}".format(ind, word)
1 tree
2 car
3 house
4 blanket

不确定您是否希望计数重置每一行或继续结束:

with open("commonwords.tsv") as f:
    line = f.read().replace("\t", "\n")
    for ind, word in enumerate(line.split(), 1):
        print "{0} {1}".format(ind, word)

您也可以在不更换的情况下拆分:

with open("commonwords.tsv") as f:
    lines = f.read().split()
    for ind, word in enumerate(lines, 1):
        print "{0} {1}".format(ind, word)

答案 1 :(得分:1)

枚举函数可以为你计算单词,但是你需要一个迭代器或一个单词列表,而不仅仅是文件的行。这是一个生成器,它遍历csv文件的行并分别输出每一列。它通过枚举来获得结果。

import csv

def yield_col(reader):
    for row in reader:
        for item in row:
            yield item

with open ("commonwords.tsv") as fp:
    reader = csv.reader(fp, dialect='excel-tab')
    for num, word in enumerate(yield_col(reader), 1):
        print num, word

答案 2 :(得分:0)

import csv
import itertools

with open('commonwords.tsv') as infile, open('/path/to/output', 'w') as outfile:
    writer = csv.writer(outfile, delimiter='\t')
    count = itertools.count(1)
    for row in csv.reader(infile, delimiter'\t'):
        for word in row:
            writer.writerow([next(count), word])