如何在没有逗号的行中添加列表的偶数元素

时间:2019-02-06 02:56:19

标签: python list

我有以下文件:

Rawdata.txt

ID Name
1 XYZ A, B, C
2 XYS D, E, F
3 YWZ G, H, I
4 XWE J, K, L

,需要将其重新格式化为以下内容:

output_data.txt

ID Name X1 Y1 Z1
1 XYZ A, B, C XYZ A  B  C
2 XYS D, E, F XYS D  E  F
3 YWZ G, H, I YWZ G  H  I
4 XWE J, K, L XWE J  K  L

到目前为止,我一直使用以下脚本:

with open('Rawdata.txt','r') as input_file:
  lines = input_file.read().splitlines()
  lines.pop(0)
  newList = [element for item in lines for element in item.split('\t')]
  #print newList

  with open('output_data.txt', 'w') as output_file:
    output_file.write("ID\tName\tX1\tY1\tZ1\n")

    for i in range(len(newList)):
       s = "".join(newList[i:(i+1)])

       output_file.write(s)
       output_file.write("\n")

现在我的文件如下所示:

ID Name X1 Y1 Z1
1 XYZ A, B, C 
2 XYS D, E, F 
3 YWZ G, H, I 
4 XWE J, K, L 

如何在output_data文件中添加其余列?

2 个答案:

答案 0 :(得分:0)

类似这样的事情。遍历文件的每一行;左分割线以获得要复制的零件;用制表符(?)替换新部分中的逗号;加入原始行和新部分;将其写入输出文件。

with open('Rawdata.txt','r') as f, open('output_data.txt', 'w') as out:
    out.write("ID\tName\tX1\tY1\tZ1\n")
    next(f)
    for line in f:
        line = line.strip()
        _, new = line.lsplit()
        new = new.replace(',', '\t')
        line = '\t'.join([line,new]) + '\n'
        out.write(line)

答案 1 :(得分:0)

我认为您的代码几乎正确。但是我只是在您的代码中做了一些小的更正,使其成为您想要的方式。

with open('Rawdata.txt','r') as input_file:
  lines = input_file.read().splitlines()
  lines.pop(0)
  newList = [element for item in lines for element in item.split('\t')]
  print(newList)
  import regex as re
  with open('output_data.txt', 'w') as output_file:
    output_file.write("ID\tName\tX1\tY1\tZ1\n")

    for i in range(len(newList)):
       s = "\t".join(newList[i:(i+1)])
       s1 = re.sub(r'\d+?',' ',s)
       s1 = re.sub(r'\,',' ',s1)
       s = s+ s1
       output_file.write(s)
       output_file.write("\n")

我使用正则表达式检测所有整数并将其替换为空格,并且在逗号中也使用了类似的大小写。希望对您有帮助