Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python替换大字符串中的字符的最佳方法?_Python - Fatal编程技术网

用Python替换大字符串中的字符的最佳方法?

用Python替换大字符串中的字符的最佳方法?,python,Python,我正在处理清理相对较大(30行)的文本块。这里有一段摘录: PID | 1 | 06225401 ^ PA0 ^ MR |患者^假 R | | | F PV1 | 1 | I | | | | | | | | | | | | | | | | | | | | | | | | | | ORC | RE | CYT-09-06645 | AP | |||||200912110333 |接口07 OBR | 1 | CYT09-06645 | 8104 | L | | | | | | | | | | |

我正在处理清理相对较大(30行)的文本块。这里有一段摘录:

PID | 1 | 06225401 ^ PA0 ^ MR |患者^假 R | | | F

PV1 | 1 | I | | | | | | | | | | | | | | | | | | | | | | | | | |

ORC | RE | CYT-09-06645 | AP | |||||200912110333 |接口07

OBR | 1 | CYT09-06645 | 8104 | L | | | | | | | | | | | 200906030000[条件]

OBX | 1 | TX | 8104 | 1 |来源 样本:[来源]| | | | | | | | | | | 200912110333 | CYT

我目前有一个脚本,可以删除非法字符或术语

    infile = open(thisFile,'r')
    m = infile.read()

    #remove junk headers
    m = m.replace("4þPATHþ", "")
    m = m.replace("10þALLþ", "")

我的目标是修改此脚本,以便在其中一个字段的末尾添加4位数字。具体来说,日期字段(“20090602”)在OBR行中。完成的脚本将能够处理任何遵循相同格式的文件。这是否可能与我当前处理文件输入的方式有关,或者我必须使用一些不同的逻辑?

您可以在此处找到有用的答案


您可能会发现这里的答案很有帮助

这里有一个提纲(未经测试)…基本上你一次只做一行

for line in infile:
    data = line.rstrip("\n").split("|")
    kind = data[0]
    # start of changes
    if kind == "OBR":
        data[7] += "0000" # check that 7 is correct!
    # end of changes
    outrecord = "|".join(data)
    outfile.write(outrecord + "\n")
以上假设您正在按线型(例如:“OBR”)和列索引(例如:7)选择修复目标。如果只有少数这样的目标,只需添加更多类似的修复语句。如果有许多目标,您可以这样指定它们:

fix_targets = {
    "OBR": [7],
    "XYZ": [1, 42],
    }
if kind in fix_targets:
    for col_index in fix_targets[kind]:
        data[col_index] += "0000"
修复代码如下所示:

fix_targets = {
    "OBR": [7],
    "XYZ": [1, 42],
    }
if kind in fix_targets:
    for col_index in fix_targets[kind]:
        data[col_index] += "0000"
在任何情况下,您都可以添加代码来检查数据[col_index]在更改之前是否确实是YYYYMMDD格式的日期

由于您没有显示示例数据,上述任何一项都不能删除不需要的标题。我想,将替换应用到每一行(并避免在替换后变为空白的情况下写入该行)可以解决问题。

这里有一个提纲(未经测试)…基本上您一次只写一行

for line in infile:
    data = line.rstrip("\n").split("|")
    kind = data[0]
    # start of changes
    if kind == "OBR":
        data[7] += "0000" # check that 7 is correct!
    # end of changes
    outrecord = "|".join(data)
    outfile.write(outrecord + "\n")
以上假设您正在按线型(例如:“OBR”)和列索引(例如:7)选择修复目标。如果只有少数这样的目标,只需添加更多类似的修复语句。如果有许多目标,您可以这样指定它们:

fix_targets = {
    "OBR": [7],
    "XYZ": [1, 42],
    }
if kind in fix_targets:
    for col_index in fix_targets[kind]:
        data[col_index] += "0000"
修复代码如下所示:

fix_targets = {
    "OBR": [7],
    "XYZ": [1, 42],
    }
if kind in fix_targets:
    for col_index in fix_targets[kind]:
        data[col_index] += "0000"
在任何情况下,您都可以添加代码来检查数据[col_index]在更改之前是否确实是YYYYMMDD格式的日期


由于您没有显示示例数据,上述所有内容都无法删除不需要的标题。我想,将替换应用于每一行(如果替换后该行仅为空白,则避免写入该行)这个问题还不完全清楚。你能提供输入和输出的示例吗?这些垃圾标题最好用“4\xfePATH\xfe”来表示,这样可以清楚地表明所谓的“非法字符”不是字符(冰岛小写字母thorn和拉丁小写字母p都不是)(乍一看,这就是它们的样子)但只是二进制垃圾。问题还不完全清楚。你能提供示例输入和输出吗?这些垃圾头可以更好地表示为“4\xfePATH\xfe”,以明确所谓的“非法字符”不是字符(既不是冰岛小写字母thorn,也不是拉丁小写字母p(乍一看就是这个样子))但这只是二进制垃圾。谢谢。我不认为这正是我要找的,但知道这一点很好。谢谢。我不认为这正是我要找的,但知道这一点很好。很好。我想可能是这样的,但我真的无法想象。谢谢你的回答。很好。outfile.write(outrecord+'\n')有一个内置的Python等价物:print>>outfile,outrecord,这更简单。@EOL-除了不常用的“print chevron”语法也被弃用。完美。我想可能是这样的,但我不能真正地将其可视化。谢谢你的回答。很好。outfile.write(outrecord+'\n'))有一个内置的Python等价物:print>>outfile,outrecord,这更简单。@EOL-除了“print chevron”语法(不经常使用)也被弃用之外。