file_IO.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #### This program is free software: you can redistribute it and/or modify
  2. #### it under the terms of the GNU General Public License as published by
  3. #### the Free Software Foundation, either version 3 of the License, or
  4. #### (at your option) any later version.
  5. ####
  6. #### This program is distributed in the hope that it will be useful,
  7. #### but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. #### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. #### GNU General Public License for more details.
  10. ####
  11. #### You should have received a copy of the GNU General Public License
  12. #### along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. d = open('output0.txt', 'w') ### For debuging. Using 'w' or 'a' creates a new file if it is missing
  14. ### Opening the "ASCII art" character source file for reading
  15. e = open('ASCII_alphabet.txt', 'r') ### Using 'r' for readin only
  16. e.seek(0)
  17. ### How do I find out how many lines a single "ASCII art" character takes?
  18. ### How do I count the lines?
  19. ## Detecting "ASCII art" chars and asinging them to data struture (tuple of tuples)
  20. #### TODO: Create "syntax checking" of the alphabet.txt file
  21. #### TODO: Find how to detect EOF (now alphabet.txt needs two '\n' chars at EOF
  22. str_temp = ''
  23. chr_tup = ()
  24. ds_tup = ()
  25. if(e.read(1) != ''):
  26. e.seek(0)
  27. r = e.read(1)
  28. e.seek(0)
  29. while(r != ''):
  30. ##print('This is r:' + r) ##for debuging
  31. stp = e.tell() #latest stop point
  32. ##print(stp) ##for debuging
  33. if(r != '\n'): ##checking if is not a line end
  34. rl = e.readline()
  35. str_temp = (rl[0:len(rl)-1]) #rl[0:len(rl)-1]
  36. chr_tup += (str_temp, )
  37. ##print(srt_tuple)
  38. d.write(str_temp + '\n')
  39. stp = e.tell()
  40. ##print(stp) ##for debuging
  41. else:
  42. r = e.read(1)
  43. if(r == '\n'):
  44. ##print('End of ASCII art character')
  45. d.write('End of ASCII art character\n')
  46. #d.write('\n')
  47. ds_tup += (chr_tup,) #ds_tup.append(chr_tup)
  48. chr_tup = ()
  49. r = e.read(1) ##r is incremented, checking for EOF
  50. stp = e.tell() ## stp must be updated!!!
  51. e.seek(stp-1) ## step back to the start of line
  52. ##print('EndOFCylcle') ##for debuging
  53. #print('EOF\n') ##for debuging
  54. #d.write('\nEOF') ##for debuging
  55. d.write('\n')
  56. d.write(str(ds_tup) + '\n\n') ##Extra newline is '\n' needed because character detection is retarded
  57. ### Text conversion to "ASCII art" text v1 #################################
  58. ###
  59. ### Problems:
  60. ## 1) No need to split the string, just "walk" the string char by char
  61. ## 1.1)Converting regular characters into ints or other numbers
  62. ## a)Create an array with "alphabet" (sorted list?)
  63. ## b)Use linear search for each character.
  64. ## 1.2)Deal with spaces between words.
  65. ## 2) Matching original text characters to "ASCII art" characters
  66. ## 3) Generating new strings of "ASCII art" characters
  67. ## Note: No lowercase letters yet!!! TODO: Add support for lowercase letters.
  68. ## For now turn lowercase letters into uppercase letters
  69. test_str = input('Enter a word: ')
  70. num_tup = ()
  71. ref_uppercase_tup = ()
  72. ref_lowercase_tup = ()
  73. for i in range(65, 91): #filling uppercase tupple
  74. ref_uppercase_tup += (chr(i),)
  75. for i in range(97, 123): #filling lowercase tupple
  76. ref_lowercase_tup += (chr(i),)
  77. ###### Converting a regular text string into tuple of ints #################
  78. for c in test_str: #"walking" the string
  79. #print(c) #for debuging
  80. if c != ' ':
  81. for y in range(len(ref_uppercase_tup)):
  82. if c == ref_uppercase_tup[y]:
  83. num_tup += (y,)
  84. break
  85. for u in range(len(ref_lowercase_tup)):
  86. if c == ref_lowercase_tup[u]:
  87. num_tup += (u,)
  88. break
  89. else:
  90. num_tup += (-1,)
  91. ###### Generating ""ASCII art" character string ##############################
  92. ascii_tup = ()
  93. ascii_row_str = ''
  94. for i in range(0, len(ds_tup[0])):
  95. #print(ds_tup[0][i])
  96. for u in range(0, len(num_tup)):
  97. if(num_tup[u] >= 0):
  98. #print(num_tup[u]) #for debuging
  99. ascii_row_str += (ds_tup[num_tup[u]][i] + ' ')
  100. else:
  101. ascii_row_str += ' '
  102. ascii_tup += (ascii_row_str,)
  103. ascii_row_str = ''
  104. print(ascii_tup[i])
  105. e.close()
  106. d.close()