Home > Net >  How do I remove Curly Brackets from csv file imported to combobox?
How do I remove Curly Brackets from csv file imported to combobox?

Time:10-26

this is my fist time playing with Python and I have a little issue with a csv file importing entrys into a combobox. Essentially, any entry with more then one word is surrounded by curly brackets. I'm not sure why this is happening and have no idea how to resolve the problem. Could anybody help me with an explanation of the problem (in simple english please ;)). I have read a few things about strings and concatination but it confuses me to be honest.

owner_var = tk.StringVar()
with open('C:/Users/Me/Desktop/Test/Sample.csv', encoding='utf-8-sig') as csv_file:
csv_reader = csv.reader(csv_file)
data = list(csv_reader)
owner_drop_down = ttk.Combobox(master, state='readonly', values=data, textvariable=owner_var, font="Verdana 12").place(x=750, y=300, height=30, width=280)

My CSV reads like this
Column A
Danny
Peter
Max Smith
John Dent
Andrew

and it prints like this
Print [['Danny'], ['Peter'], ['Max Smith'], ['John Dent'], ['Andrew']]

But what I get in my output looks like this
Danny
Peter
{Max Smith}
{John Dent}
Andrew

Can anybody explain to me what is happening and how can I fix this?

CodePudding user response:

Try the below - the code read the lines 1 by 1 and collect them in a list. Since we dont need the first line we skip it ([1:])

with open('my.csv') as f:
  words = [w.strip() for w in f.readlines()][1:]
  print(words)

my.csv

Column A
Danny
Peter
Max Smith
John Dent
Andrew

output

['Danny', 'Peter', 'Max Smith', 'John Dent', 'Andrew']

CodePudding user response:

If your csv file contains single column you can read it line by line.

data = []
with open(r"C:\Users\Me\Desktop\Test\Sample.csv") as file:
    for line in file:
        data.append(line)

print(data)

It will give you next output:

['Column A\n', 'Danny\n', 'Peter\n', 'Max Smith\n', 'John Dent\n', 'Andrew']

As you see there're two problems with this output:

  1. CSV header is not skipped;
  2. Each string (except last) contains trailing \n.

Quote from docs of open():

Open file and return a corresponding file object.

And one more from Reading and Writing Files section of python tutorial:

For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code.

From this we can assume that file object is an iterator. To solve first problem we need to skip first item of file object (iterator). We can use built-in function next():

data = []
with open(r"C:\Users\Me\Desktop\Test\Sample.csv") as file:
    next(file)  # skip first line
    for line in file:
        data.append(line)

print(data)

It will give us next output:

['Danny\n', 'Peter\n', 'Max Smith\n', 'John Dent\n', 'Andrew']

There's just one problem with this solution. Quote from next() docs:

If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

It means that if input file is empty and we won't pass default argument we will get an exception. Solution is obvious, just add the default argument.

To solve problem with trailing \n we could apply str.rstrip(). If we don't pass any arguments to str.rstrip() it will remove all whitespaces. Code:

data = []
with open(r"C:\Users\Me\Desktop\Test\Sample.csv") as file:
    next(file, None)  # added default value
    for line in file:
        data.append(line.rstrip())  # added str.rstrip()

print(data)

It will give us proper output:

['Danny', 'Peter', 'Max Smith', 'John Dent', 'Andrew']

You can shorten this code using list comprehension:

with open(r"C:\Users\Me\Desktop\Test\Sample.csv") as file:
    next(file, None)
    data = [line.rstrip() for line in file]

print(data)

Or you can do it even shorter:

with open(r"C:\Users\Me\Desktop\Test\Sample.csv") as file:
    data = [line.rstrip() for line in next(file, True) and file]

print(data)
  • Related