Analyzing Data: 1) Importing Lists from a txt file with Python, 2) counting list items, list elements, 3) selecting certain item from list


The lines below are from a beginners test I did with Python. Python is a programming language that is popular with the financial community, and important for certain data analysts.

*** start ****
# import only needed for first
import csv
# print each line of a text file, separated by commas
with open("C:\\Documents\\cars.txt", newline='') as cars:
results = list(csv.reader(cars))

print("List of items in cars.txt: ", results)
print("This is the number of list elements:", len(results))
print("This is the [1] item from the list:", results[1])


cars.close()

*** end ***

The code above will output this:

*** start ***
List of items in cars.txt: [['car1 = "Ford";'], ['car2 = "Volvo";'], ['car3 = "Mercedes";'], ['car4 = "Kia";'], ['car5 = "Range Rover";']]
This is the number of list elements: 5
This is the [1] item from the list: ['car2 = "Volvo";']
*** end ***

This is what is actually in cars.txt
*** txt start ***
car1 = "Ford";
car2 = "Volvo";
car3 = "Mercedes";
car4 = "Kia";
car5 = "Range Rover";
*** txt end ***

Be the first to comment on "Analyzing Data: 1) Importing Lists from a txt file with Python, 2) counting list items, list elements, 3) selecting certain item from list"

Leave a comment

Your email address will not be published.


*