'How can I split this data into 4 separate fields?
I have a data file that contains a date and the average price of gas for that date. I want to be able to read the data from a file, store that data in a list named "gasList", and then split that data in the list into 4 separate fields so that I can display each field separately.
Here is the date followed by the price of gas:
04-05-1993:1.068
I want to break this data down into these four separate fields:
04 05 1993 1.068?
I want to be able to test it like this:
>>>print(gasList[0])
04
>>>print(gasList[2])
1993
>>>print(gasList[3])
1.068
Solution 1:[1]
Seems like you want is list.split. In this case, the code would look like this:
data = "04-05-1993:1.068"
new_data = data.replace(":", "-").split("-")
This sets new_data to be a list of 4 different strings, which are only the significant parts.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Anonymous4045 |
