以下是我讀取包含約25行的.csv文件的代碼。每行的輸出是相同的。我希望能夠完成的是每個「行」都有一個隨機順序。下面是代碼:Python輸出從ReadLines中的隨機順序()
f_in = open("input.csv",'r')
f_out = open('output.txt', 'w')
for line in f_in.readlines():
f_out.write('<p>' + random.choice(list(open('content.txt'))).replace("\n", "").replace(".", "").replace("?", "").strip().capitalize() + ' <a href="' +
line.replace("\n", "").split(",")[0]+"" + '">' + line.replace("\n", "").split(",")[1]+"" + '</a> ' + random.choice(list(open('content.txt'))).replace("\n", "").strip().lower() + '</p>' +
#
'<p>' + random.choice(list(open('content.txt'))).replace("\n", "").replace(".", "").replace("?", "").strip().capitalize() + ' <a href="' +
line.replace("\n", "").split(",")[2]+"" + '">' + line.replace("\n", "").split(",")[3]+"" + '</a> ' + random.choice(list(open('content.txt'))).replace("\n", "").strip().lower() + '</p>' +
#
'<p>' + random.choice(list(open('content.txt'))).replace("\n", "").replace(".", "").replace("?", "").strip().capitalize() + ' <a href="' +
line.replace("\n", "").split(",")[4]+"" + '">' + line.replace("\n", "").split(",")[5]+"" + '</a> ' + random.choice(list(open('content.txt'))).replace("\n", "").strip().lower() + '</p>' +
#
'\n')
f_in.close()
f_out.close()
這是什麼是輸出<p>text a link text</p><p>text a link text</p><p>text a link text</p>
這是很好的,這就是我想要的,但我需要第2行是按不同的順序以及行3等等。
例如,它從第1行讀取的第一個輸出將是列AB CD EF,我想要的是對於第2行,輸出爲列EF AB CD。因此,對於.csv文件中的每一行,輸出需要重新排序,而不僅僅是.csv文件中每25行的AB CD EF。
我不是真正的Python高級,我的代碼可以做不同,這是我知道如何得到這個最好的方式。有人可以幫我試試得到一個可以實現這種輸出的工作代碼嗎?謝謝。
取樣輸入數據從CSV文件:
Line 1 --> Column A http://domain.com Column B my anchor text 1 Column C http://domain.com Column D my anchor text 2 Column E http://domain.com Column F my anchor text 3
Line 2 --> Column A http://domain.com Column B my anchor text 1 Column C http://domain.com Column D my anchor text 2 Column E http://domain.com Column F my anchor text 3
Line 3 --> Column A http://domain.com Column B my anchor text 1 Column C http://domain.com Column D my anchor text 2 Column E http://domain.com Column F my anchor text 3
CSV數據
http://domain.com,anchor text 1,http://domain2.com,anchor text 2,http://domain3.com,anchor text 3
http://domain.com,anchor text 1,http://domain2.com,anchor text 2,http://domain3.com,anchor text 3
http://domain.com,anchor text 1,http://domain2.com,anchor text 2,http://domain3.com,anchor text 3
所需的輸出一行做什麼,我想你
Line 1 --> Column A and B Column E and F Column C and D
Line 2 --> Column E and F Column A and B Column C and D
Line 3 --> Column C and D column E and F Column A and B
對不起,目前尚不清楚你在做什麼。你可以添加一個來自它的輸入和輸出樣本嗎? – martineau
我添加了一些示例輸入和輸出數據。我希望我解釋得更好。 – Matt
這還不是很清楚你想要做什麼 – Nate