如何使用 python3.6 从文件中提取单词部分?
How I can extract the portion of words from the file using python3.6?
我想从文本文件中提取特定的单词。
这是示例文本文件:
https://drive.google.com/file/d/0BzQ6rtO2VN95d3NrTjktMExfNkU/view?usp=sharing
请评论。
我正在尝试将字符串提取为:
"Name": "the name infront of it"
"Link": "Link infront of it"
从输入文件来看,我希望得到这样的输出:
"Name":"JTLnet"
"Link":"http://jtlnet.com"
"Name":"Apache 1.3"
"Link":"http://httpd.apache.org/docs/1.3"
"Name":"Apache"
"Link":"http://httpd.apache.org/"
.
.
.
"Name":"directNIC"
"Link":"http://directnic.com"
如果这些词在文件中的任何位置,它应该被提取到另一个文件。
请让我知道如何实现这种提取?请将文件视为大文件的一小部分。
另外,它是文本文件而不是 json.
请帮助我。
由于文本文件格式不正确,您唯一的选择是正则表达式。以下代码段适用于给定的示例文件。
请记住,这需要您将整个文件加载到内存中
import re, json
f = open(r'filepath')
textCorpus = f.read()
f.close()
# replace empty strings to non-empty, match regex easily
textCorpus = textCorpus.replace('""', '" "')
lstMatches = re.findall(r'"Name".+?"Link":".+?"', textCorpus)
with open(r'new_file.txt', 'ab+) as wf:
for eachMatch in lstMatches:
convJson = "{" + eachMatch + "}"
json_data = json.loads(convJson)
wf.write(json_data["Name"] + "\n")
wf.write(json_data["Link"] + "\n")
使用 re.findall()
和 str.split()
函数的简短解决方案:
import re
with open('test.txt', 'r') as fh:
p = re.compile(r'(?:"Categories":[^,]+,)("Name":"[^"]+"),(?:[^,]+,)("Link":"[^"]+")')
result = [pair for l in re.findall(p, fh.read()) for pair in l]
print('\n'.join(result))
输出(片段):
"Name":"JTLnet"
"Link":"http://jtlnet.com"
"Name":"Apache 1.3"
"Link":"http://httpd.apache.org/docs/1.3"
"Name":"Apache"
"Link":"http://httpd.apache.org/"
"Name":"PHP"
....
您的文件格式错误 json,带有多余的双引号。但是json模块加载不出来就够了。您剩下较低级别的正则表达式解析。
假设:
"Name"
或"Link"
之后有趣的部分是:
- 用冒号与标识符分隔 (
:
)
- 用双引号括起来 (
"
),没有包含双引号
- 文件按行组织
- 名称和 Link 字段始终在一行中(字段中没有新行)
您可以逐行处理您的文件,每行一个简单的 re.finditer
:
rx = re.compile(r'(("Name":".*?")|("Link":".*?"))')
with open(inputfile) as fd:
for line in fd:
l = rx.finditer(line)
for elt in l:
print(elt.group(0))
如果要将数据输出到另一个文件,只需在上面的代码段之前用 open(outputfile, "w") as fdout:
打开它,然后将打印行替换为:
fdout.write(elt.group(0) + "\n")
我想从文本文件中提取特定的单词。
这是示例文本文件:
https://drive.google.com/file/d/0BzQ6rtO2VN95d3NrTjktMExfNkU/view?usp=sharing
请评论。
我正在尝试将字符串提取为:
"Name": "the name infront of it"
"Link": "Link infront of it"
从输入文件来看,我希望得到这样的输出:
"Name":"JTLnet"
"Link":"http://jtlnet.com"
"Name":"Apache 1.3"
"Link":"http://httpd.apache.org/docs/1.3"
"Name":"Apache"
"Link":"http://httpd.apache.org/"
.
.
.
"Name":"directNIC"
"Link":"http://directnic.com"
如果这些词在文件中的任何位置,它应该被提取到另一个文件。
请让我知道如何实现这种提取?请将文件视为大文件的一小部分。
另外,它是文本文件而不是 json.
请帮助我。
由于文本文件格式不正确,您唯一的选择是正则表达式。以下代码段适用于给定的示例文件。
请记住,这需要您将整个文件加载到内存中
import re, json
f = open(r'filepath')
textCorpus = f.read()
f.close()
# replace empty strings to non-empty, match regex easily
textCorpus = textCorpus.replace('""', '" "')
lstMatches = re.findall(r'"Name".+?"Link":".+?"', textCorpus)
with open(r'new_file.txt', 'ab+) as wf:
for eachMatch in lstMatches:
convJson = "{" + eachMatch + "}"
json_data = json.loads(convJson)
wf.write(json_data["Name"] + "\n")
wf.write(json_data["Link"] + "\n")
使用 re.findall()
和 str.split()
函数的简短解决方案:
import re
with open('test.txt', 'r') as fh:
p = re.compile(r'(?:"Categories":[^,]+,)("Name":"[^"]+"),(?:[^,]+,)("Link":"[^"]+")')
result = [pair for l in re.findall(p, fh.read()) for pair in l]
print('\n'.join(result))
输出(片段):
"Name":"JTLnet"
"Link":"http://jtlnet.com"
"Name":"Apache 1.3"
"Link":"http://httpd.apache.org/docs/1.3"
"Name":"Apache"
"Link":"http://httpd.apache.org/"
"Name":"PHP"
....
您的文件格式错误 json,带有多余的双引号。但是json模块加载不出来就够了。您剩下较低级别的正则表达式解析。
假设:
"Name"
或"Link"
之后有趣的部分是:- 用冒号与标识符分隔 (
:
) - 用双引号括起来 (
"
),没有包含双引号
- 用冒号与标识符分隔 (
- 文件按行组织
- 名称和 Link 字段始终在一行中(字段中没有新行)
您可以逐行处理您的文件,每行一个简单的 re.finditer
:
rx = re.compile(r'(("Name":".*?")|("Link":".*?"))')
with open(inputfile) as fd:
for line in fd:
l = rx.finditer(line)
for elt in l:
print(elt.group(0))
如果要将数据输出到另一个文件,只需在上面的代码段之前用 open(outputfile, "w") as fdout:
打开它,然后将打印行替换为:
fdout.write(elt.group(0) + "\n")