从每一行删除特定文本

时间:2013-02-27 20:38:24

标签: python

我有一个这种格式的txt文件:

something text1 pm,bla1,bla1
something text2 pm,bla2,bla2
something text3 am,bla3,bla3
something text4 pm,bla4,bla4

并在我想要保留的新文件中:

bla1,bla1
bla2,bla2
bla3,bla3
bla4,bla4

我有这个,它保存前10个字符,例如每行。我可以改变这个或任何其他想法吗?

with open('example1.txt', 'r') as input_handle:
    with open('example2.txt', 'w') as output_handle:
        for line in input_handle:
            output_handle.write(line[:10] + '\n')

5 个答案:

答案 0 :(得分:3)

这就是csv模块的用途。

import csv
reader = csv.reader(open('file.csv'))

for row in reader: print(row[1])

然后您可以使用shell将文件的输出重定向到新文件,或者您可以执行类似这样的操作而不是最后一行:

for row in reader:
    with open('out.csv','w+') as f:
        f.write(row[1]+'\n')

答案 1 :(得分:1)

如果格式已修复:

with open('example1.txt', 'r') as input_handle:
    with open('example2.txt', 'w') as output_handle:
        for line in input_handle:
            if line:  # and maybe some other format check
                od = line.split(',', 1)
                output_handle.write(od[1] + "\n")

答案 2 :(得分:1)

以下是我的写作方式。

Python 2.7

import csv
with open('example1.txt', 'rb') as f_in, open('example2.txt', 'wb') as f_out:
    writer = csv.writer(f_out)
    for row in csv.reader(f_in):
        writer.write(row[-2:]) # keeps the last two columns

Python 3.x (注意open参数的差异)

import csv
with open('example1.txt', 'r', newline='') as f_in:
    with open('example2.txt', 'w', newline='') as f_out:
        writer = csv.writer(f_out)
        for row in csv.reader(f_in):
           writer.write(row[-2:]) # keeps the last two columns

答案 3 :(得分:1)

从文件中删除第一个“,” - 分隔列:

first, sep, rest = line.partition(",")
if rest: # don't write lines with less than 2 columns
   output_handle.write(rest)

答案 4 :(得分:0)

尝试:

output_handle.write(line.split(",", 1)[1])

来自the docs

  

str.split([ [, maxsplit ]])

     

使用 sep 作为分隔符字符串,返回字符串中的单词列表。如果给出 maxsplit ,则最多 maxsplit 分割完成(因此,列表最多只有maxsplit+1个元素。)