我喜欢Python,我也是Python新手。在社区(比如anttihaapala)的帮助下,我在某种程度上可以继续工作。但最后我被卡住了。请帮忙。在进入大数据POC之前,我还有两个任务要做。(计划在100多万条文本文件记录中使用此代码)
•在列(C#3)中搜索一个关键字,并在该关键字前后保留两个单词。你知道吗
•将打印输出转移到文件。你知道吗
•在这里,我不想为了引用完整性的目的而接触C#1、C#2。你知道吗
非常感谢你的帮助。你知道吗
我的输入文件:
C #1 C # 2 C# 3 (these are headings of columns, I used just for clarity)
12088|CITA|{Hello very nice lists, better to keep those
12089|CITA|This is great theme for lists keep it
所需的输出文件:(仅更改第3列或最后一列)
12088|CITA|very nice lists, better to
12089|CITA|theme for lists keep it
我当前使用的代码:
s = """12088|CITA|{Hello very nice lists, better to keep those
12089|CITA|This is great theme for lists keep it """
for line in s.splitlines():
if not line.strip():
continue
fields = line.split(None, 2)
joined = '|'.join(fields)
print(joined)
顺便说一句,如果我使用关键字搜索,我正在寻找我的第一和第二列。我的挑战是保持第一和第二栏不变。只搜索第3列,在关键字后/前保留2个单词
首先我要警告你,对一百万条记录使用这个代码是危险的。您正在处理正则表达式,只要表达式是正则的,这个方法就很好。否则,您可能会创建成吨的案例来提取您想要的数据,而不提取您不想要的数据。你知道吗
对于100万个案例,你需要熊猫,因为循环太慢了。你知道吗
这给了
关于您如何准确地执行关键字搜索还有一些问题。您的示例中已经包含了一个障碍:如何处理诸如逗号之类的字符?另外,不清楚如何处理不包含关键字的行。另外,如果关键字前后没有两个单词,该怎么办?我猜你自己有点不确定的确切要求,并没有考虑所有的边缘案件。你知道吗
尽管如此,我还是对这些问题做了一些“盲目的决定”,下面是一个简单的示例实现,它假设关键字匹配规则非常简单。我已经创建了函数
findword()
,您可以根据需要调整它。所以,也许这个例子可以帮助你找到自己的需求。你知道吗测试:
PS:我希望我的指数适合切片。不过,你应该检查一下。你知道吗
相关问题 更多 >
编程相关推荐