1

I am taking an input file that contains lists of numbers with different statements separated by spaces and taking those numbers, adding two next to each other and printing out a word out if UTF-8. so if a list of numbers were 67 5 100 1 11 97 98 10 1 110, the numbers would become 72, 101, 108, 108 and 111 which reads Hello in UTF-8.

I currently have the following code:

file_name = input('Enter filename: ')
input_file = open(file_name, 'r')
word_list = []
count3 = 0

for line in input_file:
    count1 = 0
    count2 = 1
    count3 += 1
    word_str = ''
    line = line.split()
    length = len(line)

    while count1 < length:
        num_char = int(line[count1]) + int(line[count2])
        count1 += 2
        count2 += 2
        word_str += chr(num_char)
    word_list.append(word_str)

print (word_list)

example input file:

67 5 100 1 11 97 98 10 1 110 
15 72 10 101 47 67 88 20 94 6 22 11

61 11 93 4 73 39 78 34 17 104
23 43 11 93 65 52 20 96 66 31 86 24 40 61 102 13 50 51
73 43 28 73 8 89 31 68 77 27 24 77 42 72 15 24 64 51
25 75 7 90 10 111 17 16

From this code I get the output:

['Hello', 'World!', '', 'Happy', 'Bhutanese', "teacher's", 'day!']

My problem is that I need to sort the output in my list with the last statement first and the first statement last.

So for the provided numbers the expected output would read:

['Happy Bhutanese teacher's day!',
'Hello World!']

Any help is greatly appreciated

Fischer
  • 61
  • 2
  • 10
  • 1
    please post a small example that includes input, code , expected output as well as actual output ... or at least tell us what is wrong with your current code] – Joran Beasley Nov 05 '14 at 23:07
  • 2
    sorting a list is usually done using `sorted` – njzk2 Nov 05 '14 at 23:08
  • Sure you don't want to add modulo 256? And it seems ***you don't want sorting, but re-ordering***, inverting the order. – Deduplicator Nov 05 '14 at 23:13
  • Note: `count3` is initialized to 0 and incremented it for each line in the file (`count3 += 1`), but **is never used** - you can safely delete it. – rosshamish Nov 06 '14 at 00:35

2 Answers2

1

It seems that you don't want a list of individual words - you want a list of sentences, where the sentences are separated blank lines.

For clarity, you should rename word_list to sentence_list. I'll refer to it as sentence_list from now on.

To build sentences, put a space between each word. Since sentences span multiple lines, you don't want to re-initialize word_str in every loop - you'll need to keep it around. To do this, initialize it once, before the loop.

word_str = ''
for line in input_file:
    ...

Instead of always appending the line's word to the list, word_list.append(word_str), you'll want to handle it differently for a word than for a blank line.

If the line just processed was a word, add a space to the end.

If it was a blank line, add the current word_str to the sentence_list, and reset word_str to an empty string.

You can decide which to do by checking the line's length: blank lines have length 0.

if length == 0:
    sentence_list.append(word_str)
    word_str = ''
else:
    word_str += ' '

Since you want the last sentence to appear first in the list, you'll need to reverse the list before printing it.

sentence_list.reverse()
print(sentence_list)
rosshamish
  • 1,160
  • 12
  • 15
  • I really appreciate your help and like what you're thinking. My output only reads ['Hello World! '] when i put in your changes though. Any thoughts? – Fischer Nov 06 '14 at 03:12
  • 1
    Haha, oops. `word_str` only gets appended to `sentence_list` when it finds a blank line...can you see the problem now? Hint: if the input had 3 sentences, the output would only be the first two. – rosshamish Nov 06 '14 at 03:36
  • Also, notice that trailing space at the end of each word_str? You'll want to remove that before you append it to `sentence_list`. You can do that with slicing: http://stackoverflow.com/questions/509211/explain-pythons-slice-notation?rq=1 . Look for the "everything except the last 2 items" example the top answer gives. – rosshamish Nov 06 '14 at 03:40
0

Then instead of using append, add new word to the front by hand or use insert:

word_list = []
...
for ...:
...
    while ...:
        ...
    word_list = [word_str] + word_list
    #word_list.insert(0, word_str)

print(word_list)

There is also reverse function in list. You could just say:

word_list.reverse()
print(word_list)
Batuhan Tasdoven
  • 798
  • 7
  • 19
  • This helps some, but i would like to reverse the order base on spaces in the input code. Where there is a space after the first two lines or numbers starts a new set of numbers and I want those set of numbers to be processed first in order so 61..., 23..., 73..., 25... then process 67..., 15... – Fischer Nov 06 '14 at 00:12