PyQGIS search and replace text using regex Planned maintenance scheduled April 17/18, 2019 at...
What to do with post with dry rot?
Can I add database to AWS RDS MySQL without creating new instance?
Can I throw a longsword at someone?
How to market an anarchic city as a tourism spot to people living in civilized areas?
How to rotate it perfectly?
Cold is to Refrigerator as warm is to?
Why is "Captain Marvel" translated as male in Portugal?
Why is there no army of Iron-Mans in the MCU?
Problem when applying foreach loop
How many things? AとBがふたつ
Simulating Exploding Dice
Slither Like a Snake
Estimate capacitor parameters
How to politely respond to generic emails requesting a PhD/job in my lab? Without wasting too much time
Are my PIs rude or am I just being too sensitive?
Blender game recording at the wrong time
Stars Make Stars
What do you call a plan that's an alternative plan in case your initial plan fails?
Area of a 2D convex hull
Is there a service that would inform me whenever a new direct route is scheduled from a given airport?
Stopping real property loss from eroding embankment
Using "nakedly" instead of "with nothing on"
When is phishing education going too far?
Replacing HDD with SSD; what about non-APFS/APFS?
PyQGIS search and replace text using regex
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Search and Replace text in All Fields in QGIS 3Run regexp in PyQGIS script?selecting by attribute and specific value replaceHow to do a spatial search without select() using PyQGIS?New attribute creation using ogr and regexSearch Shapefile and return ExtentsUsing regex to find an attribute with no letters in FME WorkbenchHow to select features where column matches a regex and save them in a new shapefile?Replace multiple attribute field headers in PyQGISUsing Time Manager and PyQGIS?Element Tree and RegexSearch and Replace text in All Fields in QGIS 3
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
This is based on Run regexp in PyQGIS script? and Search and Replace text in All Fields in QGIS 3
I am trying to modify the code to allow more complex replacements using regex but am unsure how to structure the re.sub function
See https://regex101.com/r/OX1W3b/2/ for one example...
1. DIMENSIONS: | ORIGIN: | Position corrected and IL (0) was changed based on RPS: 3482 -230
2. DIMENSIONS: 2 x 1350 RCP | ORIGIN: PCD13180 | Position corrected and IL (0) was changed based on RPS: 1390 -20800/1350RCP
3. DIMENSIONS: 3 x 375 RCP | ORIGIN:
4. DIMENSIONS: | ORIGIN:
5. Review attribution | DIMENSIONS: | ORIGIN:
Where there is no value after DIMENSIONS or ORIGIN it should be deleted so...
1. Position corrected and IL (0) was changed based on RPS: 3482 -230
2. DIMENSIONS: 2 x 1350 RCP | ORIGIN: PCD13180 | Position corrected and IL (0) was changed based on RPS: 1390 -20800/1350RCP
3. DIMENSIONS: 3 x 375 RCP
4.
5. Review attribution
Here's the current code - see line 10 and 33 for the regex based commands.
#Use in QGIS 3 python console
#Search and replace text in all fields of all layers
#Also allows regex use
import re
#Based on https://gis.stackexchange.com/questions/317855/search-and-replace-text-in-all-fields-in-qgis-3
#set text to search for and replace with.
searchText = "DIM"
regexText = ".*(DIMENSIONS: |)"
replaceText = ""
#run on all layers
layers = QgsProject.instance().mapLayers()
for layer_id, layer in layers.items():
i=1
print("Layer: %s" % (layer.name()))
# get data provider
dpr = layer.dataProvider()
for field in layer.fields():
fieldName=field.name()
for feature in layer.getFeatures():
inText = str(feature[fieldName])
# get field index
fieldIndex = layer.fields().indexFromName(fieldName)
#print ("Checking %s" % (inText))
if searchText in inText:
# change inText
print ("%s . REPLACED: %s in %s with %s in column: %s" % (i, searchText, inText, replaceText, fieldName))
#outText = inText.replace(searchText, replaceText)
outText = re.sub(fieldName, regexText, replaceText)
i+=1
# save changes
dpr.changeAttributeValues({feature.id(): {fieldIndex: outText}})
print ("Completed")
pyqgis regex
add a comment |
This is based on Run regexp in PyQGIS script? and Search and Replace text in All Fields in QGIS 3
I am trying to modify the code to allow more complex replacements using regex but am unsure how to structure the re.sub function
See https://regex101.com/r/OX1W3b/2/ for one example...
1. DIMENSIONS: | ORIGIN: | Position corrected and IL (0) was changed based on RPS: 3482 -230
2. DIMENSIONS: 2 x 1350 RCP | ORIGIN: PCD13180 | Position corrected and IL (0) was changed based on RPS: 1390 -20800/1350RCP
3. DIMENSIONS: 3 x 375 RCP | ORIGIN:
4. DIMENSIONS: | ORIGIN:
5. Review attribution | DIMENSIONS: | ORIGIN:
Where there is no value after DIMENSIONS or ORIGIN it should be deleted so...
1. Position corrected and IL (0) was changed based on RPS: 3482 -230
2. DIMENSIONS: 2 x 1350 RCP | ORIGIN: PCD13180 | Position corrected and IL (0) was changed based on RPS: 1390 -20800/1350RCP
3. DIMENSIONS: 3 x 375 RCP
4.
5. Review attribution
Here's the current code - see line 10 and 33 for the regex based commands.
#Use in QGIS 3 python console
#Search and replace text in all fields of all layers
#Also allows regex use
import re
#Based on https://gis.stackexchange.com/questions/317855/search-and-replace-text-in-all-fields-in-qgis-3
#set text to search for and replace with.
searchText = "DIM"
regexText = ".*(DIMENSIONS: |)"
replaceText = ""
#run on all layers
layers = QgsProject.instance().mapLayers()
for layer_id, layer in layers.items():
i=1
print("Layer: %s" % (layer.name()))
# get data provider
dpr = layer.dataProvider()
for field in layer.fields():
fieldName=field.name()
for feature in layer.getFeatures():
inText = str(feature[fieldName])
# get field index
fieldIndex = layer.fields().indexFromName(fieldName)
#print ("Checking %s" % (inText))
if searchText in inText:
# change inText
print ("%s . REPLACED: %s in %s with %s in column: %s" % (i, searchText, inText, replaceText, fieldName))
#outText = inText.replace(searchText, replaceText)
outText = re.sub(fieldName, regexText, replaceText)
i+=1
# save changes
dpr.changeAttributeValues({feature.id(): {fieldIndex: outText}})
print ("Completed")
pyqgis regex
add a comment |
This is based on Run regexp in PyQGIS script? and Search and Replace text in All Fields in QGIS 3
I am trying to modify the code to allow more complex replacements using regex but am unsure how to structure the re.sub function
See https://regex101.com/r/OX1W3b/2/ for one example...
1. DIMENSIONS: | ORIGIN: | Position corrected and IL (0) was changed based on RPS: 3482 -230
2. DIMENSIONS: 2 x 1350 RCP | ORIGIN: PCD13180 | Position corrected and IL (0) was changed based on RPS: 1390 -20800/1350RCP
3. DIMENSIONS: 3 x 375 RCP | ORIGIN:
4. DIMENSIONS: | ORIGIN:
5. Review attribution | DIMENSIONS: | ORIGIN:
Where there is no value after DIMENSIONS or ORIGIN it should be deleted so...
1. Position corrected and IL (0) was changed based on RPS: 3482 -230
2. DIMENSIONS: 2 x 1350 RCP | ORIGIN: PCD13180 | Position corrected and IL (0) was changed based on RPS: 1390 -20800/1350RCP
3. DIMENSIONS: 3 x 375 RCP
4.
5. Review attribution
Here's the current code - see line 10 and 33 for the regex based commands.
#Use in QGIS 3 python console
#Search and replace text in all fields of all layers
#Also allows regex use
import re
#Based on https://gis.stackexchange.com/questions/317855/search-and-replace-text-in-all-fields-in-qgis-3
#set text to search for and replace with.
searchText = "DIM"
regexText = ".*(DIMENSIONS: |)"
replaceText = ""
#run on all layers
layers = QgsProject.instance().mapLayers()
for layer_id, layer in layers.items():
i=1
print("Layer: %s" % (layer.name()))
# get data provider
dpr = layer.dataProvider()
for field in layer.fields():
fieldName=field.name()
for feature in layer.getFeatures():
inText = str(feature[fieldName])
# get field index
fieldIndex = layer.fields().indexFromName(fieldName)
#print ("Checking %s" % (inText))
if searchText in inText:
# change inText
print ("%s . REPLACED: %s in %s with %s in column: %s" % (i, searchText, inText, replaceText, fieldName))
#outText = inText.replace(searchText, replaceText)
outText = re.sub(fieldName, regexText, replaceText)
i+=1
# save changes
dpr.changeAttributeValues({feature.id(): {fieldIndex: outText}})
print ("Completed")
pyqgis regex
This is based on Run regexp in PyQGIS script? and Search and Replace text in All Fields in QGIS 3
I am trying to modify the code to allow more complex replacements using regex but am unsure how to structure the re.sub function
See https://regex101.com/r/OX1W3b/2/ for one example...
1. DIMENSIONS: | ORIGIN: | Position corrected and IL (0) was changed based on RPS: 3482 -230
2. DIMENSIONS: 2 x 1350 RCP | ORIGIN: PCD13180 | Position corrected and IL (0) was changed based on RPS: 1390 -20800/1350RCP
3. DIMENSIONS: 3 x 375 RCP | ORIGIN:
4. DIMENSIONS: | ORIGIN:
5. Review attribution | DIMENSIONS: | ORIGIN:
Where there is no value after DIMENSIONS or ORIGIN it should be deleted so...
1. Position corrected and IL (0) was changed based on RPS: 3482 -230
2. DIMENSIONS: 2 x 1350 RCP | ORIGIN: PCD13180 | Position corrected and IL (0) was changed based on RPS: 1390 -20800/1350RCP
3. DIMENSIONS: 3 x 375 RCP
4.
5. Review attribution
Here's the current code - see line 10 and 33 for the regex based commands.
#Use in QGIS 3 python console
#Search and replace text in all fields of all layers
#Also allows regex use
import re
#Based on https://gis.stackexchange.com/questions/317855/search-and-replace-text-in-all-fields-in-qgis-3
#set text to search for and replace with.
searchText = "DIM"
regexText = ".*(DIMENSIONS: |)"
replaceText = ""
#run on all layers
layers = QgsProject.instance().mapLayers()
for layer_id, layer in layers.items():
i=1
print("Layer: %s" % (layer.name()))
# get data provider
dpr = layer.dataProvider()
for field in layer.fields():
fieldName=field.name()
for feature in layer.getFeatures():
inText = str(feature[fieldName])
# get field index
fieldIndex = layer.fields().indexFromName(fieldName)
#print ("Checking %s" % (inText))
if searchText in inText:
# change inText
print ("%s . REPLACED: %s in %s with %s in column: %s" % (i, searchText, inText, replaceText, fieldName))
#outText = inText.replace(searchText, replaceText)
outText = re.sub(fieldName, regexText, replaceText)
i+=1
# save changes
dpr.changeAttributeValues({feature.id(): {fieldIndex: outText}})
print ("Completed")
pyqgis regex
pyqgis regex
edited 6 mins ago
GeorgeC
asked 12 mins ago
GeorgeCGeorgeC
2,90832981
2,90832981
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f318774%2fpyqgis-search-and-replace-text-using-regex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Geographic Information Systems Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f318774%2fpyqgis-search-and-replace-text-using-regex%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown