I wrote a basic web scraper that returns values into nested lists like the one below:
results = [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']]
But I want to add 2 custom values when they get pushed into the lists to look like something below:
results = [['customvalue1', 'customvalue2', 'a', 'b', 'c'], ['customvalue1', 'customvalue2', 'a', 'b', 'c'], ['customvalue1', 'customvalue2', 'a', 'b', 'c']]
Specifically, I want 'customvalue1' to be the current date in dd/mm/yyyy format, and 'customvalue2' to be a string that I define.
I tried to create the custom values within the for loop right before the append method, but haven' had luck so far.
CodePudding user response:
One of the ways to achieve it in the case of python.
import datetime
# Define customvalue2
customvalue2 = "mystring"
results = []
# Get current date
today = datetime.datetime.now()
# Loop over the data you want to add to the results list
for data in [['a', 'b', 'c'], ['a', 'b', 'c'], ['a', 'b', 'c']]:
# Format current date as dd/mm/yyyy
customvalue1 = today.strftime("%d/%m/%Y")
# Create a new list with the custom values and the data
entry = [customvalue1, customvalue2] data
# Append the entry to the results list
results.append(entry)