user2734395
user2734395

Reputation: 1

appending extra data to a file -python

I having hard time trying to append extra data to a file. duplicate index number 2 times and add 22, and 23, leaving the phone number on the existing line 20. example

original

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,

the new file suppose to be like this 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,

My code:

#!/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()

any help will be appreciated.

Upvotes: 0

Views: 267

Answers (3)

edi_allen
edi_allen

Reputation: 1872

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='')

Upvotes: 0

Birei
Birei

Reputation: 36262

One way:

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='')

It yields:

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,

Upvotes: 0

nickie
nickie

Reputation: 5808

Just some hints, so you can come up to the solution yourself. You can just read the input file line by line. Use something like:

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

You'll need to split each line in pieces, use split for that.

Upvotes: 2

Related Questions