Doug Smythies
Doug Smythies

Reputation: 145

python write list of file prefixes and specific content to another file

I have a list of of file prefixes that was provided as input to a python script. It is in two forms, comma and blank separated. Example (although, they are not always numbered):

is0,is1,is2,is3,is4,is5,is6,is7,is8
is0 is1 is2 is3 is4 is5 is6 is7 is8

Related code fragment (I manually inserted the newline above):

if raw_input_file_list:
    input_file_list = raw_input_file_list.replace(",", " ")
    print(raw_input_file_list, input_file_list)

They represent a set of comma separated files, i.e. is0.csv, is1.csv, ... And the script uses gnuplot to plot the data to a .png file. Then the script creates an HTML file wrapper to display the graph. That part is working fine. Additionally, I am attempting to present a small portion of the data in a table in the HTML file. Example, without borders and such:

      number of work packets completed
name  1000  2000  3000  4000
is0   8369 16739 25108 33478
is1  47648 65186 77259 87103
is2  46417 63642 75575 85394
...

Where the "1000" column is the corresponding entry from the file. Example:

doug@s19:~/freq-scalers/step$ grep ^1000 is?.csv
is0.csv:1000, 8369
is1.csv:1000, 47648
is2.csv:1000, 46417
is3.csv:1000, 46101
is4.csv:1000, 47686
is5.csv:1000, 47018
is6.csv:1000, 38351
is7.csv:1000, 38107
is8.csv:1000, 17354

So far I have not been able to even get the file name column, always ending up with one letter per row, let alone extracting the data columns. Example:

<table summary="Some Selected Data..." class="dindex">
<TR>
<TD class="dlinks"><P>
i<BR>
s<BR>
0<BR>
,<BR>
i<BR>
s<BR>
1<BR>
,<BR>

Code fragments (I have tried a great many methods):

    f_handle.write('<table summary="Some Selected Data..." class="dindex">\n')
    f_handle.write('<TR>\n')
    title_list = input_file_list
    f_handle.write('<TD class="dlinks"><P>\n')
    for x in range(len(raw_input_file_list)):
       f_handle.write('{}'.format(raw_input_file_list[x]))
       f_handle.write('<BR>\n')
    f_handle.write('</P></TD>\n')
    f_handle.write('<TD class="dlinks"><P>\n')
    for f in input_file_list:
       f_handle.write('{}\n'.format(f))
    f_handle.write('</P></TD>\n')

Where f_handle is:

f_handle = open('index.html', 'w')

Obviously python is not may area of knowledge. Any help appreciated.

Upvotes: 1

Views: 79

Answers (1)

ttyip
ttyip

Reputation: 1261

If raw_input_file_list is a string, raw_input_file_list[x] is the xth character of that string.

What you might want to do is to use the split() method to split the string by commas into a list, and iterate through that list:

foo_list = raw_input_file_list.split(",")
for file_name in foo_list:
    # file name would be is0 then is1 etc.
    f_handle.write(file_name)
    f_handle.write('<BR>\n')

Let me know if you need any further clarification.

Upvotes: 1

Related Questions