我有一个分隔符分隔的dat文件,我正在寻找操作。下面是它在开始时的样子:
'Field1'|'Field2'|'Field3'|'Field4'|'Field5'
'George'|'June'|'11'|'1943'|'Yes'
'Steve'|'May'|'17'|'1905'|''
'Jerry'|'March'|'01'|'1993'|'Yes'
第5场我将考虑成为我的"擦拭"领域。所以,让我们说一下,如果字段5的值是"是",则擦除用户选择的所有其他字段。我本质上希望让用户选择"擦除"字段,让用户选择要擦除""字段,它将导出一个新的DAT文件。
例如,用户说擦除字段=字段5 "要擦拭"字段是字段2和字段3 完成后,脚本也应该完全删除擦除字段
输出应如下所示:
'Field1'|'Field2'|'Field3'|'Field4'
'George'|''|''|'1943'
'Steve'|'May'|'17'|'1905'
'Jerry'|''|''|'1993'
我一直在使用csv模块,但我遇到了困难。任何意见都将不胜感激。
由于
答案 0 :(得分:-1)
from csv import DictReader, DictWriter, QUOTE_ALL
def solution(*, infile, outfile, wiping_field, fields_to_wipe=()):
with open(infile, 'r') as in_fh:
reader = DictReader(in_fh, delimiter='|', quotechar="'")
fieldnames = reader.fieldnames
if wiping_field not in fieldnames:
raise ValueError('wiping field must be in fieldnames')
if any(field not in fieldnames for field in fields_to_wipe):
raise ValueError('all fields to wipe must be in fieldnames')
result = []
for line in reader:
current = dict(line)
if current[wiping_field] == 'Yes':
for field in fields_to_wipe:
current[field] = ''
current.pop(wiping_field)
result.append(current)
fieldnames.remove(wiping_field)
with open(outfile, 'w') as out_fh:
writer = DictWriter(
out_fh,
delimiter='|',
fieldnames=fieldnames,
quotechar="'",
quoting=QUOTE_ALL
)
writer.writeheader()
writer.writerows(result)
输出:
In[3]: with open('test.dat', 'r') as f:
...: print(f.read())
...:
'Field1'|'Field2'|'Field3'|'Field4'|'Field5'
'George'|'June'|'11'|'1943'|'Yes'
'Steve'|'May'|'17'|'1905'|''
'Jerry'|'March'|'01'|'1993'|'Yes'
In[4]: solution(
...: infile='test.dat',
...: outfile='output.dat',
...: wiping_field='Field5',
...: fields_to_wipe=('Field2', 'Field3')
...: )
In[5]: with open('output.dat', 'r') as f:
...: print(f.read())
...:
'Field1'|'Field2'|'Field3'|'Field4'
'George'|''|''|'1943'
'Steve'|'May'|'17'|'1905'
'Jerry'|''|''|'1993'