pythonpython-3.xprintingoutputcode-separation

How to print two elements without being separated by space in output in python3?


I should write a code for calculating average of some grades in a csv file. My code is going to be check by another program(and not a human.) I already have the Help file about the checking program and I should design my code to be understandable for it.

This is my code:

import csv
from statistics import mean 

def calculate_averages(input_file_name, output_file_name):
    with open("C:\\Users\\Tandis\\Desktop\\ggg.CSV", 'r') as f:
        rows = csv.reader(f)
        for row in rows:
             name = row[0]
             name = name.strip() + ","
             scores = list()
             for grade in row[1:]:
                 scores.append(int(grade))
                 average = str(mean(scores)).strip()
             print(name,average)

  
print(calculate_averages("ggg.csv" , 'jj'))

And the output is:

mandana, 7.5
hamid, 6.066666666666666
sina, 11.285714285714286
sara, 9.75
soheila, 7.833333333333333
ali, 5
sarvin, 11.375
None
Press any key to continue . . .

But the output that checking program can understand is like:

mandana,7.5
hamid,6.066666666666666
sina,11.285714285714286
sara,9.75
soheila,7.833333333333333
ali,5.0
sarvin,11.375

I don't know how to remove the space between numbers and names in output. Any Ideas?


Solution

  • You can use end=‘’ or sep=‘’ which are params in the print function.

    The code will be like the following:

    print(name, average, sep=‘’)
    # or you can use
    print(name, average, end=‘’)
    

    But in this case the another space is exists either on name or average variables, you can delete all the snd or start spaces while processing the data if you are process them.