I'm parsing a text file into few dictionaries so that I can write them to a CSV file. But now I have comments in the text file. How do I ignore the comment lines and work with rest of the content?
I have checked few posts which recommend Pandas read_csv
but it will work after I have a dataframe.
I need to ignore the comments and read the rest content before parsing.
EDIT: I'm concerned with sql comments: -- and /* .... */
Part of my code: (form is a grammar defined by me)
with open("xyz.txt", 'r') as file:
if re.search(r'select|SELECT', file.read()):
print("hello select")
a = form.parseString(open('xyz.txt').read());
z=a.asDict()
Text file:
/*this is a multi line comment which
needs to be ignored */
select book from tab where b=100 --single line comment which should be ignored
select sal from emp where job_id=101
I tried using startswith(#)
for single line comment but the code kept on running and no result..and I have no idea for multi line comments.
with open("xyz.txt", 'r') as file:
for line in file:
li=line.strip()
if not li.startswith("#"):
new=line.rstrip()
while new:
if re.search(r'select|SELECT', file.read()):
print("hello select")
a = form.parseString(open('xyz.txt').read());
z=a.asDict()