如何摆脱\ n,逗号和括号?

时间:2014-11-12 10:47:09

标签: python file random

这是我的代码:

import random
a = open("10.txt", "r")
#print (a.read())
grid1 = a.readlines()
random.shuffle(grid1) 
row1 = grid1[1].rstrip(), grid1[2].rstrip(), grid1[3].rstrip()
row2 = grid1[4].rstrip(), grid1[5].rstrip(), grid1[6].rstrip()
row3 = grid1[7].rstrip(), grid1[8].rstrip(), grid1[9].rstrip()
print (row1)
print (row2)
print (row3)

但它出现了:

('GHOST', 'TOOTH', 'BROWN')
('ABOUT', 'SMOKE', 'FUNNY')
('NIGHT', 'CAMEL', 'PRICE')

我希望它没有括号,逗号,语音标记等。

3 个答案:

答案 0 :(得分:0)

使用print(''.join(row1))依此类推

答案 1 :(得分:0)

或者甚至不打扰行:

#! /usr/bin/env python

import random

grid = [
'GHOST', 'TOOTH', 'BROWN',
'ABOUT', 'SMOKE', 'FUNNY',
'NIGHT', 'CAMEL', 'PRICE']

print grid

random.shuffle(grid)

print ''.join(["%s%s" % (word, ' ' if i % 3 else '\n') for i, word in enumerate(grid, 1)])

典型输出

['GHOST', 'TOOTH', 'BROWN', 'ABOUT', 'SMOKE', 'FUNNY', 'NIGHT', 'CAMEL', 'PRICE']
TOOTH PRICE CAMEL
ABOUT NIGHT SMOKE
BROWN GHOST FUNNY

(加一条空白行)

答案 2 :(得分:0)

因为row1是元组python,所以使用元组语法打印它。菊

row1 = ('GHOST', 'TOOTH', 'BROWN')
row2 = ('ABOUT', 'SMOKE', 'FUNNY')
row3 = ('NIGHT', 'CAMEL', 'PRICE')

for row in [row1, row2, row3]:
    for x in row:
         print x,
    print