0

Is it possible to write a string stored into a list into a .CSV file into one cell?

I have a folder with files and I want to write the file names onto a .csv file.

Folder with files:

    Data.txt
    Data2.txt
    Data3.txt

Here is my code:

    import csv
    import os
    index = -1 
    filename = []
    filelist = []
    filelist = os.listdir("dirname")

        f = csv.writer(open("output.csv", "ab"), delimiter=",", quotechar=" ", quoting=csv.QUOTE_MINIMAL)

    for file in filelist:
        if (len(filelist) + index) <0: 
            break
        filename = filelist[len(filelist)+index]
        index -= 1
        f.writerow(filename) 

Output I'm getting is one letter per cell in the .csv file:

      A B C D E F G H I       
    1 D a t a . t x t
    2 D a t a 2 . t x t
    3 D a t a 3 . t x t 

Desired output would be to have it all in 1 cell. There should be three rows on the csv file with strings "Data.txt" in cell A1, "Data2.txt" in cell B1, and "Data3.txt" in cell C1:

      A          B
    1 Data.txt
    2 Data2.txt
    3 Data3.txt

Is it possible to do this? Let me know if you need more information. I am currently using Python 2.7 on Windows 7.

Solution/Corrected Code:

    import csv
    import os
    index = -1 
    filename = []
    filelist = []
    filelist = os.listdir("dirname")

        f = csv.writer(open("output.csv", "ab"), delimiter=",", quotechar=" ", quoting=csv.QUOTE_MINIMAL)

    for file in filelist:
        if (len(filelist) + index) <0: 
            break
        filename = filelist[len(filelist)+index]
        index -= 1
        f.writerow([filename]) #Need to send in a list of strings without the ',' as a delimiter since writerow expects a tuple/list of strings.
8
  • Please can you post the code that causes the issue, the code posted has some basic syntax errors and a missing import. Also you set index to -1 and break if index is less than 0 causing the script not do anything. Commented Jun 25, 2014 at 15:43
  • Updated @Noelkd Sorry about that. Commented Jun 25, 2014 at 15:53
  • It's still not right, getting a syntax error on the if line because of a missing colon, can you take a look at this Commented Jun 25, 2014 at 15:55
  • Now it is :D Forgot to put that in when i rewrote it Commented Jun 25, 2014 at 15:58
  • output.csv should be a string Commented Jun 25, 2014 at 16:00

2 Answers 2

3

You can do this:

import csv
import os

filelist = os.listdir("dirname")  # Use a real directory
f = csv.writer(open("output.csv", 'ab'), delimiter=",", quotechar=" ", quoting=csv.QUOTE_MINIMAL)
for file_name in filelist:
    f.writerow([file_name])

Writerow expects a sequence, for example a list of strings. You're giving it a single string which it is then iterating over causing you to see each letter of the string with a , between.

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

2 Comments

That solved it. Never knew it was just to have the brackets :D That makes a lot of sense now. Have a great day! Thanks for all of the advice and help @Noelkd
No problem try to fix your code blocks in the question you only need a four space indent.
0

you can also put each sentence/value that you want to be in 1 cell inside a list and all the internal lists inside one external list.

something like this:

import csv
import os

list_ = [["value1"], ["value2"], ["value3"]]
with open('test.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(list_)

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.