【问题标题】:Python converting a space delimited file to csv issue with dates?Python将空格分隔文件转换为带有日期的csv问题?
【发布时间】:2020-06-24 10:20:30
【问题描述】:

我需要将空格分隔的文件转换为逗号分隔的文件。然而,问题是输入字符串包含包含空格的日期时间。实际输入要长得多,出于问题的目的,我将其缩短了。大小是可变长度的,所以我不能硬编码空格的位置。

输入:

StoreID= 0  JobID= 5429  Started= 19/02/2020 12:32:06  Stopped 19/02/2020 13:28:15  ObjKey= 0ce0e90a

目标输出:

StoreID=,0,JobID=,5429,Started=,19/02/2020 12:32:06,Stopped,19/02/2020 13:28:15,ObjKey=,0ce0e90a

目前我只是使用行拆分和替换,但这会在时间和日期之间添加逗号。我正在考虑使用正则表达式尝试匹配数字逗号,然后在第二遍中匹配数字,但无法使其正常工作。

    line = "StoreID= 0  JobID= 5429  Started= 19/02/2020 12:32:06  Stopped 19/02/2020 13:28:15  ObjKey= 0ce0e90a"
    tmp = " ".join(line.split()).replace(' ', ',')
    output = "StoreID=,0,JobID=,5429,Started=,19/02/2020,12:32:06,Stopped,19/02/2020,13:28:15,ObjKey=,0ce0e90a"

更新

这是一个完整输入行的副本,并使用@Serge Ballesta 的答案。正如您在行尾看到的那样,有一些以空格分隔的字段被遗漏了。

line = "StoreID= 0  JobID= 5428  Started= 19/02/2020 12:32:06  Stopped 19/02/2020 13:28:14  ObjKey= 0ce0e90a_5e4d2ac5_1178_28cf  ObjectSize= 125315962691  Written= 125315962691  Read= 0  Cloned= 0  DedupRatio10x= 1501  CompRatio10x= 44  WriteFrag10x= 18  MatchCand10x= 22 MatchHits10x= 19  RunTimeMBps= 3369.000 37.197  ClientTimeMBps= 1091.337 114.828  ClientMessageTimeMBps= 218.814 572.705  ServerTimeMBps= 122.837 1020.179  Bottleneck20%= [ISV_WAIT]  Bottleneck30%= [ISV_WAIT]  Bottleneck40%= [ISV_WAIT]  Clone%= 0.00 0.00  Match%= 14.52 10.70  Store%= 4.95 0.19 Flush%= 0.58 0.36 SeekRead 0"
tmp = " ".join(line.split()).replace(' ', ',')
output = "StoreID= 0,JobID= 5428,Started= 19/02/2020 12:32:06,Stopped 19/02/2020 13:28:14,ObjKey= 0ce0e90a_5e4d2ac5_1178_28cf,ObjectSize= 125315962691,Written= 125315962691,Read= 0,Cloned= 0,DedupRatio10x= 1501,CompRatio10x= 44,WriteFrag10x= 18,MatchCand10x= 22 MatchHits10x= 19,RunTimeMBps= 3369.000 37.197,ClientTimeMBps= 1091.337 114.828,ClientMessageTimeMBps= 218.814 572.705,ServerTimeMBps= 122.837 1020.179,Bottleneck20%= [ISV_WAIT],Bottleneck30%= [ISV_WAIT],Bottleneck40%= [ISV_WAIT],Clone%= 0.00 0.00,Match%= 14.52 10.70,Store%= 4.95 0.19 Flush%= 0.58 0.36 SeekRead 0"

【问题讨论】:

  • 它是空格分隔的还是制表符分隔的文件?键列表(StoreID、JobID 等)是不变的还是变化的?
  • @buran 它是空格分隔的,我相信键是不变的。这将运行的实际文件是 400MB,所以我已经对前 10 个文件进行了测试,它们看起来是不变的。
  • 您唯一的示例行在主要项目之间有 两个 空格,并且只有一个“内部”整个字符串。这对整个文档是否正确?
  • @usr2564301 我的错,是的,文件实际上在值之间有两个空格,但是有些有一个。我无法更改输入文件。

标签: python regex file csv


【解决方案1】:

如果您的输入行正确,则 分隔符 是两个空格的序列,因此您可以这样做:

output = ','.join(line.split('  '))

如果它是 至少 2 个空白字符的序列,您可以使用正则表达式:

output = ','.join(re.split(r'\s\s+', line))

对于最后一行,分隔符是一系列空格,后跟一个大写字母。正则表达式可以完成这项工作:

line = "StoreID= 0  JobID= 5428  Started= 19/02/2020 12:32:06  Stopped 19/02/2020 13:28:14  ObjKey= 0ce0e90a_5e4d2ac5_1178_28cf  ObjectSize= 125315962691  Written= 125315962691  Read= 0  Cloned= 0  DedupRatio10x= 1501  CompRatio10x= 44  WriteFrag10x= 18  MatchCand10x= 22 MatchHits10x= 19  RunTimeMBps= 3369.000 37.197  ClientTimeMBps= 1091.337 114.828  ClientMessageTimeMBps= 218.814 572.705  ServerTimeMBps= 122.837 1020.179  Bottleneck20%= [ISV_WAIT]  Bottleneck30%= [ISV_WAIT]  Bottleneck40%= [ISV_WAIT]  Clone%= 0.00 0.00  Match%= 14.52 10.70  Store%= 4.95 0.19 Flush%= 0.58 0.36 SeekRead 0"
output = re.sub(r'\s+([A-Z])', r',\1', line)
print(output)

给予:

StoreID= 0,JobID= 5428,Started= 19/02/2020 12:32:06,Stopped 19/02/2020 13:28:14,ObjKey= 0ce0e90a_5e4d2ac5_1178_28cf,ObjectSize= 125315962691,Written= 125315962691,Read= 0,Cloned= 0,DedupRatio10x= 1501,CompRatio10x= 44,WriteFrag10x= 18,MatchCand10x= 22,MatchHits10x= 19,RunTimeMBps= 3369.000 37.197,ClientTimeMBps= 1091.337 114.828,ClientMessageTimeMBps= 218.814 572.705,ServerTimeMBps= 122.837 1020.179,Bottleneck20%= [ISV_WAIT],Bottleneck30%= [ISV_WAIT],Bottleneck40%= [ISV_WAIT],Clone%= 0.00 0.00,Match%= 14.52 10.70,Store%= 4.95 0.19,Flush%= 0.58 0.36,SeekRead 0

【讨论】:

  • 我已经更新了这个问题,但是这个文件确实包含一些空格。
【解决方案2】:

您可以将“dd/mm/yyyy”替换为“dd/mm/yyyy”

output = re.sub(r'([\d]+\/[\d]+\/[\d]{4})\,',r'\g<1> ',output)

对于 Match%=,15.43,11.62,Store%=,5.03,0.22:

output = 'Match%=,15.43,11.62,Store%=,5.03,0.22'
output = re.sub(r'([\d]+\.[\d]{2})\,([\d]+\.[\d]{2})',r'\g<1> \g<2>',output)

【讨论】:

  • 所以这确实可以删除日期,但是有些字段的两个值也用逗号分隔。这个例子有没有办法做到这一点:Match%=,15.43,11.62,Store%=,5.03,0.22
猜你喜欢
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 2021-06-02
  • 1970-01-01
  • 2021-05-22
  • 2020-09-01
  • 2015-11-30
  • 2013-11-14
相关资源
最近更新 更多