简体   繁体   中英

Python csv write list

I have a code that writes a bunch of lines to csv files. It writes what i want, but... it inserts brackets in each column... The code is:

    root_text_csv = "C:/Users/s/Desktop/text/knife"+numarul_folderelor+".csv"
    fisier_text_knife = csv.writer(open(root_text_csv,'wb'))
    row_test = []
    for filenames in os.listdir(foldere_prrra):
        knife = filenames.replace("<SP>", " ").replace(".sys", "")
        test = ['cut '+knife+' off for fuged,', 'cut '+knife+' off,', 'cut '+knife+' fool alabala,', 'cut '+knife+' nieh,', 'prrra '+knife+' alabala,',
                'cut '+knife+' fuged streaming,', 'cut '+knife+' alabala fuged,', 'cut '+knife+' off alabala,', 'prrra '+knife+' fool alabala,',
                'cut '+knife+' off fuged,', 'prrra '+knife+' niether alabala,', 'cut off '+knife+' for fuged,' 'cut '+knife+' fuged fool alabala,',
                'cut '+knife+' off niether,', 'where to cut '+knife+',', ''+knife+' fool alabala off,', 'steel '+knife+' off fuged,', 'cut '+knife+' fuged niether,',
                ''+knife+' fool alabala off,', 'fuged streaming '+knife+',', 'cut '+knife+' niether,', 'cut '+knife+' fuged,', 'red '+knife+' off,', ''+knife+' off,',
                'red '+knife+',', 'cut '+knife+',', ''+knife+' fuged,', 'fuged '+knife+',', ''+knife+' off,', 'off '+knife+',', ''+knife+',']
        random.shuffle(test)
        scris = [x for x in test if len(x) <= 30]
        row_test.append(scris) #row_test.append(scris[0])
    fisier_text_knife.writerow(row_test)

If i replace the row_test.append(scris[0:7]) with row_test.append(scris[0]) it writes only one string off them all(but writes it without the brackets -->[ ]<--).

I want to write 7 or 8 lines. I wasted my brain on this code.

Thank you for the time.

The reason this is happening is because scris is a list.

instead of:

row_test.append(scris[0:7])

do this rather:

row_test += scris[0:7]

This works too (see comment below):

row_test.extend(scris[0:7])

If it still doesn't make sense open a console and type in the example stuff below. Every time you change l print it out.

the example below should show you why this works:

l = [1,2,3]          #    l = [1,2,3]
l.extend([4,5])      #now l = [1,2,3,4,5]
l.append([6,7])      #now l = [1,2,3,4,5,[6,7]]
l.extend('hi there') #raises an exception

When you call some_list.append(some_item) it puts some_item inside some_list . So if some_item is also a list then you get a list inside a list.

When you call some_list.extend(some_item) it joins some_item to the end of some_list . So if some_item is not a list then this will error.

EDIT I think i see what you want now...

So given a list like [['1','2','3'],['4','5','6'],['7','8','9']] you want the csv to look like: 123,456,789 , right?

Instead of:

fisier_text_knife.writerow(row_test)

Try this out:

csv_friendly = [''.join(l) for l in row_test]
fisier_text_knife.writerow(csv_friendly)

The string which will be written to the file is the same as when you print(row_test) - as a list of lists.

You can use join for that to create a large string for each line, something like:

row_test+= ','.join(scris) + '\n'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM