Skip to main content
Code Review

Return to Question

Post Reopened by Mast , Community Bot, Morwenn, RubberDuck, rolfl
Slight tweaks.
Source Link
Morwenn
  • 20.2k
  • 3
  • 69
  • 132

The below pythonPython 3 program scans 4 files a.txt, b.txt, c.txt, d.txt and then outputs the read data into a file output.txt in a formatted way. The first line of each file is guarenteedguaranteed to have ana header and the second line of each file will be blank. I'm required to scan those four files.

The below python 3 program scans 4 files a.txt, b.txt, c.txt, d.txt and then outputs the read data into a file output.txt in a formatted way. The first line of each file is guarenteed to have an header and the second line of each file will be blank. I'm required to scan those four files.

The below Python 3 program scans 4 files a.txt, b.txt, c.txt, d.txt and then outputs the read data into a file output.txt in a formatted way. The first line of each file is guaranteed to have a header and the second line of each file will be blank. I'm required to scan those four files.

added 150 characters in body
Source Link
Spikatrix
  • 1k
  • 1
  • 9
  • 21

The below python 3 program scans 4 files a.txt, b.txt, c.txt, d.txt and then outputs the read data into a file output.txt in a formatted way. The first line of each file is guarenteed to have an header and the second line of each file will be blank. I'm required to scan those four files.

Program:

The below python 3 program scans 4 files a.txt, b.txt, c.txt, d.txt and then outputs the read data into a file output.txt in a formatted way. Program:

The below python 3 program scans 4 files a.txt, b.txt, c.txt, d.txt and then outputs the read data into a file output.txt in a formatted way. The first line of each file is guarenteed to have an header and the second line of each file will be blank. I'm required to scan those four files.

Program:

added 54 characters in body
Source Link
Spikatrix
  • 1k
  • 1
  • 9
  • 21

The below python 3 program scans 4 files a.txt, b.txt, c.txt, d.txt and then outputs the read data into a file output.txt in a formatted way. (Never mind about the bad variable names)Program:

def main():
 with open('a.txt', 'r') as afile_a, open('b.txt', 'r') as bfile_b, open('c.txt', 'r') as cfile_c, open('d.txt', 'r') as dfile_d:
 lines1, lines2, lines3, lines4 = afile_a.readlines(), bfile_b.readlines(), cfile_c.readlines(), dfile_d.readlines()
 lines = [lines1, lines2, lines3, lines4]
 number_of_spaces = 5
 assert len(lines1) == len(lines2) == len(lines3) == len(lines4), "Error. Different number of lines found in the files"
 
 irow, jcolumn = 10, 01
 with open('output.txt', 'w') as output:
 while jrow < len(lines):
 output.write(lines[j][0]lines[row][0].strip() + ' ' * number_of_spaces)
 jrow += 1
 output.write('\n')
 jrow = 0
 
 while icolumn < len(lines1):
 while jrow < len(lines):
 output.write(lines[j][i]lines[row][column].strip() + ' ' * (number_of_spaces + len(lines[j][0]lines[row][0].strip()) - len(lines[j][i]lines[row][column].strip())))
 jrow += 1
 output.write('\n') 
 icolumn += 1
 jrow = 0 
 
if __name__ == "__main__":
 main()

The below python 3 program scans 4 files a.txt, b.txt, c.txt, d.txt and then outputs the read data into a file output.txt in a formatted way. (Never mind about the bad variable names)

def main():
 with open('a.txt', 'r') as a, open('b.txt', 'r') as b, open('c.txt', 'r') as c, open('d.txt', 'r') as d:
 lines1, lines2, lines3, lines4 = a.readlines(), b.readlines(), c.readlines(), d.readlines()
 lines = [lines1, lines2, lines3, lines4]
 number_of_spaces = 5
 assert len(lines1) == len(lines2) == len(lines3) == len(lines4), "Error. Different number of lines found in the files"
 
 i, j = 1, 0
 with open('output.txt', 'w') as output:
 while j < len(lines):
 output.write(lines[j][0].strip() + ' ' * number_of_spaces)
 j += 1
 output.write('\n')
 j = 0
 
 while i < len(lines1):
 while j < len(lines):
 output.write(lines[j][i].strip() + ' ' * (number_of_spaces + len(lines[j][0].strip()) - len(lines[j][i].strip())))
 j += 1
 output.write('\n') 
 i += 1
 j = 0 
 
if __name__ == "__main__":
 main()

The below python 3 program scans 4 files a.txt, b.txt, c.txt, d.txt and then outputs the read data into a file output.txt in a formatted way. Program:

def main():
 with open('a.txt', 'r') as file_a, open('b.txt', 'r') as file_b, open('c.txt', 'r') as file_c, open('d.txt', 'r') as file_d:
 lines1, lines2, lines3, lines4 = file_a.readlines(), file_b.readlines(), file_c.readlines(), file_d.readlines()
 lines = [lines1, lines2, lines3, lines4]
 number_of_spaces = 5
 assert len(lines1) == len(lines2) == len(lines3) == len(lines4), "Error. Different number of lines found in the files"
 
 row, column = 0, 1
 with open('output.txt', 'w') as output:
 while row < len(lines):
 output.write(lines[row][0].strip() + ' ' * number_of_spaces)
 row += 1
 output.write('\n')
 row = 0
 
 while column < len(lines1):
 while row < len(lines):
 output.write(lines[row][column].strip() + ' ' * (number_of_spaces + len(lines[row][0].strip()) - len(lines[row][column].strip())))
 row += 1
 output.write('\n') 
 column += 1
 row = 0 
 
if __name__ == "__main__":
 main()
Post Closed as "Not suitable for this site" by RubberDuck, Morwenn, Jamal
Source Link
Spikatrix
  • 1k
  • 1
  • 9
  • 21
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /