1

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?

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 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 – Pro-grammar Aug 18 '21 at 16:35

3 Answers3

1

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())
ggorlen
  • 44,755
  • 7
  • 76
  • 106
SvenTUM
  • 784
  • 1
  • 6
  • 16
1

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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
0

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

Pro-grammar
  • 365
  • 2
  • 17
  • its type error i have corrected , then also it gets fail –  Aug 18 '21 at 16:38
  • raise KeyError(key) –  Aug 18 '21 at 16:43
  • first part is their here : https://stackoverflow.com/questions/68834778/retrieve-multiple-configuration-keys-from-ini-file-based-on-dynamic-list just read this here in this question i have getting from file it gets fail –  Aug 18 '21 at 16:46
  • @SvenTUM is correct. dm.txt needs quotations around it. – Pro-grammar Aug 18 '21 at 17:03