[英]How to clean tab-delimited file with pages
我有一个制表符分隔的文件,看起来与此类似:
01/17/2017 Display Warehouse Stocks of Material 1
Material Plnt SLoc SL BUn Unrestricted Transit/Transf. In Quality Insp. Restricted-Use Blocked Returns
1111 5252 7000 EA 20.000 0.000 0.000 0.000 0.000 0.000
2222 4646 7000 EA 30.000 0.000 0.000 0.000 0.000 0.000
1/17/2017 Display Warehouse Stocks of Material 2
Material Plnt SLoc SL BUn Unrestricted Transit/Transf. In Quality Insp. Restricted-Use Blocked Returns
3333 6060 6000 EA 20.000 0.000 0.000 0.000 0.000 0.000
4444 5252 6000 EA 10.000 0.000 0.000 0.000 0.000 0.000
如您所见,此文件在经过一定数量的数据后会有页面,因此我需要清除数据以具有类似以下内容:
Material Plnt SLoc SL BUn Unrestricted Transit/Transf. In Quality Insp. Restricted-Use Blocked Returns
1111 5252 7000 EA 20.000 0.000 0.000 0.000 0.000 0.000
2222 4646 7000 EA 30.000 0.000 0.000 0.000 0.000 0.000
3333 6060 6000 EA 20.000 0.000 0.000 0.000 0.000 0.000
4444 5252 6000 EA 10.000 0.000 0.000 0.000 0.000 0.000
我用python编写了一个脚本,部分清除了文件。
def _cleanup(txtfile):
file = open(txtfile, 'r+')
lines = file.readlines()
file.seek(0)
for line in lines:
if line.startswith((' ', '\tMaterial')) == False and line.startswith((' ', '\t')):
file.write(line)
file.truncate()
file.close()
return True
def _main():
sample = 'sample.txt'
print('Done' if _cleanup(sample) else 'Something is wrong')
_main()
脚本给了我这个:
1111 5252 7000 EA 20.000 0.000 0.000 0.000 0.000 0.000
2222 4646 7000 EA 30.000 0.000 0.000 0.000 0.000 0.000
3333 6060 6000 EA 20.000 0.000 0.000 0.000 0.000 0.000
4444 5252 6000 EA 10.000 0.000 0.000 0.000 0.000 0.000
如您所见,脚本删除了表头,这是我需要的。
我知道这可能不是这样做的最佳选择,如果您知道另一种方法是受欢迎的!
这样做的主要目的是将数据放在数据库表中,可能是我使用的方式不正确。
使用正则表达式...,并使用键“ ^ [0-9] {4}。*”-不带引号。
^-行首
[0-9]-任何数字
{4}-重复4次
。*-获取整行
那应该做。
以下是有关如何使用正则表达式从文件中读取的链接。 https://codereview.stackexchange.com/questions/40423/reading-from-text-file-with-regexmatch
看起来,如果该行是非空的,并且其第一项是整数,则您需要它; 否则,不是。 然后,此代码应执行您想要的操作。
>>> headerFound = False
>>> with open('sample.txt') as sample:
... for line in sample.readlines():
... line = line.strip()
... if line:
... items = line.split()
... if items[0]=='Material' and not headerFound:
... print (items)
... headerFound = True
... continue
... try:
... first = int(items[0])
... print (items)
... except:
... pass
...
['Material', 'Plnt', 'SLoc', 'SL', 'BUn', 'Unrestricted', 'Transit/Transf.', 'In', 'Quality', 'Insp.', 'Restricted-Use', 'Blocked', 'Returns']
['1111', '5252', '7000', 'EA', '20.000', '0.000', '0.000', '0.000', '0.000', '0.000']
['2222', '4646', '7000', 'EA', '30.000', '0.000', '0.000', '0.000', '0.000', '0.000']
['3333', '6060', '6000', 'EA', '20.000', '0.000', '0.000', '0.000', '0.000', '0.000']
['4444', '5252', '6000', 'EA', '10.000', '0.000', '0.000', '0.000', '0.000', '0.000']
当然,可以使用更高级的版本。 您可以检查斜线的外观以消除线条。 您可以检查字母字符。
IIUC,您缺少标头。 在这种情况下,只需将标题行的第一次出现添加到输出文件中
def _cleanup(txtfile):
file = open(txtfile, 'r+')
lines = file.readlines()
file.seek(0)
AddHeader=False
for line in lines:
if line.startswith(('\tMaterial'))==True and AddHeader==False:
file.write(line)
AddHeader=True
if line.startswith((' ', '\tMaterial')) == False and line.startswith((' ', '\t')):
file.write(line)
file.truncate()
file.close()
return True
def _main():
sample = 'sample.txt'
print('Done' if _cleanup(sample) else 'Something is wrong')
_main()
产量
Material Plnt SLoc SL BUn Unrestricted Transit/Transf. In Quality Insp. Restricted-Use Blocked Returns
1111 5252 7000 EA 20.000 0.000 0.000 0.000 0.000 0.000
2222 4646 7000 EA 30.000 0.000 0.000 0.000 0.000 0.000
3333 6060 6000 EA 20.000 0.000 0.000 0.000 0.000 0.000
4444 5252 6000 EA 10.000 0.000 0.000 0.000 0.000 0.000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.