Skip to content Skip to sidebar Skip to footer

Getting First Column Csv File Python

Trying to get first column (names) into a list to show the user and is then updated with the changes to CSV file. Using this code: import csv list = [] exRtFile = open ('exchangeRa

Solution 1:

You are appending the empty column list every other row; test for the column before appending:

col = []
exRtFile =open ('exchangeRate.csv','r')
exchReader = csv.reader(exRtFile)
forcolumnin exchReader:
    if column:
        col.append(column[0])

Do not use an endless loop (while True:) in there.

Or using a list comprehension and the file as a context manager to close it automatically:

withopen('exchangeRate.csv', newline='') as exRtFile:
    exchReader = csv.reader(exRtFile)
    col = [c[0] for c in exchReader if c]

Note that I added the newline='' argument to the open() call; this is recommended for CSV files because they can use a mix of line endings between values and rows, which the csv.reader() will manage for you if you let it.

Post a Comment for "Getting First Column Csv File Python"