Hello,

I have this code

#!/usr/bin/env python

import csv
import random
alpha = 'abcdefghijklmnopqrstuvwxyz'
e = range(0,99)
x = random.sample(alpha,14)

c = csv.writer(open("test2.csv", "wb"))
c.writerow([x])

cr = csv.reader(open("test2.csv","rb"))

for row in cr:
        print ', '.join(row)

print "How many rows?"
l = input()

k = 1

while k < int(l):

        y = random.sample(e,14)
        c.writerow([y])

        cr = csv.reader(open("test2.csv","rb"))
        for row in cr:
                print ','.join(row)

        k += 1

print "done"

which produces (when l is set to 6):

"['y', 'g', 'v', 'f', 'd', 'c', 'h', 's', 'm', 'p', 'j', 't', 'x', 'e']"
"[79, 55, 74, 70, 73, 80, 69, 57, 83, 9, 50, 87, 37, 5]"
"[2, 90, 69, 40, 27, 30, 55, 80, 42, 12, 51, 44, 83, 16]"
"[9, 35, 56, 96, 76, 29, 44, 68, 95, 33, 97, 67, 15, 39]"
"[86, 48, 29, 98, 81, 24, 79, 69, 82, 18, 77, 27, 12, 94]"
"[74, 29, 75, 81, 0, 20, 87, 71, 2, 36, 5, 64, 61, 9]"

is there anyway to get rid of all the brackets and quotation marks?

Recommended Answers

All 6 Replies

print ','.join(row)[2:-2]

print ','.join(row)[2:-2]

Nothing changed

Nothing changed

The problem is that when you print a list, it prints the repr() of the list elements. To remove the quotation marks, you must apply str() to each element and join the results with commas

>>> import random
>>> from string import ascii_lowercase as alpha
>>> x = random.sample(alpha,14)
>>> print x
['m', 'y', 'b', 'k', 'z', 't', 'a', 'r', 'n', 'o', 'c', 'w', 'q', 'v']
>>> print ", ".join(str(elt) for elt in x)
m, y, b, k, z, t, a, r, n, o, c, w, q, v
>>> def print_list_elements(alist):
...  print ", ".join(str(elt) for elt in alist)
... 
>>> print_list_elements(x)
m, y, b, k, z, t, a, r, n, o, c, w, q, v
>>>
>>> x = random.sample(range(99), 14)
>>> print_list_elements(x)
74, 40, 97, 96, 36, 86, 19, 6, 4, 30, 0, 34, 46, 35

Here is what I updated but it did not change my output at all.

#!/usr/bin/env python

import csv
import random
from string import ascii_lowercase as alpha
x = random.sample(alpha,14)

c = csv.writer(open("test3.csv", "wb"))
c.writerow([x])

cr = csv.reader(open("test3.csv","rb"))

for row in cr:
        print ",".join(str(elt) for elt in x)

print "How many rows?"
l = raw_input()

k = 1

while k < int(l):

        y = random.sample(range(99), 14)
        c.writerow([y])

        cr = csv.reader(open("test3.csv","rb"))

        for row in cr:
                print ", ".join(str(elt) for elt in y)
        k +=1

print "done"

Here is what I updated but it did not change my output at all.

#!/usr/bin/env python

import csv
import random
from string import ascii_lowercase as alpha
x = random.sample(alpha,14)

c = csv.writer(open("test3.csv", "wb"))
c.writerow([x])

cr = csv.reader(open("test3.csv","rb"))

for row in cr:
        print ",".join(str(elt) for elt in x)

print "How many rows?"
l = raw_input()

k = 1

while k < int(l):

        y = random.sample(range(99), 14)
        c.writerow([y])

        cr = csv.reader(open("test3.csv","rb"))

        for row in cr:
                print ", ".join(str(elt) for elt in y)
        k +=1

print "done"

Here is a modified version which should work. It's not good to keep a file opened for reading and for writing at the same time, or to open a file many times in a loop.

#!/usr/bin/env python

import csv
import random
from string import ascii_lowercase as alpha


with open("test3.csv", "wb") as out_f: # <--- using 'with' ensures that the file is closed automatically
    c = csv.writer(out_f)
    x = random.sample(alpha,14)
    c.writerow(x)
    nrows = int(raw_input("How many rows? "))
    r = range(99)
    for k in range(nrows):
        y = random.sample(r, 14)
        c.writerow(y)

with open("test3.csv", "rb") as in_f:
    cr = csv.reader(in_f)
    for row in cr:
        print ", ".join(str(elt) for elt in row)

print "done"

""" my output -->
How many rows? 4
f, p, t, q, g, i, c, k, x, b, m, e, y, r
46, 75, 22, 98, 8, 78, 79, 37, 84, 30, 49, 86, 41, 34
15, 42, 34, 35, 5, 32, 18, 54, 91, 61, 51, 0, 95, 88
59, 3, 16, 56, 15, 42, 97, 34, 71, 96, 74, 30, 7, 68
35, 2, 23, 56, 18, 31, 73, 88, 40, 59, 21, 93, 94, 64
done
"""

Thank you so much Gribouillis.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.