用Python打印第五个单词
我试图从一个SQL文件中找出字符串后面的第五个单词 string=插入到记录中 SQL文件: 代码 运行此代码时,我得到以下输出:用Python打印第五个单词,python,Python,我试图从一个SQL文件中找出字符串后面的第五个单词 string=插入到记录中 SQL文件: 代码 运行此代码时,我得到以下输出: seems,seems,3,10, 我希望得到: seems,picked,3,10, 有人能告诉我我的代码出了什么问题吗 提前谢谢大家,, Dan这个正则表达式有效 import re with open('dump.sql', 'r') as f: s = f.read() matches = re.findall('\((\'[^\']*\',
seems,seems,3,10,
我希望得到:
seems,picked,3,10,
有人能告诉我我的代码出了什么问题吗
提前谢谢大家,,
Dan这个正则表达式有效
import re
with open('dump.sql', 'r') as f:
s = f.read()
matches = re.findall('\((\'[^\']*\',\s*){3}', s)
for match in matches:
print(match)
您可以计算字符串的起始区域,然后打印后面的所有内容。 例如:
stringA = "Hello World My name is Robot Tim, and who are you?"
# Find the character position to start.
n = stringA.index(stringA.split()[5]);
#print(n);
stringB = stringA[n:]
print(stringB);
试试这个
我不知道它是否适用于所有sql文件
string = 'INSERT INTO record VALUES'
with open('dump.sql', 'r') as dump:
for line in dump:
if string in line:
tail = line.split(string)[1]
for group in tail.split("),("):
group = group.replace("\'", "").replace("(", "").replace(")\n", "")
print(group.split(",")[2].strip())
输出:
在列表中获取结果
以防您希望在列表中获得结果
string = 'INSERT INTO record VALUES'
l = []
with open('dump.sql', 'r') as dump:
for line in dump:
if string in line:
tail = line.split(string)[1]
for group in tail.split("),("):
group = group.replace("\'", "").replace("(", "").replace(")\n", "")
print(group.split(",")[2].strip())
l.append(group.split(",")[2].strip())
在最后一行中,从尾部检索值,它应该来自组,考虑更改,打印WordStILL(3)来打印Word组(3)不是一个选项,因为我将使列表索引超出范围。是的,对于这个例子来说,效果很好,但这只是我SQL文件的一小部分。如果我们对整个文件进行扩展,这有点不同-这只是一个简单的示例,说明我的SQL文件是如何的,我不会得到任何东西。我们应该坚持在字符串后面加第五个单词,否则就不行了。我只能就你要的部分给你一个答案。如果你有许多问题中没有描述的其他隐藏问题,你没有提出完整的问题。那我帮不了你…谢谢你,这正是我需要的。
string = 'INSERT INTO record VALUES'
with open('dump.sql', 'r') as dump:
for line in dump:
if string in line:
tail = line.split(string)[1]
for group in tail.split("),("):
group = group.replace("\'", "").replace("(", "").replace(")\n", "")
print(group.split(",")[2].strip())
seems
picked
3
10
>>>
string = 'INSERT INTO record VALUES'
l = []
with open('dump.sql', 'r') as dump:
for line in dump:
if string in line:
tail = line.split(string)[1]
for group in tail.split("),("):
group = group.replace("\'", "").replace("(", "").replace(")\n", "")
print(group.split(",")[2].strip())
l.append(group.split(",")[2].strip())