5

I have a file with 196 list in it,and I want to create new 196 output files and write each of the list in a new file, so that I will have 196 output files each containing 1 list of input data Here is the input file:

"['128,129', '116,118', '108,104', '137,141', '157,144', '134,148', '138,114', '131,138', '248,207', '208,247', '246,248', '101,106', '131,115', '119,120', '131,126', '138,137', '132,129']"
"['154,135', '151,147', '236,244', '243,238', '127,127', '125,126', '122,124', '123,126', '127,129', '122,121', '147,134', '126,132', '128,137', '233,222', '222,236', '125,126']"

.....here for eg, I have given only 2 list but total 196 list are present. Output should be:

file 1 :

128,129
116,118
108,104

file2 :

154,135
151,147
236.244

Current code:

fn = open("/home/vidula/Desktop/project/ori_tri/inpt.data","r")
fnew = fn.read()
fs = fnew.split('\n')
for value in fs:
    f = [open("/home/vidula/Desktop/project/ori_tri/input_%i.data" %i,'w') for i in range(len(list_of_files))]
    f.write(value)
    f.close()

Error: list do not attribute write.

shreya
  • 297
  • 3
  • 7
  • 15
  • Since Stackoverflow won't write the code for you: what have you tried yourself? See also the [FAQ](http://stackoverflow.com/faq). –  Dec 10 '12 at 09:51
  • 1
    In addition to explaining what you want to achieve, also show the code you have, and explain the problem you have having. – Lennart Regebro Dec 10 '12 at 09:57
  • fn = open("/home/vidula/Desktop/project/ori_tri/inpt.data","r") fnew = fn.read() fs = fnew.split('\n') for value in fs: f = [open("/home/vidula/Desktop/project/ori_tri/input_%i.data" %i,'w')for i in range(len(list_of_files))] f.write(value) f.close() Error: list do not attribute write. – shreya Dec 10 '12 at 10:23
  • this list cannot be written in the output files. – shreya Dec 10 '12 at 10:26
  • I've added the code in your comment to your question; makes it easier to read, and people can now (easily) suggest fixes to your code. –  Dec 10 '12 at 10:32

5 Answers5

11

Your current code is loading everything into memory, which is quite unnecessary, then it is making a list in a place that is not appropriate, hence your error. Try this:

fn = open("/home/vidula/Desktop/project/ori_tri/inpt.data","r")
for i, line in enumerate(fn):
    f = open("/home/vidula/Desktop/project/ori_tri/input_%i.data" %i,'w')
    f.write(line)
    f.close()

This will just write each line as it was to each file. Look up the enumerate function I used to do the indexing.

Having done this, you will still need to write parsing logic to turn each line into a series of lines...I'm not going to do that for you here, since your original code didn't really have logic for it either.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

your f is a list of files, you have to loop through it:

for file in f:
   file.write(value)
Mel
  • 5,837
  • 10
  • 37
  • 42
Samuele Mattiuzzo
  • 10,760
  • 5
  • 39
  • 63
  • Tried:Traceback (most recent call last): File "/home/vidula/Desktop/project/try_brain-input.py", line 38, in f.write(value) AttributeError: 'list' object has no attribute 'write' – shreya Dec 10 '12 at 10:47
  • no you didn't try :) you still have "f.write", but you have to iterate throu the elements of that list (assuming f is still created using your initial generator expression) – Samuele Mattiuzzo Dec 10 '12 at 10:49
1

You can't make python look inside the list class for the write object as an iterable in the list comprehension. The list is not compatible with the write() method. In python lists are appended.

Assuming your data file has new lines already in the file, create a filter object to remove blank lines then iterate:

string1 = '128,129', '134, 167', '127,189'
string2 = '154, 134', '156, 124', '138, 196'
l1 = list(string1)
l2 = list(string2)
data = [l1, l2]
f = open("inpt.data", "w")
for val in data:
    f.write(str(val))
    f.write('\n')
f.close()

with open("inpt.data", "r", encoding='utf-8') as fs:
    reader = fs.read()
    file = reader.split('\n')
    file = filter(None, file)

The simplest way:

# create one file for each list of data (1) on every new line 
i = 0
for value in file:
    i += 1
    f = open("input_%s.data" % i, 'w')
    f.write(value)
fs.close()

The pythonic simple way:

for i, line in enumerate(file):
    fi = open("input_%s.data" % i, 'w')
    fi.write(line)
fs.close()
Rider
  • 39
  • 6
0

I am assuming that you want to read 196 files and then write the data (after some modification) to new 196 files. If you use maps and reduce (functional programming) it can do what you want. Though without much explanation in the question, I am unable to help much.

def modify(someString):
    pass # do processing

def newfiles(oldfilename): return '%s.new.txt'%(oldfilename) # or something 

filenames = ('a', 'b', 'c', 'd', ....) 
handles = [(open(x, 'r'), open(newfile(x), 'w')) for x in filenames] # not using generator
tmp = [y[1].write(modify(y[0].read())) for y in handles) 
pranshus
  • 187
  • 6
  • @ sam. I just saw the error (single n double quote(, before I could change it, you did it. Thanks – pranshus Dec 10 '12 at 10:40
  • 1
    "Lambda programming" is not the term you are looking for. I assume you meant to say "functional programming". – Alex V Dec 10 '12 at 10:40
  • @ max. Yes indeed I was trying to say functional programming. Corrected. Thanks – pranshus Dec 10 '12 at 10:42
  • I have to produce multiple files from single file, My input file is in read mode and I have to produce 196 files from the input file.My input file contains as shown above, 196 list and I want to create 196 output files each having one list from input file, in short, distribute th list in input file to many output files. – shreya Dec 10 '12 at 10:43
  • @vids. I think john's answer would help you in that case. If it does not, please be more specific in the question. – pranshus Dec 10 '12 at 11:06
0

I think this is what you are looking for:

with open("/home/vidula/Desktop/project/ori_tri/inpt.data","r") as fn:
    listLines = fn.readlines()

for fileNumber, line in enumerate(listLines):
    with open("/home/vidula/Desktop/project/ori_tri/input{0}.data".format(fileNumber), "w") as fileOutput:
        fileOutput.write(line)