Read a number a words from file

This is the place for queries that don't fit in any of the other categories.

Read a number a words from file

Postby Jesstess » Fri Apr 05, 2013 11:29 pm

Lets say i have a file with approx 500 words.
I only want to read 40 at a time
How do i do?
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Read a number a words from file

Postby micseydel » Fri Apr 05, 2013 11:43 pm

What exactly does your file look like? Python files are read line by line. You could write a generator which encapsulates the behavior you're talking about regardless of the format, but we could give you more detail if we have more detail. Further, we'd like to see the effort you've made.

If you can read all 500 words into memory, and you just want to iterate over sets of 40 of them, you can use any recipe that looks like this
Code: Select all
def take_by_parts(words, num):
    words_iter = iter(words)
    for words_group in zip(*[words_iter]*num):
       yield words_group
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1271
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Read a number a words from file

Postby Jesstess » Fri Apr 05, 2013 11:49 pm

My assignment is to read approximately 40 words at a time from a text file and check for uneccessary repetition of words.
If the word has already been used in these 40 words, its supposed to be marked with a surrounding asterix (*word*) that marks it has allready been used.
This is supposed to go on until the WHOLE textfile has been gone through.
This is how far ive got.
Code: Select all
class repeatcheck(object):
    def __init__(self):
        self.ord_i_fil = []
        self.kontroll_lista = []
        self.rattad_strang = ""

    def __str__(self):
        print (self.rattad_strang)

    def readfile(self):
        fil = input("Vilken fil vill du kontrollera?")
        f= open(fil,"r")
        f1 = f.read().split()
        limit = 40
        while len(self.ord_i_fil) <= limit :
            for i in f1:
               self.ord_i_fil.append(i)
               
        print(self.ord_i_fil)
    def upprepning(self):
        for i in self.ord_i_fil:
            if i not in self.kontroll_lista:
                self.kontroll_lista.append(i)
                self.rattad_strang += i
                self.rattad_strang += " "
            else:
                self.rattad_strang += "*"
                self.rattad_strang += i
                self.rattad_strang += "*"
                self.rattad_strang += " "
               
           
       
       

   
def main():
    q = repeatcheck()
    q.readfile()
    q.upprepning()
    q.__str__()

main()

Putting limit to 40 creates a problem when the textfile to be checked contain less than 40 words.
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm

Re: Read a number a words from file

Postby micseydel » Sat Apr 06, 2013 12:16 am

Here are some problems:
  • This is an English forum. You are more likely to get help if you write English code. I can understand/guess most of your variable names, but some of them might as well be names of your pets as far as my understanding of them. You're welcome to do as you wish, but it would help us to help you.
  • I'm a big proponent of OOP that makes sense. Unfortunately, Java programmers (and people taught Python by Java programmers) learn horrible design habits, in particular making classes out of things that aren't objects. A "repeatcheck" object doesn't make as much sense to me as, say, a Fraction, a Queue, a Stack or list or whatever else.
  • It's fine to define __magic_methods__ (methods that begin and end with double underscores) but we don't usually call them directly. Instead of object.__str__(), call str(object).
  • You asked about a file in your original question, when that wasn't relevant to what you need help with. All that was relevant to us for helping you was working with a list that had words in it. When you ask a question like this, you should make it as direct to desired input and output as possible. For example,
    My input is
    Code: Select all
    words = ('one', 'two', 'three', 'four', 'five', 'six', 'seven')
    and my desired result from this input is
    Code: Select all
    [('one', 'two'), ('three', 'four'), ('five', 'six')]
    (or whatever)

    We don't need the extra details of the class, and not just because I think using a class is overkill for this program.
  • I'm relatively sure I gave you a solution to your problem (or close to it); you made no mention of it in your response. This can be frustrating for us, if we try to help, and we don't get feedback, we can become discouraged. You could have said, "your solution does work, but I want help modifying what I have here" or "your solution doesn't do [x]" or whatever else, but you should respond to our attempts to help you. (As for this long message with excessive advice; you don't need to go over every point, or explain that your professor is forcing you to use classes, feel free to only talk about solving the problem. Or say whatever you'd like.)

