@jagking I was looking into this code and was trying to refine it further,
1.) if the length of line is less than 50 , (suppose the length of line is 45) it should insert 5 blank spaces to streamline the data, i was trying it with all possible cases but it is not appending the blank spaces to the last line when only single line is there for ex:
abc…
abc…
abc…
2.) in case xyz occurs, it should skp the line.
I was expecting that it should append blank spaces to make each row of length 50:
for example:
abc 123 456 #if row is less than 50 , it should append blank spaces to sync with all other rows
def format_file(inFile, outFile,dic):
expected = 0
gap = " "*50
len1=50
newLine = []
join = " ".join
write = outFile.write
for line in inFile:
sLine = line[:-1]
linePos = dic[line[:3]]
if linePos == 4:
continue
if linePos == 0:
if newLine:
if len(line)!=len1:
gap2=len1-len(line)
newline.append(" "*gap2)
write(join(newLine)+"\n")
newline = [sLine]
newLine = [sLine]
expected = 1
else:
gaps = linePos - expected
if gaps != 0:
newLine.append(gap*gaps)
expected = linePos + 1
else:
expected += 1
newLine.append(sLine)
if len(line)!=len1:
gap2=len1-len(line)
newline.append(" "*gap2)
if newLine:
newLine[-1] = line
if len(line)!=len1:
gap2=len1-len(line)
newline.append(" "*gap2)
write(join(newLine))
def main():
positions = {"abc":0,
"cde":1,
"efg":2,
"hij":3,
"xyz":4}
with open("Text.txt","r") as inTxt, open("NewText.txt","w")as outTxt:
format_file(inTxt,outTxt,positions)
if __name__ == "__main__":
main()