2014-02-21 28 views
0

以下是我讀取包含約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 
+0

對不起,目前尚不清楚你在做什麼。你可以添加一個來自它的輸入和輸出樣本嗎? – martineau

+0

我添加了一些示例輸入和輸出數據。我希望我解釋得更好。 – Matt

+0

這還不是很清楚你想要做什麼 – Nate

回答

1

的一種方式「問因爲是將每個行的元組中的域/文本對分組,然後對該列表進行混洗。

下面是一些代碼,會從CSV文件中讀取,洗牌域/文本對每一行,並同時輸出文本和CSV文件,洗牌後的行:

import random 
import csv 

with open("input.csv") as infile: 
    csvreader = csv.reader(infile) 
    with open("output.csv", 'w') as outcsv: 
     csvwriter = csv.writer(outcsv) 
     with open("output.txt", 'w') as outtxt: 
      for row in csvreader: 
       random_pairs = [(row[2*i], row[2*i + 1]) for i in range(int(len(row)/2))] 
       random.shuffle(random_pairs) 
       outline = [] 
       for pair in random_pairs: 
        outtxt.write('<a href="' + pair[0] + '">' + pair[1] + '</a>') 
        outline.append(pair[0]) 
        outline.append(pair[1]) 
       outtxt.write('\n') 
       csvwriter.writerow(outline) 

使用您的CSV數據提供的結果在下面的輸出:

output.txt的:

<a href="http://domain3.com">anchor text 3</a><a href="http://domain2.com">anchor text 2</a><a href="http://domain.com">anchor text 1</a> 
<a href="http://domain3.com">anchor text 3</a><a href="http://domain.com">anchor text 1</a><a href="http://domain2.com">anchor text 2</a> 
<a href="http://domain2.com">anchor text 2</a><a href="http://domain3.com">anchor text 3</a><a href="http://domain.com">anchor text 1</a> 

OU tput.csv:

http://domain3.com,anchor text 3,http://domain2.com,anchor text 2,http://domain.com,anchor text 1 
http://domain3.com,anchor text 3,http://domain.com,anchor text 1,http://domain2.com,anchor text 2 
http://domain2.com,anchor text 2,http://domain3.com,anchor text 3,http://domain.com,anchor text 1 
+0

非常感謝!這正是我需要做的。我認爲我不知道如何解釋它,但你在挑選我想要的東西方面做得很好。這工作完美。謝謝。 – Matt

1

我已經嘗試了破解密碼進入功能;它應該更容易理解和維護。

import csv 
from itertools import izip 
import random 

LOREM_IPSUM = "content.txt" 
LINK_TEXT = "input.csv" 
OUTPUT  = "output.phtml" 

def csv_rows(fname, **kwargs): 
    with open(fname, "rb") as inf: 
     incsv = csv.reader(inf, **kwargs) 
     for row in incsv: 
      yield row 

def by_twos(iterable): 
    # given (a, b, c, d, ...) returns ((a,b), (c,d), ...) 
    return izip(*([iter(iterable)]*2)) 

def a(href, *content): 
    return "<a href=\"{0}\">{1}</a>".format(href, " ".join(content)) 

def p(*content): 
    return "<p>{0}</p>".format(" ".join(content)) 

def br(): 
    return "<br/>" 

def main(): 
    with open(LOREM_IPSUM) as inf: 
     lines = (line.strip() for line in inf) 
     content = [line.capitalize() for line in lines if line] 
    randtxt = lambda: random.choice(content) 

    with open(OUTPUT, "w") as outf: 
     for row in csv_rows(LINK_TEXT): 
      links = [a(href, text) for href,text in by_twos(row)] 
      random.shuffle(links) # randomize order 
      paras = (p(randtxt(), link, randtxt()) for link in links) 
      outf.write("".join(paras)) 
      outf.write(br()) 

if __name__=="__main__": 
    main()