getting next line while looping f.readlines()

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

getting next line while looping f.readlines()

Postby metulburr » Thu Jul 18, 2013 11:09 am

I am trying to figure out how to get the next x number of lines while already looping readlines(). My only solution was to create 2 readlines() and loop the second one when i needed to get the lines. I might not be figuring this out because of being tired, so it could be a stupid simple fix.

overall, i am trying to convert this:
Code: Select all
import iostream
import test_header.h

namespace std

def main():
   print('test')


to this dynamically:
Code: Select all
#include <iostream>
#include "test_header.h"

using namespace std;

int main(int argc, char **argv ){
    cout << "test" << endl;
}


full code:
Code: Select all
class CConvert:
    def __init__(self, filename):
        self.filename = filename
        self.lines = None
        self.temp = []
        self.replacer = []
        self.keywords = [
            'import',
            'namespace',
            'def'
        ]
       
    def open(self):
        f = open(self.filename)
        self.lines = f.readlines()
        self.lines2 = f.readlines()
        f.close()
       
    def convert(self):
        for line in self.lines:
            #if len(line.split()) == 2 and line.split()[0] == 'import':
            if line.startswith('import'):
                l = line.split()
                l[0] = '#include'
                if l[1].endswith('.h'):
                    l[1] = '\"{}\"'.format(l[1])
                    print(l[1])
                else:
                    l[1] = '<{}>'.format(l[1])
                    print(l[1])
                self.replacer.append(' '.join(l))
               
            elif line.startswith('def'):
                if line.split()[1].startswith('main'):
                   
                    for line2 in self.lines2:
                        if line.spartswith('def'):
                            if line.split()[1].startswith('main'):
                                print(line)
                    #statement = f.next()
                    statement = 'cout << "test" << endl;'
                    self.replacer.append('int main(int argc, char **argv ){{{STATE}}}'.format(STATE=statement))
                else:
                    return_type = 'int'
                    name = 'func'
                    args = '()'
                    self.replacer.append('{} {}{}'.format(return_type, name, args))
           
            elif line.startswith('namespace'):
                self.replacer.append('using namespace {};'.format(line.split()[-1]))



app = CConvert('convert_test.test')
app.open()
app.convert()
print(app.replacer)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: getting next line while looping f.readlines()

Postby Yoriz » Thu Jul 18, 2013 11:51 am

New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 871
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: getting next line while looping f.readlines()

Postby ochichinyezaboombwa » Thu Jul 18, 2013 7:03 pm

I have no idea about what your code is trying to do but clearly to get a copy of a list you don't need to read a file twice. Just
Code: Select all
lines2 = list(lines1)
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests

cron