2017-06-18 92 views
0

我想用.join()方法將字符串列表加入到一個字符串中。然而,看起來結果仍然是輸出仍然產生一個字母表的列表,而不是一個字。下面是培訓相關代碼:我怎樣才能將一個字符串列表加入到Python中的一個字符串中

import sys 
import traceback 
import weka.core.jvm as jvm 
#import wekaexamples.helper as helper 
from weka.core.converters import Loader 
from weka.classifiers import Classifier 


def main(args): 
# load a dataset 
loader = Loader(classname="weka.core.converters.ArffLoader") 
train = loader.load_file("C:/Users/Syahirah/Desktop/Train_FYP.arff") 
train.class_index = train.num_attributes - 1 
test = loader.load_file("C:/Users/Syahirah/Desktop/Test_FYP_prediction.arff") 
test.class_is_last() 

# classifier 
cls = Classifier(classname="weka.classifiers.bayes.NaiveBayes") 
cls.build_classifier(train) 

# output predictions 
#print("ID - actual - predicted ") 
for index, inst in enumerate(test): 
    #print "\n", inst 
    varinst = str(inst) 
    #print type(varinst),varinst 
    split_inst = varinst.split(',') 
    kata = split_inst[1] 
    semua = ' '.join(kata) 
    print semua 

這裏的輸出:

T h e 
c u s t o m e r s 
a r e 
a l l o w e d 
t o 
r e s e r v e 
r o o m 
o n l i n e 
' 
b y 
t h e 
w a y 
t h e y 
a b l e 
t o 
c h a n g e 
o r 
c a n c e l 
t h e i r 
r e s e r v a t i o n 

所需的輸出應該是這樣的:

The customers are allowed to reserve room online, by the way they able to 
change or cancel their reservation 
+0

你的'test'變量包含什麼?這並沒有太多的繼續。 –

+0

@速度實例變量 – Lily

+0

'test'變量包含具有變量的對象列表。在這段代碼中,我提取出第二個變量「word」。從單詞列表中,我想用完整的句子來表達它。我對解釋感到非常抱歉。這是不好解釋,因爲我真的是新的python。 – Lily

回答

2

在你的.py文件的第一行添加此行:

from __future__ import print_function 

接下來,將這些行更改S:

semua = ' '.join(kata) 
print semua 

要:

semua = ''.join(kata) 
print(semua, end='') 

你要確保你不添加空格和地方不需要換行。

+0

哇!謝謝 – Lily

+0

@Lily不用擔心。提問時所需要的只是一點點清晰。 ;) –

相關問題