How to specify newline argument in open command in IronPython 2.7?

Landon Mitchell Kanner
Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 331
50 Answers 100 Comments Second Anniversary 25 Likes
✭✭✭✭

I am trying to append some data to a CSV file in SpaceClaim scripting environment using the commands shown below, but it is NOT allowing me to give newline = ' ' argument for the open function. This syntax is running perfectly alright when used in Standalone Python environment (via Spyder).

If I remove newline = ' ' argument, SpaceClaim did not complain, but the output CSV file has been generated with an Empty Row between each data line, which I want to avoid. Any tips/tricks to resolve this would be so helpful. Thank you.

 # Appending Data to an Existing CSV file
file = open(csv_robustness_input_path, 'a', newline = '')
writer = csv.writer(file)

data_to_append = []

for i in range(len(Opt_Param_Name)):
    data = [Opt_Param_Name[i],"Optimization",Opt_Param_Ref_Value[i],0,0, "",1,"", "REAL", "Continuous", str(Opt_Param_Min[i])+":"+str(Opt_Param_Max[i]), "","", "", "",""]
      data_to_append.append(data)

writer.writerows(data_to_append)    
file.close()
# End

Best Answer

  • Landon Mitchell Kanner
    Landon Mitchell Kanner Member, Employee, GitHub-issue-creator Posts: 331
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    Answer ✓

    Use the open command from the io module:

    import csv
    import io
    data = 'hello world'
    
    with io.open(r'D:\test.txt','a',newline='') as file:
        writer = csv.writer(file)
        writer.writerows(data)