Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python code in USS on z/OS Mainframe - Square brackets not recognized

Tags:

python

zos

I am trying to execute the below python code on z/OS Mainframe USS. The problem I'm facing is that when I run the code, I get the below error message. It appears the square brackets are not recognized in my code.

  File "/u/q123/python/pyfilr.py", line 11
    print(lineï..0:4ï..)                   
              ¬                            
SyntaxError: invalid syntax 

Below is my code:

#!/usr/local/bin/rocket/python/python27                                 
# -*- coding: utf-8 -*-                                                 
import os                                                               
import json                                                             
def main():                                                             
    curpath = os.path.abspath(os.curdir)                                                      
    inp_file_path = os.path.join(curpath, os.path.join("python","inp.txt")
    file1 = open(inp_file_path,"r") 
    line = file1.readline().strip() 
    while line!="":
        print(line[0:4])
        jsonstr = json.dumps(line)
        line = file1.readline().strip()
        print(jsonstr)
    file1.close()
if __name__ == "__main__":
    main()   

If my remove the 2nd line "# -- coding: utf-8 -- " in my code then it errors out for below error message.

SyntaxError: Non-ASCII character '\xdd' in file /u/q123/python/pyfilr.py on line 11, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

I am not sure how to fix the error. I am using python 2.7.

Can you suggest a solution to this problem so that I can use square brackets in my code?.

like image 951
sanddunes Avatar asked Jun 30 '26 03:06

sanddunes


2 Answers

Ascii is very deeply built into Python, which is an issue on z/OS, which natively supports EBCDIC. You can use the chtag command to tell z/OS to treat a file as a specific encoding. To see if the file is tagged already, you can issue the command

ls -T <filename>

To tag the file, you can issue the command

chtag -tvc UTF-8 <filename>

I would suggest deleting the file, touching an empty file, tagging it, and then putting content in it, instead of just tagging the file.

like image 127
Kevin McKenzie Avatar answered Jul 01 '26 15:07

Kevin McKenzie


Since you are using python 2.7 the python script should be an ASCII file and your session should enable auto conversion. Here is what will work:

export _BPXK_AUTOCVT=ON
export _CEE_RUNOPTS="FILETAG(AUTOCVT,AUTOTAG) POSIX(ON)"
iconv -f ibm-1047 -t utf-8 pyfilr.py >pyfilrA.py
chtag -t -c utf-8 pyfilrA.py
python pyfilrA.py

Also add a missing closing parenthesis on line 7 of your script.

like image 34
Milos Lalovic Avatar answered Jul 01 '26 17:07

Milos Lalovic