drop_words.txt:
one
two
three
four
, а затем:
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 = '')
ВЫХОД:
one, two, three, four
РЕДАКТИРОВАТЬ:
, и если обернуть слова вместе, вы имеете в виду группирование их , вы можете использовать группировщик , например:
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))))
ВЫХОД:
one, two, three, four
[['one', ' two'], [' three', ' four']]
РЕДАКТИРОВАТЬ 2:
ОП упомянул пакет из 10 цифр подряд
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
ВЫХОД:
one, two, three, four, five, six, seven, eight, nine, ten,
eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen,