0

Okay so I'm taking a introduction Python course and I have to take a text file with a few sentences and export it to another text file using the command prompt. I've got it basically done but I need help with it exporting to another text file. Any help?

import sys
import string


def main(argv):
    args=argv[1:]
    settings={}
    for entry in args:
        e=entry.split(":")
        settings[e[0]]=e[1]
    count={}
    inputfile=open(settings['in'],'r',)
    exclude=string.punctuation+"\n"
    for lin in inputfile:
        chunks=line.split(" ")
        for ch in exclude:
            chunk=chunk.replace(ch,' ')
        if chunk:
            if not(chunk in count):
                count [chunk]=1
            else:
                count[chunk]=count[chunk]+1
    inputfile.close()
    Keys=[key for key in count]
    keys=sorted(Keys)
    outpufile=open (settings['out'],"w")
    for key in Keys:
        outputfile.write(key+" "+"/n")
Jean Jung
  • 1,200
  • 12
  • 29
  • 6
    Welcome to SO :) Help with what specifically? I.e. what is the problem, how did you try to resolve it? What are you trying to achieve? Any errors that you get (post stack trace if so). – Aleksander Lidtke Sep 10 '15 at 19:15
  • I don't get any errors I just dont know how to make it export it to a new text file. for example the imported text should have a sentence in it then the exported file has how many times the word occurred in the sentence. ie what:2 where:1 this:1 – Chanston Cochran Sep 10 '15 at 19:19
  • First, you're not calling your main function. Remove the function definition and indent everything one layer less OR add `main(sys.argv)` at the end of your Python script. – jojonas Sep 10 '15 at 19:22
  • OK, this means you're interested in *writing to files*. Here's a post with an example. The idea the same as reading from files, just the other way around ;) http://stackoverflow.com/questions/6159900/correct-way-to-write-line-to-file-in-python – Aleksander Lidtke Sep 10 '15 at 19:22
  • Please be more specific about what you've tried and what isn't working. If you have a lot of problems, try to break them down and research them separately, then only ask questions when you can't find answers to specific problems. – zstewart Sep 10 '15 at 19:23
  • http://www.docdroid.net/CeXEiu8/assign2.pdf.html here is a link to the pdf for the assignment. What do I need to do to my code to make it print to the output text file? – Chanston Cochran Sep 10 '15 at 19:35
  • In the code above, you aren't closing the file. Does writing `outputfile.close()` at the end help? If it does (or even if it doesn't) you should look up the `with` keyword. – saulspatz Sep 10 '15 at 20:11

0 Answers0