Python Forum

Full Version: Reading list items without brackets and quotes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
These brackets and quotes always cause me grief.

If I create the list from reading a csv file then print the list, it prints with brackets.

import csv
with open ('csvtest2.csv','r') as csv_file:
    reader =csv.reader(csv_file)
    included_cols = [0]
    next(reader) # skip first row
    gateways  =[]
    for row in reader:
        content = list(row[i] for i in included_cols)
        gateways.append (content)

print(*gateways, sep = "\n")
OUTPUT:
['NODE001']
['NODE002']
['NODE003']
['NODE004']

This breaks my SSH API code which appears to be reading the item with the brackets and not the actual item. If I can get print to print correctly then my app should work. The problem appears to be how the list is written. This shouldn't be that hard. Help.. please?


If I create the list on the fly eg. list=["item1", "item2", "item3"] it prints only the items without bracket and quotes.

gateways = ["NODE001", "NODE002", "NODE003", "NODE004"]
print(*gateways, sep = "\n")
OUTPUT:
NODE001
NODE002
NODE003
NODE004

The SSH API works fine with above.

Thanks much!
The square brackets mean it is a list. And that is exactly what you do, you are making a list of it. A list with one item.
content = list(row[i] for i in included_cols)
If you don't want that you can do:
content = row[0]
In the second one you're making a list from several strings: gateways = ["NODE001", "NODE002", "NODE003", "NODE004"]

In the first you're extracting the strings (from potentially multiple columns) and shoving them into a list. Then you're packing all the lists into your gateways list. It looks like it's been written so that you could potentially include multiple columns, not just the nodename.

So I think you've got two choices. If you might need multiple columns, change your SSH process to extract the nodename.

If you don't need multiple columns, change the code so you only spit out the nodename (like:...)
import csv
with open ('csvtest2.csv','r') as csv_file:
    reader =csv.reader(csv_file)
    next(reader) # skip first row
    node_column = 0
    gateways = [row[node_column] for row in reader]

print(*gateways, sep = "\n")
@ibreeden

Thank you! I kid you not I would pay you right now if I could!

So the second example is NOT a list??

gateways = ["NODE001", "NODE002", "NODE003", "NODE004"]
Which results in single items being printed?

Thanks and thanks again!
The second example is a list. It's a list of strings.
The first one is also a list, but it's list of other lists.

>>> l1 = ["a", "b", "c"]
>>> l2 = [["d"], ["e"], ["f"]]
>>> print(*l1, sep="\n")
a
b
c
>>> print(*l2, sep="\n")
['d']
['e']
['f']
@bowlofred Thanks

I think I got it. So the 2nd example is a list of single strings.

gateways = ["NODE001", "NODE002", "NODE003", "NODE004"]
Prints only items..

How would that line be changed so that printing it WOULD produce the brackets/quotes?

These to example didn't work :

gateways = ["NODE001,dat1", "NODE002,dat2", "NODE003,dat3", "NODE004,dat4"]
gateways = [NODE001,dat1, NODE002,dat2, NODE003,dat3, NODE004,dat4]
(Jan-14-2022, 06:58 PM)bowlofred Wrote: [ -> ]The second example is a list. It's a list of strings.
The first one is also a list, but it's list of other lists.

>>> l1 = ["a", "b", "c"]
>>> l2 = [["d"], ["e"], ["f"]]
>>> print(*l1, sep="\n")
a
b
c
>>> print(*l2, sep="\n")
['d']
['e']
['f']

Got it!! Thank you.