user16375592
user16375592

Reputation:

How to read one column of a CSV file into a list of values

I have the below data in the file dm.txt

A|B|Name
VB|C|City
SDF|JK|Code

Code to read the file and index into a separate config file:

from configparser import ConfigParser
config = ConfigParser()
config.read("demo.ini")

# cutting the values of file from column 3 
keys = [ x.split("|")[2].rstrip("\n") for x in open(dm.txt).readlines()]
vals = [config["DEMFILE"][x] for x in keys]
print(vals)

It throws the error: KeyError (keys). How can I read the third column of the CSV into a list?

Upvotes: 1

Views: 258

Answers (3)

SvenTUM
SvenTUM

Reputation: 809

I'd recommend you write it more stretched out and refactor it after you made it work (e.g. using list comprehension).

Error

The open command takes the file as a string. Therefore it should be open("dm.txt", "r").

Note

This does fix the original issue, but there are possible other issues when using this line. This is not how to use open properly. Use the corresponding context manager instead.

As stated before write out your code one step after another and refactor it later.

Solution

with open("a.txt", "r") as f:
    keys = (line.split("|")[2].rstrip("\n") for line in f.readlines())

Upvotes: 1

ggorlen
ggorlen

Reputation: 56965

First of all, dm.txt needs quotes around it -- it's a string, not an object, so your code won't run at all as-is, and you should get a pretty clear NameError about that.

Secondly, please don't try to parse CSVs by hand, even if they're trivial. Use the builtin CSV library and access row[2] (the third column) in each row:

Python 3.9.0 (tags/v3.9.0:9cf6752, Oct  5 2020, 15:34:40) [MSC v.1927 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> with open("dm.csv", newline="") as csvfile:
...     reader = csv.reader(csvfile, delimiter="|")
...     keys = [row[2] for row in reader]
...
>>> keys
['Name', 'City', 'Code']

Now you can key into your config file using the elements of the keys list.

Upvotes: 1

Pro-grammar
Pro-grammar

Reputation: 375

Off by one error... indexes start at 0 then 1 then 2 etc... so you need to use 2 instead of 3 for the 3rd key. In this statement x.split("|")[3] you are asking for the 4th item in the split and there are only 3 items in your split, hence the keyError message.

I recommend using

keys = [ x.split("|")[2].rstrip("\n") for x in open("dm.txt").readlines()] 

Additionally:

Try using full paths in read() and open() functions i.e:

from configparser import ConfigParser
config = ConfigParser()
config.read("C:\\PathToIni\\demo.ini")

# cutting the values of file from column 3 
keys = [ x.split("|")[2].rstrip("\n") for x in open("C:\\PathToTxt\\dm.txt").readlines()]
vals = [config["DEMFILE"][x] for x in keys]
print(vals)

This outputs:

["'XYZABC'", "'Kolkata'", "'123'"]

for me using the two files:

dm.txt:

A|B|Name
VB|C|City
SDF|JK|Code

demo.ini

[DEMFILE]
Name='XYZABC'
Surname='TYUIO'
Code='123'
City='Kolkata'

This answer also shows how to create a generic agnostic relative path Relative paths in Python

Upvotes: 0

Related Questions