1

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.

0

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

.extend() would be better than += with a slice.
row_test += scris[0:7] instead of the row_test = [] before the for loop? I cant do that... That row_test = [] is for writing colomns.
1

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'

1 Comment

exactly, its a list of lists... how can i write that list of lists without that damned brackets?

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.