I'm still learning python and was wondering what a pythonic way (aka less is more) of coding the following problem.
Due to the wonders of 1990s technology, a text file is dumped to the server everyday with csv tables in it. That's right - one text file with two csv tables.
Goal: To parse out the csv tables and make them into two different csv files.
File looks like this:
start of file
blah blah
blah blah
blah blah
+--------
,tbl1, tbl1,
+--------
,data, data,
,data, data,
.....
,data,data
+--------
blah blah
blah blah
blah blah
+--------
,tbl2, tb2,
+--------
,data, data,
,data, data,
....
,data, data,
blah blah
blah blah
Issue: Table 1 is varying in length.
I need to be able to extract table 1 no matter what the length of the table is and make it a CSV file.
I have
def lp_to_csv(in_file_name, out_filename):
#open the input & output files.
inputfile = open(in_file_name, 'rb')
csv_file = out_filename
out_csvfile = open(csv_file, 'wb')
#read in the correct lines
my_text = inputfile.readlines()[117:-8]
del my_text[1]
for row in my_text:
#cycle through to find the end
if row[0] != ",":
print "excludeded: " + row
#convert to csv using "," as delimiter
in_txt = csv.reader(my_text, delimiter = ',')
#hook csv writer to output file
out_csv = csv.writer(out_csvfile)
#write the data
out_csv.writerows(in_txt)
#close up
inputfile.close()
out_csvfile.close()
but that code only makes 1 csv file and it includes the second block of 'blah blah'.
I think I know how to make it two csv files (create a subroutine that creates a csv file with the my_text object, but how do I cut out the blah blah and how do i trigger when to chop it into 2 tables?
Can anyone direct me for a nice pythonic way?
ANSWERED:
Very similar to below answer:
I created a subroutine that created a list of where the delimiter +-----
was. Since it's always in groups of three, First, Heading Row, End.
Then I chunk into groups of three the list.
Then activate the table making code on each group of three.
Pretty handy so far & stateless which is nice.