Python – Writing to a CSV with python without brackets


I am trying to write the below information to a csv file in this format:

Tmin, -40

However my output looks like the below with the brackets and quotes around the -40:

Tmin, ['-40']

My code is below. Basically I am searching through a csv file for a specific phrase, in this case "Tmin", and reading the entry 1 cell to the right. I then want to write both "Tmin" and the value read directly to a second csv file.

I have searched for a way to remove the quotes and brackets but have not been able to get it to work. I am pretty new to python so any help would be helpful. Thanks.

import csv
fileInput = "Input.csv"
fileOutput = "output.csv"
Name_Column = 0
Value_Column = 1

with open(fileInput, 'r') as file:
    reader = csv.reader(file)
    Tmin = [line[Value_Column] for line in reader if line[Name_Column] == 'Tmin']
    print "Tmin=", Tmin    #print to screen to check value read

with open (fileOutput,'w') as file:
   writer = csv.writer(file)
   print writer.writerow(['Tmin',Tmin])

Best Solution

python is just printing the Tmin as a list, you can either print the CSV row as a string, or just the first element in the row.


print "Tmin=", ", ".join(Tmin)


print "Tmin=", Tmin[0]

if you want to have csv output through the csv writer, you need a single list, not a list inside a list:


writer.writerow(['Tmin']+Tmin) #note this is already printing the result to a file so dont "print" the return value