2013-08-30 67 views
0

我很难试图附加额外的数据到文件。 重复索引数的2倍,并添加22和23,留下的电话号码 对现有线20 例如附加额外的数据到一个文件-python

原始

MYFILE:

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() 

任何帮助将不胜感激。

+0

那么,什么是你的现有代码做错了什么?如果你能解释这一点,它将有助于解决它。 (我的意思是,除了没有用IndentationError编译,还有一个很难猜测你实际意味着什么......) – abarnert

回答

2

只是一些提示,所以你可以自己想出解决方案。 您可以逐行读取输入文件。使用类似:

with open("myfile", "rt") as f, open ("mynewfile", "wt") as g: 
    for line in f: 
    # split line and generate your two new lines 
    print >> g, line 
    print >> g, ... # new line 1 
    print >> g, ... # new line 2 

你需要拆分件中的每一行,使用split了点。

+1

或者,如果他们在python 3.x上,他们会使用print(line,file =克)'。 – SethMMorton

0

方式一:

import fileinput 

with fileinput.input() as f: 
    for line in f: 
     print(line, end='') 
     fields = line.split(r',', 2) 
     for n in [22, 23]: 
      fields[1] = n 
      print(','.join(map(str, fields)), end='') 

它产生:

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,23,123-123-4878, 
+0

谢谢...男人。我知道是太多的要求,但我忘了说,与22,23线不需要电话号码...我将如何改变。 – user2734395

0
for x in open('myfile.txt'): 
    x = x.rstrip() 
    L = x.split(',') 
    print(x) 
    print(L[0], 22, L[2], "\n", sep=',', end='') 
    print(L[0], 23, L[2], "\n", sep=',', end='') 
+0

对不起...我刚试过。我放弃了... 版本2.6.6 给出错误。 文件 “fix_hb_master.py”,第7行 打印(行,端= '') ^ 语法错误:无效的语法 版本:3.0 文件 “fix_hb_master.py”,第5行,在 与的FileInput .input()为f: AttributeError:'FileInput'对象没有属性'__exit__' 有什么想法? – user2734395

+0

一旦我找到正确的版本一切工作正常。我只是将L [2]更改为[3]以清空22,23个字段。 – user2734395