Write a program that reads the “file.in” file, extracts columns 10-14 and writes
ID: 3802539 • Letter: W
Question
Write a program that reads the “file.in” file, extracts columns 10-14 and writes the number into a file, “file.out” as shown. The number is to be written 2 times into the file in columns of width 15 each. The first column should be left justified, the second right justified. The second column should display the number divided by 100.0 in scientific notation.
“file.in”
“file.out”
abcdefghi123456789012345678901234567890abcdefghi23456789012345678901234567890s
abcdefghi3456789012345678901234567890gj
abcdefghi456789012345678901234567890dnf
abcdefghi56789012345678901234567890djdi
abcdefghi6789012345678901234567890rrggg
abcdefghi789012345678901234567890fggffg
abcdefghi89012345678901234567890gbbfgfg
abcdefghi9012345678901234567890fgfgfgfg
abcdefghi012345678901234567890fgfgfgfgf
abcdefghi123456789012345678901234567890
Explanation / Answer
Python 2.7 code:
import os
from decimal import Decimal
in_file = "filein.txt"
f= open("file.out","w+")
content = []
if os.path.exists(in_file):
with open(in_file) as in_f:
content = in_f.readlines()
else:
print "input file does not exist"
for i in range(0,len(content)):
f.write(content[i][9:14])
f.write(" ")
f.write('%E' % Decimal(float(content[i][9:14])/100.0))
f.write(" ")
Sample filein.txt:
abcdefghi123456789012345678901234567890
abcdefghi23456789012345678901234567890s
abcdefghi3456789012345678901234567890gj
abcdefghi456789012345678901234567890dnf
abcdefghi56789012345678901234567890djdi
abcdefghi6789012345678901234567890rrggg
abcdefghi789012345678901234567890fggffg
abcdefghi89012345678901234567890gbbfgfg
abcdefghi9012345678901234567890fgfgfgfg
abcdefghi012345678901234567890fgfgfgfgf
abcdefghi123456789012345678901234567890
Sample file.out
12345 1.234500E+02
23456 2.345600E+02
34567 3.456700E+02
45678 4.567800E+02
56789 5.678900E+02
67890 6.789000E+02
78901 7.890100E+02
89012 8.901200E+02
90123 9.012300E+02
01234 1.234000E+01
12345 1.234500E+02
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.