Hi there. For some reason I am struggling with this problem. The purpose of this
problem is to take an existing .py file and change it so instead of writing to
a screen, it has to be written to an output file. The output file will be a csv
file.
I know how write to an output file, it creates the csv file but doesn't print
anything there yet. The problem is that says ,"outfile is not defined"
I cannot figure out why! Is there something else I need to define? or something is wrong?
In line 13 is the outfile where it says it's not defined. I have outfile on the top.
Any help would be appreciated...

def makeMagnitudeList():
        quakefile = open("earthquakes.txt","r")
        outfile = open("extractMag.csv","w")
        headers = quakefile.readline()
        
        maglist = [ ]
        for aline in quakefile:
            vlist = aline.split()
            maglist.append(float(vlist[1]))
        return maglist

magList = makeMagnitudeList()
outfile.write(makeMagnitudeList())
print(magList)

Edited 5 Years Ago by Sprewell184: n/a

All makeMagnitudeList() function do is to return maglist.
outfile will be local to function,to make it work we have to move it out.
Remember to close() file or use with open() .
you can not write a list to file,it has to string.
If you give a sample of earthquakes.txt file it is easier to test it out,here just a file a found.
Python has a CVS module who is made for work with cvs.

'''#in.txt
header something
1900	13
1901	14
1902	 8
1903	10
1904	16
'''

def makeMagnitudeList():
        quakefile = open("in.txt","r")
        headers = quakefile.readline()
        maglist = []
        for aline in quakefile:
            vlist = aline.split()
            maglist.append(float(vlist[1]))
        quakefile.close()
        return maglist

magList = makeMagnitudeList()
print(magList)

outfile = open("out.csv", "w")
outfile.write(', '.join(str(i) for i in magList))
outfile.close()

'''#out.csv
13.0, 14.0, 8.0, 10.0, 16.0
'''

Edited 5 Years Ago by snippsat: n/a

Thanks for the help. I was thinking that the outfile might not work unless I put outside. The .join function, is there a different way to show that part?
I have not covered the join function yet. And Here is the sample of the text file. It's alot more than what I will show
but I'm only showing a few as for a test:

MAG UTC DATE-TIME LAT LON Region
MAP 4.3 2011/03/12 20:16:59 25.423 -109.730 10.0 GULF OF CALIFORNIA
MAP 5.2 2011/03/12 20:09:55 36.747 144.050 24.2 OFF THE EAST COAST
MAP 5.0 2011/03/12 20:08:25 35.775 141.893 24.5 NEAR THE EAST COAST OF HONSHU, JAPAN
MAP 4.8 2011/03/12 19:59:01 38.101 142.840 17.6 NEAR THE EAST COAST OF HONSHU, JAPAN
MAP 4.6 2011/03/12 19:55:28 37.400 142.384 24.7 OFF THE EAST COAST

Edited 5 Years Ago by Sprewell184: accidentally posted b4 finishing

If i run your sample file in my code output will be.
4.3, 5.2, 5.0, 4.8, 4.6
Is that what you want?

The .join function, is there a different way to show that part?

join is important to learn about,here is an example.

>>> l = [4.3, 5.2, 5.0, 4.8, 4.6]
>>> l
[4.3, 5.2, 5.0, 4.8, 4.6]
>>> #So a list with floats,if write to a file it has to be string
>>> s = [str(i) for i in l]
>>> s
['4.3', '5.2', '5.0', '4.8', '4.6']
>>> #Now it`s a list with strings,so to make it a string we use join
>>> ''.join(s)
'4.35.25.04.84.6'
>>> #Have to but in a ,
>>> ', '.join(s)
'4.3, 5.2, 5.0, 4.8, 4.6'
>>> #Now is ready to be written to a file.

Edited 5 Years Ago by snippsat: n/a

So everytime if we have a function to define, and If i wanna write
to an output file, I always have to put it outside the function.
And yes thank you

So everytime if we have a function to define, and If i wanna write
to an output file, I always have to put it outside the function.

No of course not,we could have placed all in one function.
For better design it can be smarter to make a new function like code under.
A function should not do to many task,if a function do to many task code get harder to read.

def makeMagnitudeList():
        quakefile = open("in.txt","r")
        headers = quakefile.readline()
        maglist = []
        for aline in quakefile:
            vlist = aline.split()
            maglist.append(float(vlist[1]))
        quakefile.close()
        return maglist

def write_to_file(arg):
    outfile = open("out.csv", "w")
    outfile.write(', '.join(str(i) for i in arg))
    outfile.close()

magList = makeMagnitudeList()
write_to_file(magList)

Edited 5 Years Ago by snippsat: n/a

Comments
wise advice
This article has been dead for over six months. Start a new discussion instead.