Question: In this problem, you will write a function named process _ line ( ) that accepts a string representing a sequence of distinct values. The
In this problem, you will write a function named processline that accepts a string representing a sequence of distinct
values. The function will split the string into individual strings, coerce each string into the desired data type, and will then
return the results as a list.
The function should accept three parameters named line, schema, and sep.
line should contain a string that is to be split into separate values.
schema should be a list of data types indicating the desired types for each of the values in line.
sep should be a string indicating the character used to separate the different values in line.
As an example, consider the following string:
testline Blah,Hello World,
We wish to separate this into smaller strings by splitting it at commas. We will then coerce each resulting token into the
appropriate data type according to the following schema:
testschema int int, str str float
The function call processlinetestline, testschema, should return the following list:
'Blah', 'Hello World',
Write a function named processline that accepts three parameters line, schema, and sep as described above.
The function should perform the following steps:
Use the split method to split the string line on the character provided by sep. Store the resulting list in a
variable named tokens.
Create an empty list named result.
Simultaneously loop over the elements of the lists tokens and schema which are intended to have the same
number of elements Each time the loop executes, perform the following steps:
a Store the current element of tokens in a variable named t
b Store the current element of schema in a variable named dt
c Note that dt will contains a data type, while t will hold a string. Coerce t into a value with the desired
data type using dtt Append the converted value into the list result.
Return result.
We will apply this function in the next example, but we will first test it to make sure that it is working correctly.
In a new code cell, create the following objects:
testschema int int, str str float
testline Blah,Hello World,
Use the processline function to split the string testline at the commas, coercing the individual tokens into
the data types stored in testschema. Print the result.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
