Python脚本需要帮助添加导出.txt

时间:2014-08-24 11:30:04

标签: python export

f = open('f.txt', 'r')
import re

for line in f:
    u = line.split(':')[0]
    p = line.split(':')[1]
    m = re.search(r'\d+$', u)

    if m is not None:
        m1 = re.search(r'\d+',p)
        if m1 is None:
            print(u + ':' + p.rstrip() + m.group())
f.close()

f = open('f.txt', 'r')
for line in f:
    l = line.rstrip().split(':')[1]
    m = re.search(r'\d+', l)
    if m is None:
        print(line.rstrip() + '123')
f.close()

f = open('f.txt', 'r')
for line in f:
    l = line.rstrip().split(':')[1]
    m = re.search(r'\d+', l)
    if m is None:
        print(line.rstrip() + '1')
f.close()

f = open('f.txt', 'r')
for line in f:
    u = line.split(':')[0]
    p = line.split(':')[1]
    m = re.search(r'\d+$', u)

    if m is not None and len(m.group()) == 4:
        if int(m.group()) < 2013 and int(m.group()) > 1950:
            m1 = re.search(r'\d+$',p)
            if m1 is None:
                print(u + ':' + p.rstrip() + m.group()[2:])
f.close()

f = open('f.txt', 'r')
for line in f:
    s = line.split(':')[1]
    m = re.search(r'\d+', s)

    if m is not None:
        newone = line.split(':')[0] + ':' + re.sub(r'\d+',"", s).rstrip()
        if newone[-1:] != ':':
            print(line.split(':')[0] + ':' + re.sub(r'\d+',"", s).rstrip())
f.close()

这是我制作的.py scrit,它运行正常,但是一旦完成编辑f.txt中的所有行,它就不会导出.txt - 它只是关闭

我可以帮助添加一些代码以使其导出为finished.txt - 提前感谢

1 个答案:

答案 0 :(得分:0)

您没有在代码的任何部分以书面模式打开文件。

你这样做:

e = open('filename.txt', 'w')

然后,写一行:

e.write('example string')