Author Topic: Python problems  (Read 14789 times)

sand_man

  • Super Jackass
  • Jackass II
  • Posts: 54
  • Karma: +10/-0
Python problems
« on: August 24, 2005, 08:19:18 AM »
I've been playing around with python today and I'm having problems with file IO.
Since my C file parser in C was a slight failure, I decided to try and use Python to do the same thing (because I heard that Python, Perl and other scripting languages are good for string parsing etc).
The problem is I don't know how to test a file object for EOF.

Code: [Select]

in_file = open("main.c","r")
out_file = open("out.txt","w")
out_file.write("")

#while ??:  <---WHAT DO I TEST FOR???
c = 0
is_comment = 0
line = in_file.readline()

while c < len(line):
    if line[c] == "/" and line[c+1] == "/" and is_comment == 0:
        out_file.write("")
        out_file.write(line[c])
        is_comment = 1
    else:
        out_file.write(line[c])
    c = c + 1
   
if is_comment:
    out_file.write("")


out_file.write("\n")
out_file.close()
in_file.close()


I've checked the docs and several tutorials and it just makes no sense to me.

(the color labels and code labels have been edited because it makes the post look funny)

sand_man

  • Super Jackass
  • Jackass II
  • Posts: 54
  • Karma: +10/-0
Python problems
« Reply #1 on: August 24, 2005, 09:55:06 AM »
It seems I may have found a solution before you all could even reply :)

does this seem right?
Code: [Select]

in_file = open("main.c","r")
out_file = open("out.txt","w")
out_file.write("\n")

line = in_file.readline()
while (line):
    c = 0
    is_comment = 0


    while c < len(line):
        if line[c] == "/" and line[c+1] == "/" and is_comment == 0:
            out_file.write("")
            out_file.write(line[c])
            is_comment = 1
        else:
            out_file.write(line[c])
        c = c + 1
   
    if is_comment:
        out_file.write("")

    line = in_file.readline()


out_file.write("\n")
out_file.close()
in_file.close()

ygfperson

  • Founders
  • Posts: 601
  • Karma: +10/-1
    • Last.fm
Python problems
« Reply #2 on: August 24, 2005, 04:59:02 PM »
I haven't used python in a while but wouldn't it make more sense to test in_file instead?

sand_man

  • Super Jackass
  • Jackass II
  • Posts: 54
  • Karma: +10/-0
Python problems
« Reply #3 on: August 24, 2005, 07:51:37 PM »
That is what I thought but it just kept looping. I probably wasn't doing it properly.
Anyway, if eof is reached then loop will be 0 so this works.

sand_man

  • Super Jackass
  • Jackass II
  • Posts: 54
  • Karma: +10/-0
Python problems
« Reply #4 on: August 25, 2005, 04:40:04 AM »
This is my work so far. As messy as it looks, it works nicely. But I can't work out how to identify the keywords. I assume I should use regex? I need some help with this.
Code: [Select]


in_file = open("capp.cpp","r")
out_file = open("out.txt","w")
out_file.write("\n")

keywords = ["auto","double","int","long",\
            "break","else","switch","char"\
            "case","enum","register","typedef",\
            "extern","return","union","const",\
            "float","short","unsigned","continue",\
            "for","signed","void","default","goto",\
            "sizeof","volatile","do","if","static","while"]

alpha = ['a','b','c','d','e','f','g','h',\
         'i','j','k','l','m','n','o','p',\
         'q','r','s','t','u','v','w','x','y','z']


line = in_file.readline()
is_mcomment = 0
is_string  = 0
is_comment = 0
while (line):
    c = 0
   
    is_scomment = 0
    is_prepro  = 0
    is_number = 0

   
    while c < len(line):
       

        ### MULTI LINE COMMENTS ###
        if line[c] == "/" and line[c+1] == "*":
            if is_prepro:
                out_file.write("")
                is_prepro = 0
            out_file.write("")
            is_mcomment = 1
            is_comment = 1
        elif line[c] == "/" and line[c+1] == "/":
            if is_prepro:
                out_file.write("")
                is_prepro = 0
            out_file.write("")
            is_scomment = 1
            is_comment = 1

        if line[c] == "#" and not is_comment:
            out_file.write("")
            is_prepro = 1

       
        if line[c] == "\"" and not is_comment:
            if not is_string:
                out_file.write("")
                is_string = 1
            else:
                is_string = 2
                   
        if line[c].isdigit() and not is_prepro and not is_string and not is_comment:
            if not line[c-1] in alpha and not line[c+1] in alpha:
                out_file.write("")
                is_number = 1
       
       
           
        out_file.write(line[c])

        ### ENDING STRINGS, DIGITS AND MULTI LINE COMMENTS ###
        if line[c] == "*" and line[c+1] == "/":
            c = c + 1 # No idea why this needs to be done
            out_file.write("")
            is_mcomment = 0
            if is_mcomment == 0 and is_scomment == 0:
                is_comment == 0
        if is_string == 2:
            out_file.write("")
            is_string = 0
        if is_number == 1:
            out_file.write("")
            is_number = 0

       
       
        c = c + 1

       
    ### ENDING SINGLE LINE COMMENTS AND PREPROCESSOR ###
    if is_scomment:
        out_file.write("")
        is_scomment = 0
        if is_mcomment == 0 and is_scomment == 0:
                is_comment == 0
    if is_prepro:
        out_file.write("")
        is_prepro = 0
   
    line = in_file.readline()


out_file.write("\n")
out_file.close()
in_file.close()
« Last Edit: August 25, 2005, 05:25:49 AM by sand_man »

webwhy

  • Jackass IV
  • Posts: 608
  • Karma: +15/-10
Python problems
« Reply #5 on: August 29, 2005, 10:45:45 PM »
you can test for membership in a python sequence object such as your "keywords" list with the in operator
Code: [Select]

keywords = ['int', 'double', 'long', 'static']
if 'int' in keywords:
    print 'found'


Also you can you the read the contents of the file into a sequence object with the readlines or xreadlines method on the file object.
Code: [Select]

f = open('main.cpp', 'r')
for line in f.xreadlines():
    print line
f.close()