2013-10-11 14 views
-1

結果顯示是這樣的。我試過關於方法的問題,無論它看起來好像不能向上移動。謝謝你提前如何從新的新行寫入csv不是 n

  1. 反正有法院陣列向上?
  2. 或者我應該刪除該行直接全的

if __name__ == '__main__': #start of program

master = Tk() 
    newDirRH = "C:/VSMPlots" 
    FileName = "J123" 
    TypeName = "1234" 
    Field = [1,2,3,4,5,6,7,8,9,10] 
    Court = [5,4,1,2,3,4,5,1,2,3] 
    Field = "\n,,".join(str(x) for x in Field) # to leave a line in each Field array 
    Court = "\n,,,".join(str(y1) for y1 in Court) # to leave a line in each Court array 
    stringText = "Name, Type, Field, Court\n" + str(FileName) + ',' + str(TypeName) + ',' + Field + '\n,,,' + Court 

    newfile = newDirRH + "/Try.csv" 
    text_file = open(newfile, "w") 
    x = stringText 
    text_file.write(x) 
    text_file.close() 
    print "Done" 

enter image description here

+0

可能重複[?如何加入列表逐元素在Python(http://stackoverflow.com/questions/1885300/如何加入列表元素明智的蟒蛇) – tripleee

+0

你的問題不是很清楚措辭,但似乎很清楚,你應該實際上正在尋找如何加入兩個列表。 – tripleee

+0

嗨,我知道zip獲得2數組togehter但問題是它不能寫入CSV文件。我不想要如何加入2個列表 –

回答

1

首先請用CSV module寫CSV。其次,使用zip來創建字段/法院對的列表,例如,使用zip來創建字段/法院對的列表。

>>> Field = [1,2,3,4,5,6,7,8,9,10] 
>>> Court = [5,4,1,2,3,4,5,1,2,3] 
>>> zip(Field, Court) 
[(1, 5), (2, 4), (3, 1), (4, 2), (5, 3), (6, 4), (7, 5), (8, 1), (9, 2), (10, 3)] 

您可以通過此列表迭代如下:

for field, court in zip(Field, Court): 
    stringText = ','.join((FileName, TypeName, field, court)) 
+0

嗨你的代碼作品,但它是所有呈現在一個水平線...我怎樣才能使它垂直各自的列 –

+1

它沒有出現。你可以在'for'循環中用'stringText'來做任何你喜歡的事情。 – tripleee