2017-06-05 89 views
-3

我有一個文件,該文件看起來像這樣(2/3的連續的行,即隨機組合):Python的修改CSV數據

String A 
String B 
String C 
<Blank Row> 
String D 
String E 
<Blank Row> 
String F 
String G 
String H 
<Blank Row> 
String I 
String J 
String K 
<Blank Row> 
String L 
String M 

我想當存在連續3行中的輸出文件以除去中間排並轉置剩餘的2行。如果只有2行,它們應該轉置。最終的結果應該是這樣的。

String A,String C 
String D,String E 
String F,String H 
String I,String K 
String L,String M 

任何指針如何做到這一點?

+3

你迄今爲止編寫任何代碼? (請將其張貼出來;因爲SO不是編碼服務,所以你寫的問題是無關緊要的。) – DyZ

+0

一行:''\ n'.join(['{},{}'。format(l [ ('\ n \ n')]]])' – dawg

回答

1

您可以使用groupbycountitertools模塊以及list comprehension

這個答案有點不好意思,但是要訣竅。請參閱評論以更好地理解背後的邏輯。

我假設你的投入,你是在一個名爲my_input_file文件給出了輸入和輸出文件被稱爲output_file

from itertools import groupby, count 

# Read the file and split by the space between Value and its number 
# Leave the case where the empty string '' exists without splitting its spaces 
with open("my_input_file", 'r') as f: 
    data = (k.split() if k != '' else k for k in f.read().splitlines()) 

# Group the fields splitted, which are lists, in data 
# And separate them by the field where the string 'Blank' exists 
sub = [list(v) for _, v in groupby(data, lambda x: isinstance(x, list))] 

final = [] 
for elm in sub: 
    # if the lenght of the grouped elements is > 1 
    if len(elm) >1: 
     # Convert the number of the values into an int 
     # For further calculations 
     dd = map(lambda x: [x[0], int(x[1])], elm) 

     # Group the consecutive numbers of elem 
     for _,v in groupby(dd , lambda x,y=count(): x[1] - next(y)): 
      # If there is a consecutive numbers 
      bb = list(v) 
      if len(bb) >1: 
       # Conveert them into strings. Then, append the first and the final one to the final list 
       final.append(' '.join(map(str, bb[0])) + ',' + ' '.join(map(str, bb[-1]))) 

      # If there is'nt any consecutif numbers. Append the element to the final list 
      else: 
       final.append(" ".join(map(str, bb[0]))) 


# create the output file 
with open("output_file", 'a') as f: 
    for k in final: 
     f.write(k + '\n') 

這段代碼輸出包含一個文件:

Value 1,Value 3 
Value 4,Value 5 
Value 6,Value 8 
Value 9,Value 11 
Value 12,Value 13 

測試此代碼,並留下您的反饋,如果您有任何或可能,請報告錯誤,如果你發現任何他們。

編輯:

根據上次編輯。

如果輸入的文件是:

What Test 
Makes No Sense 
is This 

My name 
Is Sample 123 

Your Name 
is ABC 2134 

What is you 
technical question don't know 
name? 

訣竅,就是這麼簡單。你可以做這樣的事情,只有groupby利用itertools模塊:

from itertools import groupby 

with open("my_input_file", 'r') as f: 
    data = f.read().splitlines() 

final = [list(v) for _, v in groupby(data, lambda x: x != '')] 

with open("ouput_file", 'a') as f: 
    for k in final: 
     if k != ['']: 
      f.write(k[0] + ',' + k[-1] + '\n') 

而且,你的輸出文件將是:

What Test ,is This 
My name ,Is Sample 123 
Your Name ,is ABC 2134 
What is you ,name? 
+0

感謝您的幫助,我們不知道我們沒有(0),l [-1])for l in [b.splitlines()for b in f_in.read一個字符串,但實際上是一個空格。我已經更新了這個問題,以更好地反映產出。我得到以下錯誤「ValueError:無效文字爲int()以10爲基礎:'STRINGVALUE'」 – misguided

+0

dd = map(lambda x:[x [0],int(x [1])],elm)似乎成爲問題,這可能是因爲我解釋這個問題的方式。 – misguided

+0

你正在編輯你的問題! :/保持冷靜並添加你的文件的方式。第二個值是一個字符串,一個int還是什麼? –

0

爲了旋轉:你知道所有線路末

with open("PATH TO FILE.txt", r) as file: 
    input = file.read() 
    input.replace("\n", "") 

對於只有空格,或以確定它們的線條有一個新的生產線。到目前爲止:

with open("PATH TO FILE.txt", r) as file: 
     input = file.read() 
     if not line.strip(): 
      input.replace("\n", "") 

,你可以保持一個計數或做一個while循環,所以你計數,直到你打,只有空格行和當計數把每行一個列表或東西, 如果計算3抓住第一和第三,否則抓住兩者。請記住重置計數