0

How to append and save a print function to excel or csv

code :


firstpts= ['20']
for pfts in firstpts:
    try:
          (Operation)
        print('test11 : PASSED')

    except:
        (Operation)
        print('test11 : FAILED')


secondpts= ['120']
for sfts in secondpts:
    try:
         (Operation)
        print('test22 : PASSED')

    except:
        (Operation)
        print('test22 : FAILED')

If i run this code i"ll get this in output

test11 : PASSED
test22 : FAILED

How to redirect that outputs of all try-except cases to a csv

  • Possible duplicate of [How to redirect 'print' output to a file using python?](https://stackoverflow.com/questions/7152762/how-to-redirect-print-output-to-a-file-using-python) – wwii Aug 24 '19 at 14:42

2 Answers2

0

Create a file csv an write informations inside.

firstpts= ['20']
for pfts in firstpts:
    if int(pfts) < 100:
        print('test11 : PASSED')
        result_test11 = 'test11 : PASSED'
    else:
        print('test11 : FAILED')
        result_test11 = 'test11 : FAILED'

secondpts= ['120']
for sfts in secondpts:
    if int(sfts) < 100:
        print('test22 : PASSED')
        result_test22 = 'test22 : PASSED'
    else:
        print('test22 : FAILED')
        result_test22 = 'test22 : FAILED'

f = open("file.csv","w+")
f.write("{}\n{}".format(result_test11, result_test22))
f.close()
Kevno
  • 53
  • 5
0

First things first, your fundamental use of try-catch is wrong as is, for if-elsing.

Anyways, that aside, you would need to collect all of your logged statements in a string and then write that string to a '.csv' file.

Like this:-

# @author Vivek
# @version 1.0
# @since 24-08-2019

data = ""
firstpts = [20]
for pfts in firstpts:
    try:
        if pfts < 100:
            print('test11 : PASSED')
            data = 'test11 : PASSED\n'

    except:
        if pfts > 100:
            print('test11 : FAILED')
            data += 'test11 : PASSED\n'

secondpts = [120]
for sfts in secondpts:
    try:
        if sfts < 100:
            print('test22 : PASSED')
            data += 'test11 : PASSED\n'

    except:

        if sfts > 100:
            print('test22 : FAILED')
            data += 'test22 : FAILED'

file = open('test.csv', 'w')
file.write(data)
file.close()
v2k
  • 41
  • 2