1

I have a list in Phyton similar to:

mylist ='thank you i love you ', 'my mom my biggest supporter’,’ my life line my best friend was a single parent worked 2 jobs to support us im so blessed to have her ’,’ as a mom and now shes my babys nonna happy mothers day mommy i love you', 'me and my mom love her to pieces'.

I want to save a csv or txt file whose output should look like:

1. thank you  i love you
2. my mom my biggest supporter
3. my life line my best friend was a single parent worked 2 jobs to support us im so blessed to have her
4. as a mom and now shes my babys nonna happy mothers day mommy i love you
5. me and my mom love her to pieces 

I’ve been trying with:

for item in mylist:
    mylist.write("%s/n" % item)

But I get:

AttributeError: 'list' object has no attribute 'write'

How shoul I proceed?

Thanks in advance!

3
  • You can usethe pandas module: stackoverflow.com/questions/29310792/… Commented Mar 17, 2017 at 20:51
  • also note that csv stands for comma-separated values, which is not really what you present as desired output. Commented Mar 17, 2017 at 21:20
  • She'll marry you ! Commented Mar 17, 2017 at 21:33

4 Answers 4

3

That may seem a trivial one, but I didn't find any duplicates answering that particular simple question (looked for them for several minutes!) so here's my proposal:

with open("output.txt","w") as f:
    for i,item in enumerate(mylist,1):
        f.write("{}. {}\n".format(i,item))
  • mylist is an input, you cannot write into it. You have to open a file object and iterate on mylist elements (using enumerate to zip it with the indexes starting at 1).
  • also you wrote /n for linefeed character, which should be \n (stay away from os.linesep as it would add \r twice on Windows)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. It was perfect.
0

First, you need to change the list slightly as sometimes you are using instead the '. This is the corrected list:

myList = 'thank you i love you ', 'my mom my biggest supporter', ' my life line my best friend was a single parent worked 2 jobs to support us im so blessed to have her ', ' as a mom and now shes my babys nonna happy mothers day mommy i love you', 'me and my mom love her to pieces'

Secondly, you need to be writing the values to a file object, not a list object.

file_object = open('the_output_file_name.txt', 'w')    #creates a file

for item in myList:
    file_object.write("%s\n" % item)    #writes the item to the file

file_object.close()

If you want the output to have line numbers like in your example, you could use this which iterates over the list and over a list of numbers equal to the length of the list:

file_object = open('the_output_file_name.txt', 'w')    #creates a file

for item, number in zip(myList, range(len(myList))):    #loop over myList and a range()
    file_object.write("%d. %s\n" % (number + 1, item))    #writes the item to the file

file_object.close()

Comments

0

mylist is a list object, which do not have write function. Therefore you got an AttributeError.

You need to open a file to write some data, and here's my solution:

with open('output.txt', 'w') as f:
    [f.write("%d. %s\n" % (i,v)) for i,v in enumerate(mylist, 1)]   

Comments

0

You should write to a file not to a list object, also, the single quotes are wrong, try the following:

open('text.txt', 'a').write("\n".join(mylist))

Comments

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.