How to display data in tabular format on a txt file
<p>I have been stuck on this python problem for hours. I'm trying to figure out how to write the data that can be manually entered above into a txt file in a way that it shows up in a two row eight column table. The contents in name_array are supposed to be headers and the  contents in data_array are the actual data pieces.</p>

<pre><code>name = str(raw_input( "Enter the student's name: "))
medianScore = float(raw_input("Enter the median group score for quizzes:"))
indScore = float(raw_input("Enter the score of the individual quiz:  "))
assignmentScore = float(raw_input("Enter the score of the assignment: "))
test1Score = float(raw_input("Enter the score of exam one: "))
test2Score = float(raw_input("Enter the score of exam two: "))
test3Score = float(raw_input("Enter the score of the final exam: "))
fileName = str(raw_input("Enter the name of the file you would like to create: "))
f = file(fileName + ".txt" , a)

finalScore = ((medianScore * .14) + (indScore * .14) + (assignmentScore * .12) + (test1Score * .15) +(test2Score * .20) + (test3Score * .25))
data_array = [name, finalScore, test3Score, test1Score, test2Score, assignmentScore,  indScore, medianScore]
name_array = [ "Student", "Final Grade", "Final Exam", "Exam 1", "Exam 2", "Assignments", "Solo Quizzes", "Group Quizzes"]
</code></pre>

<p>If you want to simply output a csv-like file you can use the <code>csv</code> package:</p>

<pre><code>import csv

writer = csv.writer(f, delimiter='\t')
writer.writerow(name_array)
writer.writerow(data_array)
</code></pre>

<p>It will output:</p>

<pre><code>Student Final Grade Final Exam  Exam 1  Exam 2  Assignments Solo Quizzes    Group Quizzes
asd 3.88    6   4   5   3   2   1
</code></pre>

<p>In this example use <code>tab</code>s as separator, but you can cange it with any char you want. See <a href="https://docs.python.org/2/library/csv.html#csv.writer" rel="nofollow">this documentation</a> for more options.</p>

<hr>

<p>Instead if you want something more human-readable you can use intead the <code>tabulate</code> package:</p>

<pre><code>from tabulate import tabulate

f.write(tabulate([data_array], headers=name_array))
</code></pre>

<p>It will produce:</p>

<pre><code>Student      Final Grade    Final Exam    Exam 1    Exam 2    Assignments    Solo Quizzes    Group Quizzes
---------  -------------  ------------  --------  --------  -------------  --------------  ---------------
asd                 3.88             6         4         5              3               2                1
</code></pre>

<p>See <a href="https://pypi.python.org/pypi/tabulate" rel="nofollow">this documentation</a> for more options to format your table.</p>

python file tabular