0

So I just dont know how to interpret what I wanted to do, but I will state otherwise. I have a input.txt which holds these list of words separated by 1 blank line:

your
name
is

kimi
no
nawa

Now i wanted them to be stored in a lists or string with similar results like:

list = ['your','name','is','kim','no','nawa']

or

string = 'yournameiskiminonawa

or

list1 = ['your','name','is']
list2 = ['kim','no','nawa']

string = 'kiminonawa'
string1 = 'yournameis'

and below is my startup code:

values = []
    for line in open('input.txt'):
    line = line.split()
    for value in line:
        values.append(value)
    print line
SovietSenpai
  • 277
  • 2
  • 3
  • 16
  • Which of the four possible outputs that you list do you actually want? – Tagc Jan 07 '17 at 23:56
  • Also, does your file deliberately have that extra empty line? You are also really complicating this. Keep only `for line open('input.txt')` and print `line` only to see what is happening in the first loop. Remove that extra inner loop. Look at what you have, and you will realize what your mistake is – idjaw Jan 07 '17 at 23:58
  • All of them. I'm experimenting right now, so it would be best to take them all and teach me the proper way.. – SovietSenpai Jan 07 '17 at 23:59
  • I agree with @Tagc. Which output do you want? – Christian Dean Jan 07 '17 at 23:59
  • @idjaw yes it does have, and I'm really new to python. – SovietSenpai Jan 08 '17 at 00:00

2 Answers2

1

Here are implementations of functions that give you outputs in the four ways you specify. They operate on string data, which you can access by opening the file with open('input.txt').read().

DATA = """
your
name
is

kimi
no
nawa"""

def as_one_list(s):
    return [x for x in s.splitlines() if x]

def as_one_string(s):
    return ''.join(as_one_list(s))

def as_two_lists(s):
    return tuple(as_one_list(x) for x in s.split('\n\n')[:2])

def as_two_strings(s):
    return tuple(as_one_string(x) for x in s.split('\n\n')[:2])

print(as_one_list(DATA))
print(as_one_string(DATA))
print(as_two_lists(DATA))
print(as_two_strings(DATA))

Output

['your', 'name', 'is', 'kimi', 'no', 'nawa']
yournameiskiminonawa
(['your', 'name', 'is'], ['kimi', 'no', 'nawa'])
('yournameis', 'kiminonawa')
Tagc
  • 8,736
  • 7
  • 61
  • 114
0

Which one do you really want?

l=[i for i in open('input.txt') if len(i.strip()] 

Here is an example

NinjaGaiden
  • 3,046
  • 6
  • 28
  • 49