0

I have a txt file with two values in a single column, one below the other:

0008

001U

I need to use each line in the file to be pasted into a four character field so I'm doing a for loop:

inputFile = open("C:/Users/UserName/Desktop/LCDIVextract.txt", 'r')
lines = inputFile.readlines()
inputFile.close()

for line in lines:
    em.fill_field(6, 15, f'{line}', 4)

However I'm getting this error:

py3270.FieldTruncateError: length limit 4, but got "['0008']". 

I have tried to replace the undesired characters for blank spaces, I also tried to trim it, but nothing worked. The only way I was able to get my code do what I wanted it's if I explictly wrote:

lines = ["0008","001U"]
for line in lines:
        em.fill_field(6, 15, f'{line}', 4)

I'm using py3270 and nothing else. I'm sure there's an error, but I'm not sure where. Any assistance is much appreciated. Thanks!

  • Those brackets mean you have a list, which is a pretty important thing to understand. Check that out first, and then look at the documentation for readlines() which explicitly states it will return a list. From there you should be closer to a solution. – Chris Jun 17 '20 at 23:37
  • Either use your debugger or print out the value of `lines` before the loop. With the .txt file as you say (and removing the 3270 code as I don't have a mainframe handy), `lines` contains `['0008\n', '001U']` before the loop. Try `em.fill_field(6, 15, f'{line.strip()}', 4)` – David Buck Jun 17 '20 at 23:50
  • @DavidBuck that worked just fine! Thanks a lot! Question has been answered now. – BadProgrammer Jun 18 '20 at 00:05
  • 1
    @DavidBuck your answer did, but that post you suggested gave me more insight, so thanks again! – BadProgrammer Jun 18 '20 at 13:26

0 Answers0