0

I am trying to take a list of few numbers and split every number to a new line into new file.

Thus f=['Hans', 'Anna', 'Vladimir', 'Michael', 'Ed', 'Susan', 'Janice', 'Jo'] in a file names.txt need to take len of the list in f and separate each len to new line.

['4 4 8 7 2 5 6 2 '] should be in a created file

   4
   8
   7
   2
   5
   6
   2

Tthis is what I did :

with open("names.txt","r") as f:
    f=f.read()
    f=f.split()  # take from original file

def size(x):

    with open("name_length.txt","w") as w:
        l=[str(len(n))+' ' for n in x]   # create the len list
        l="".join(l)
        l=str(l)
        l=l.split('\n+')
        print(f'this,{l}')
        print(f'this is,{l}')
        w.writelines(l)
        return l

print(size(f))         
1
  • Could you edit your post with your expectation output?Your format isn't very good. Commented Apr 10, 2020 at 5:12

1 Answer 1

2

Try this :

#Read the data from input file.

with open("names.txt","r") as f:
    input_list = f.read().splitlines()    # Will look like : lines = ['Hans', 'Anna', 'Vladimir', 'Michael', 'Ed', 'Susan', 'Janice', 'Jo']

def write_len(input_list):
    with open("name_length.txt","w") as w:
        for item in input_list:
            f.write("{0}\n".format(len(item)))

    return 1

print(size(input_list))

I did not understand why you needed to create a string with join first and then split it. Above is an example of how you can directly write the length of strings into a file with every element in new line.

Also, I would like to add that you opened a file for reading and named the file pointer as f. In the next line you are using same variable f for storing the value of read"(). This is something you should avoid doing. Context of variable f is changing and may create problem in some scenarios.

Here is how you can use list comprehension and join to accomplish the same :

#Read the data from input file.

with open("names.txt","r") as f:
    input_list = f.read().splitlines()    # Will look like : lines = ['Hans', 'Anna', 'Vladimir', 'Michael', 'Ed', 'Susan', 'Janice', 'Jo']

def write_len(input_list):
    final_str = "\n".join([str(len(item)) for item in input_list])

    with open("name_length.txt","w") as w:
        w.write(final_str)

    return 1

print(size(input_list))

OUTPUT :

4
4
8
7
2
5
6
2

The above output gets written in a file.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very helpful, the output should be each number in separate line thus 4 in the first line then 4 then 8 then 7 etc , in addition if possible to use list comprehension ,reduce ,map or filter and not to use for loop ,this exercise meant to show how this functions can replace the for loop in few lines
The earlier solution does the same thing of writing each number in separate line of a file. However I have given another solution using list comprehension which will also do the same thing and the output is written in a file and looks like above.
Feel free to ask any further doubts. Also don't forget to upvote and mark answer accepted if it helped. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.