0

I am trying to take a file with multiple entries and write it to a new file. The new file should contain stripped and split by comma data in separate list. Once I have done that I want to calculate the z score using row 3(observed) and row 4(expected). The z score formula I am using is Zi = (Observed i - Expected i )/sqrt(Expected i ).Then I want to add the Zscores to the list of data in the input file, which is where I am having trouble. I am using output_file = open(outpath,"w") but nothing is being written to the output file.

example input_file data: Ashe,1853282.679,1673876.66,1,2 Alleghany,1963178.059,1695301.229,0 ,1 Surry,2092564.258,1666785.835,5 ,6 Currituck,3464227.016,1699924.786,1 ,1 Northampton,3056933.525,1688585.272,9 ,3 Hertford,3180151.244,1670897.027,7 ,3 Camden,3403469.566,1694894.58,0 ,1 Gates,3264377.534,1704496.938,0 ,1 Warren,2851154.003,1672865.891,4 ,2

my code:

   import os
from math import sqrt
def calculateZscore(inpath,outpath):
    "Z score calc"
    input_file = open(inpath,"r")
    lines = input_file.readlines()
    output_file = open(outpath,"w")


    county = []
    x_coor = []
    y_coor = []
    observed = []
    expected = []
    score = 0
    result = 0



    for line in lines:
        row = line.split(',')
        county.append(row[0].strip())
        x_coor.append(row[1].strip())
        y_coor.append(row[2].strip())
        observed.append(int(row[3].strip()))
        expected.append(int (row[4].strip()))

    o = observed
    e = expected
    length_o = len(o)
    length_e = len(e)
    score = 0
    for i in range(length_o):
        score += (o[i] - e[i])
        result += (score/(sqrt(e[i])))







def main():
    "collects data for code "
workingDirec = raw_input("What is the working directory?")
original_file = raw_input("The input filename is?")
full_original = os.path.join(workingDirec,original_file)
chi_square = raw_input("The name of the chi-squared stats table file is?")
full_chi_square = os.path.join(workingDirec,chi_square)
output_file = raw_input ("What is the output filename?")
full_output = os.path.join(workingDirec,output_file)

calculateZscore(full_original,full_output)

Any guidance would be appreciated

3
  • 1
    Unless my eyes aren't what they used to be, I don't see you doing output_file.write() anywhere. Commented Jun 24, 2015 at 0:44
  • sorry, it wasn't formatted correctly Commented Jun 24, 2015 at 0:54
  • I still do not see you using write() anywhere. You will need to use it if you want to write to a file. Commented Jun 24, 2015 at 1:01

2 Answers 2

1

In order to write to a file object, you need to call fileobj.write() for a file object opened in write ("w"), append ("a"), read/write ("r+"), or append/write modes ("a+").

You need to call at some point at the end of your function:

output_file.write(str(result))  # want character representation

For example:

for i in range(length_o):
    score += (o[i] - e[i])
    result += (score/(sqrt(e[i])))
output_file.write(str(result))

Then, at the end, you should probably close the file output_file.close()

(This is just in case you are calling a specific module many times, it's good practice to close all the file objects you open. Typically this is done by "with open(filename, mode) as e".

Sign up to request clarification or add additional context in comments.

Comments

1

The Problem is access mode in your code input_file = open(inpath,"r") you are opening file in read only mode you need to give write permission too.

r Opens a file for reading only

Here are some of the permission mode, regarding writing into file. You can give whichever suits you

file = open('myfile.txt', 'r+')

r+ Opens a file for both reading and writing. The file pointer placed at the beginning of the file.

file = open('myfile.txt', 'w+')

w+ Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

file = open('myfile.txt', 'a+')

a+ Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

Then You need to write in that file too.. One simple workaround you can implement,

file = open('myfile.txt', 'r+')
file.write( "Python \n");
file.close()

1 Comment

There's no a single example of fileobj.write() here, and they have an output file. So this isn't the proper answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.