2017-06-05 84 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抓住第一和第三,否则抓住两者。请记住重置计数