我很难尝试将额外数据附加到文件中。将索引号复制 2 次并添加 22 和 23,将电话号码留在现有线路 20 上。示例
原来的
我的文件:
1,20,323.454.1234,
2,20,212.333.3333,
3,20,212.222.4444,
4,20,850.234.3881,
5,20,850.111.3881,
6,20,510-222-5241,
7,20,510-343-5241,
8,20,678-456-3555,
9,20,678-123-3555,
10,20,123-123-4878,
新文件应该是这样的 mynewfile:
1,20,323.454.1234,
1,22,323.454.1234,
1,23,323.454.1234,
2,20,212.333.3333,
2,22,212.333.3333,
2,23,212.333.3333,
3,20,212.222.4444,
3,22,212.222.4444,
3,23,212.222.4444,
4,20,850.234.3881,
4,22,850.234.3881,
4,23,850.234.3881,
5,20,850.111.3881,
5,22,850.111.3881,
5,23,850.111.3881,
6,20,510-222-5241,
6,22,510-222-5241,
6,23,510-222-5241,
7,20,510-343-5241,
7,22,510-343-5241,
7,23,510-343-5241,
8,20,678-456-3555,
8,22,678-456-3555,
8,23,678-456-3555,
9,20,678-123-3555,
9,22,678-123-3555,
9,23,678-123-3555,
10,20,123-123-4878,
10,22,123-123-4878,
10,20,123-123-4878,
我的代码:
#!/usr/bin/python
import re
myfile = open('myfile', 'r+')
lines = myfile.readlines()
sentences = []
for line in lines:
if line:
sentences.insert(line + '22')
for line in lines:
if line:
sentences.insert(line + '23')
myfile.close()
outputfile = open('mynewfile', 'w+')
if len(sentences) > 0:
for sentence in sentences:
outputfile.write(sentence)
outputfile.close()
任何帮助将不胜感激。