使用sh将面向列的文件转换为CSV输出

2024-09-30 10:29:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个来自MapReduce输出的文件,下面的格式需要使用shell脚本转换为CSV

25-MAY-15
04:20
Client
0000000010
127.0.0.1
PAY
ISO20022
PAIN000
100
1
CUST
API
ABF07
ABC03_LIFE.xml
AFF07/LIFE
100000
Standard Life 

================================================
==================================================

AFF07-B000001

 2000

ABC Corp
..

BE900000075000027


AFF07-B000002

 2000

XYZ corp
..

BE900000075000027



AFF07-B000003

 2000

3MM corp
..

BE900000075000027

我需要像下面的CSV格式输出,我想重复文件中的一些值,并添加如下格式的事务ID

25-MAY-15,04:20,Client,0000000010,127.0.0.1,PAY,ISO2002,PAIN000,100,1,CUST,API,ABF07,ABC03_LIFE.xml,AFF07/LIFE,100000,Standard Life, 25-MAY-15,04:20,Client,0000000010,127.0.0.1,PAY,ISO2002,PAIN000,100,1,CUST,API,AFF07-B000001, 2000,ABC Corp,..,BE900000075000027

25-MAY-15,04:20,Client,0000000010,127.0.0.1,PAY,ISO2002,PAIN000,100,1,CUST,API,ABF07,ABC03_LIFE.xml,AFF07/LIFE,100000,Standard Life, 25-MAY-15,04:20,Client,0000000010,127.0.0.1,PAY,ISO2002,PAIN000,100,1,CUST,API,AFF07-B000002,2000,XYZ Corp,..,BE900000075000027

事务ID是AFF07-b00001、AFF07-b00002、AFF07-b00003,它们有不同的值,我在事务ID开始的地方放了一个标记行。在取消标记之前,值应该是重复的,并且需要将transaction ID列与重复值一起添加到上述格式中给出的行之前

我可能需要bashshell脚本,CentOS是linux的风格

当我执行代码时,我得到如下错误

Traceback (most recent call last):
  File "abc.py", line 37, in <module>
    main()
  File "abc.py", line 36, in main
    createTxns(fh)
  File "abc.py", line 7, in createTxns
    first17.append( fh.readLine().rstrip() )
AttributeError: 'file' object has no attribute 'readLine'

有人能帮我吗


Tags: clientapiid格式xmlpaymaylife
2条回答

这是输入文件和输出格式的正确描述吗?你知道吗

输入文件包括:

  • 17行,后面是
  • 每组10行-每组持有一个事务id

每个输出行包括:

  • 29个公共字段,然后是
  • 从上述10个行组中的每一个派生出5个字段

所以我们把它翻译成一些Python:

def createTxns(fh):
  """fh is the file handle of the input file"""

  # 1. Read 17 lines from fh
  first17 = []
  for i in range(17):
    first17.append( fh.readLine().rstrip() )


  # 2. Form the common fields.

  commonFields = first17 + first17[0:12]

  # 3. Process the rest of the file in groups of ten lines.

  while True:
      # read 10 lines
      group = []
      for i in range(10):
        x = fh.readline()
        if x == '':
          break
        group.append( x.rstrip() )

      if len(group) <> 10:
        break                   # we've reached the end of the file

      fields = commonFields + [ group[2], group[4], group[6], group[7[, group[9] ]

      row = ",".join(fields)

      print row

def main():
  with open("input-file", "r") as fh:
    createTxns(fh)

main()

此代码显示如何:

  • 打开文件句柄
  • 从文件句柄读取行
  • 去掉结尾的换行符
  • 读取文件时检查输入是否结束
  • 将列表串联在一起
  • 把线连在一起

如果您选择python路线,我建议您阅读Input and Output。你知道吗

你只需要把问题分解并尝试一下。对于前17行,在字符串中使用f.readline()和concat。然后使用replace方法获取csv中所需字符串的开头。你知道吗

str.replace("\n", ",")

然后使用split方法将它们分解到列表中。你知道吗

str.split("\n")

然后在循环中写出文件。使用计数器使你的生活更轻松。首先写出标题字符串

25-MAY-15,04:20,Client,0000000010,127.0.0.1,PAY,ISO2002,PAIN000,100,1,CUST,API,ABF07,ABC03_LIFE.xml,AFF07/LIFE,100000,Standard Life, 25-MAY-15,04:20,Client,0000000010,127.0.0.1,PAY,ISO2002,PAIN000,100,1,CUST,API

然后在列表中用“,”写下该项。你知道吗

,AFF07-B000001, 2000,ABC Corp,..,BE900000075000027

在计数为5时,再次写入带有标题的“\n”,不要忘记重置计数器,以便可以重新开始。你知道吗

\n25-MAY-15,04:20,Client,0000000010,127.0.0.1,PAY,ISO2002,PAIN000,100,1,CUST,API,ABF07,ABC03_LIFE.xml,AFF07/LIFE,100000,Standard Life, 25-MAY-15,04:20,Client,0000000010,127.0.0.1,PAY,ISO2002,PAIN000,100,1,CUST,API

试一试,如果你需要更多的助手,请告诉我们。我假设你有一些脚本背景:)祝你好运!!你知道吗

相关问题 更多 >

    热门问题