I have a table with 3 string columns. 'author', 'title' and 'mytext'
I need to find a string (for e.g. question mark ?) and replace it (for e.g. period .) across all records ('mytext' column). There are only 300 records in the table.
I am looking for an easy way to find - replace text using python.
I am using this code that is working as expected. Are there any surprises / issues expected in the future? I guess for 300 records, it should be OK.
import boto3
dynamodb = boto3.resource('dynamodb', aws_access_key_id="xxx", aws_secret_access_key="xxx", region_name='us-east-1' )
table = dynamodb.Table('table-test')
response = table.scan()
data = response['Items']
for item in data:
if item["mytext"] != item["mytext"].replace("?", "."):
updated_data = {
'author': item["author"] ,
'title': item["title"] ,
'mytext': item["mytext"].replace("?", ".")
}
table.put_item(Item=updated_data)
Here's a sample snippet that scans the entire table and replaces all occurrences of the question mark character with a period.
Note that, given your current structure (with "author" as the partition key and "title" as the sort key), you can only update the "mytext" field. This is because key parts cannot be modified.
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('table-test')
def replace_question_mark():
response = table.scan()
data = response['Items']
for item in data:
table.update_item(
Key={'author': item["author"], 'title': item["title"]},
UpdateExpression="set mytext=:m",
ExpressionAttributeValues={':m': item["mytext"].replace("?", ".")},
ReturnValues="UPDATED_NEW")
Be aware that a full scan can be very expensive (both computationally and in terms of costs). Use it at your own risk!