1

I have a text file which contains 40 words, e.g. one two three ... forty. All words are below one another, no commas included. I need to print them to screen or to another file one next to each other, separated with commas, (e.g.: one, two, three, ...) and also have them wrap up every ten (10), or seven (7) words. This is my code, which fails to work:

import textwrap
flag = 1
comma = ', '

with open('drop_words.txt', encoding='utf-8') as file:
    content = file.read()
    content = content.split()
    words = comma.join(content)
    if len(content)%7 == 0:
        print(words, '\n')

Can anyone help? Thank you.

DirtyBit
  • 16,613
  • 4
  • 34
  • 55
G. Trialonis
  • 83
  • 3
  • 11

2 Answers2

1

drop_words.txt:

one
two
three
four

and then:

with open('drop_words.txt', encoding='utf-8') as file:
    content = file.readlines()
    # you may also want to remove empty lines
    content = [l.strip() for l in content if l.strip()]
    print(", ".join(content), end = '')

OUTPUT:

one, two, three, four

EDIT:

and if by wrapping the words together you mean grouping them, you could use a grouper like:

import itertools as IT

def grouper(n, iterable):
    iterable = iter(iterable)
    return iter(lambda: list(IT.islice(iterable, n)), [])

with open('list.txt', encoding='utf-8') as file:
    content = file.readlines()
    content = [l.strip() for l in content if l.strip()]
    print(", ".join(content))
    grouping = ", ".join(content)
    #creating a list out of the comma separated string
    grouping = grouping.split(",")
    # grouping the two elements
    print(list(grouper(2, list(grouping))))

OUTPUT:

one, two, three, four
[['one', ' two'], [' three', ' four']]

EDIT 2:

OP mentioned the pack of 10 digits in a row

wrap = 0
newLine = True
with open('list.txt', encoding='utf-8') as file:
    content = file.readlines()
    # you may also want to remove empty lines
    content = [l.strip() for l in content if l.strip()]
    for line in content:
        if wrap < 10:
            print("{}, " .format(line), end = '')
        else:
            if newLine:
                print("\n")
                newLine = not newLine
            print("{}, ".format(line), end='')
        wrap += 1

OUTPUT:

one, two, three, four, five, six, seven, eight, nine, ten, 

eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, 
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
  • 1
    Thank you. Your code helps me a lot, although I failed to communicate what I really wanted to say. What I meant was not to wrap the words at the current size of a text window, but introduce a carriage return [ENTER] every ten words, so that each line in the text window would have ten words. However, I intend to incorporate your code into my program. It does the job in a different way. Thank you. – G. Trialonis Jan 31 '19 at 12:14
  • @G.Trialonis I'm glad it helped, you may mark the answer by clicking on the tick mark to accept it, cheers :) – DirtyBit Feb 07 '19 at 06:02
0

this might do the job for printing the names:

with open('drop_words.txt', encoding='utf-8') as f:
    words = [line for line.strip() in f]
    print(','.join(words))

If you want to wrap them by pack of 7 words, you can use a function as following:

def grouped(iterable, n):
    return zip(*[iter(iterable)]*n)

>>> grouped(word, 7)
Julien Briand
  • 275
  • 1
  • 7