Also, I believe the set datatype is excellent for a problem like this.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1271
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Read a number a words from file

Postby Jesstess » Sat Apr 06, 2013 1:10 am

micseydel wrote:Here are some problems:
  • This is an English forum. You are more likely to get help if you write English code. I can understand/guess most of your variable names, but some of them might as well be names of your pets as far as my understanding of them. You're welcome to do as you wish, but it would help us to help you.
  • I'm a big proponent of OOP that makes sense. Unfortunately, Java programmers (and people taught Python by Java programmers) learn horrible design habits, in particular making classes out of things that aren't objects. A "repeatcheck" object doesn't make as much sense to me as, say, a Fraction, a Queue, a Stack or list or whatever else.
  • It's fine to define __magic_methods__ (methods that begin and end with double underscores) but we don't usually call them directly. Instead of object.__str__(), call str(object).
  • You asked about a file in your original question, when that wasn't relevant to what you need help with. All that was relevant to us for helping you was working with a list that had words in it. When you ask a question like this, you should make it as direct to desired input and output as possible. For example,
    My input is
    Code: Select all
    words = ('one', 'two', 'three', 'four', 'five', 'six', 'seven')
    and my desired result from this input is
    Code: Select all
    [('one', 'two'), ('three', 'four'), ('five', 'six')]
    (or whatever)

    We don't need the extra details of the class, and not just because I think using a class is overkill for this program.
  • I'm relatively sure I gave you a solution to your problem (or close to it); you made no mention of it in your response. This can be frustrating for us, if we try to help, and we don't get feedback, we can become discouraged. You could have said, "your solution does work, but I want help modifying what I have here" or "your solution doesn't do [x]" or whatever else, but you should respond to our attempts to help you. (As for this long message with excessive advice; you don't need to go over every point, or explain that your professor is forcing you to use classes, feel free to only talk about solving the problem. Or say whatever you'd like.)

Also, I believe the set datatype is excellent for a problem like this.


Thanks for your response!
I know its a hardship to understand my Swedish variable names. I have to do so because those who are going to "grade" it are Swedish.
I also believe that a object-oriented code is not necessary here, but the "project" i was given requires that it must contain 1 class.
The reason i didn't respond to your solution was simply because, i did not understand it.
I've never come across either yield, iter or zip, and i dont want to put things in my code that i dont quite frankly understand, since later i have to present to the examinator.
And if im not totally off iter is only used in python 2.x?

Ok, back to topic, i will explain a bit more exclusively.

Someone, I, Stalin, Bush, whomever have written a text file in wordpad.
This text file contains of i.e 500 words, written horizontally, not vertically.
I only want to check 40 of those words at a time, if some words are repeated or not.
I also want the program to be able to read text files with less than 40 words aswell

Edit: This is not actually my assignment, I'm doing it for a classmate who is currently in hospital recovering from a stroke, and he asked me to because he didnt want to get too backed up when he got back. If anybody cares, that is.
Edit nr 2: Im sorry if im not making a lot of sense, its 3:30 AM here in CET.

Edit nr 3: This is the initial problem im having.
Code: Select all
def readfile(self):
        words_in_file = []
        file= input("Which file do you wish to check?")
        f= open(file,"r")
       
        limit = 40
        number_words = 0
        while number_words <= limit :                           # Here is where i wanna make sure only 40 words is appended to my list words_in_file
            number_words += 1
            f1 = f.readline().split()
            for i in f1:
                words_in_file.append(i)


But this fails, all the words are indeed put in my list. I know there is something wrong-o with the loops, but i can't figure what!
P.S Just ignore the misplaced words_in_file, I have no clue how it got there.

Edit 4: I only want to read 40 words at a time, check for repeated words, then go on with the next 40 words, all until the textfile is completely checked.
Jesstess
 
Posts: 13
Joined: Fri Apr 05, 2013 3:47 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 3 guests

cron