0

please in need your help , i wanrt to compy some specific lines from a file to another two ones. the variables n and p are global . here is my code :

def files():
    i = 1
    X = 1
    f90 = open('C:\Users\Homura\Desktop\python\data90.txt' , 'w')
    f10 = open('C:\Users\Homura\Desktop\python\data10.txt' , 'w')
    f = open('C:\Users\Homura\Desktop\python\TData.txt' , 'r')
    while True:
        line = f.readline()
        if line.startswith('0'):
            while i <= n: # taking 90% of negative tweets and writing them in f90
                f90.write(line)
                i += 1
            while i != n: #putting the rest of the negative tweets ( 10%) in the f10 file 
                f10.write(line)
                i += 1
        elif line.startswith('1'):
            while ( x <= p): # taking 90% of positive tweets and writing them in f90 
                f90.write(line)
                x += 1 
            while (x != p): #putting the rest of the positive tweets ( 10%) in the f10 file
                f10.write(line)
                x += 1                  

    f.close()
    f10.close()
    f90.close()
    return f10 , f90
kad
  • 1
  • 2

1 Answers1

1

First, a small observation, you have an uppercase X and a lowercase x - are they supposed to be the same?

X = 1
#...
x += 1 

Next, the chances are the lines do not start with the characters you think they start with. Consider adding an else to catch neither a 0 or a 1 at the start.

Finally since you say while True it's not clear how to while loop will ever terminate, so perhaps it doesn't and the files never get closed. Try just doing for line in file... (see e.g. here)

Try this:

   for line in f:
        if line.startswith('0'):
            while i <= n: # taking 90% of negative tweets and writing them in f90
                f90.write(line)
                i += 1
            while i != n: #putting the rest of the negative tweets ( 10%) in the f10 file 
                f10.write(line)
                i += 1
        elif line.startswith('1'):
            while ( x <= p): # taking 90% of positive tweets and writing them in f90 
                f90.write(line)
                x += 1 
            while (x != p): #putting the rest of the positive tweets ( 10%) in the f10 file
                f10.write(line)
                x += 1         
        else:
            print "Unmatched line ", line

EDIT

All of the above assumes you have managed to open the input file ok.

Note

" The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character. String literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and use different rules for interpreting backslash escape sequences"

from the docs

You use input files with backslashes in: e.g.

f = open('C:\Users\Homura\Desktop\python\TData.txt' , 'r')

Try changing this to a raw string

f = open(r'C:\Users\Homura\Desktop\python\TData.txt' , 'r')
Community
  • 1
  • 1
doctorlove
  • 17,477
  • 2
  • 41
  • 57
  • Thank you so much for your help , but it not working File ".\first.py", line 64 if line.startswith('0'): ^ IndentationError: expected an indented block – kad Feb 10 '14 at 10:58
  • Indentation error often means you have a muddle of tabs and spaces. – doctorlove Feb 10 '14 at 11:00
  • i copy past what u gave me , i had this problem , i dont se that we need an indent , but python knows wayyy better :D – kad Feb 10 '14 at 11:01
  • please, what do you mean by that, i am using notepad +++ i m using tab for indents – kad Feb 10 '14 at 11:05
  • I used spaces for indents - you need to be consistent. – doctorlove Feb 10 '14 at 11:23
  • In notepad++ replace, select extended, do \t for tab and use 4 spaces - be consistent. Most people use spaces not tabs. – doctorlove Feb 10 '14 at 11:25
  • thank you so much , i did it , but the same problem, no Errors but nothing happens !!! no files have been filled up . – kad Feb 10 '14 at 19:10