Python Forum
Reading list items without brackets and quotes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading list items without brackets and quotes
#1
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!

Attached Files

.py   Pytest1.py (Size: 438 bytes / Downloads: 187)
.csv   csvtest2.csv (Size: 56 bytes / Downloads: 166)
Reply
#2
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]
Reply
#3
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")
Reply
#4
@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!
Reply
#5
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']
Reply
#6
@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]
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to parse and group hierarchical list items from an unindented string in Python? ann23fr 0 179 Mar-27-2024, 01:16 PM
Last Post: ann23fr
  trouble reading string/module from excel as a list popular_dog 0 416 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,321 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Finding combinations of list of items (30 or so) LynnS 1 871 Jan-25-2023, 02:57 PM
Last Post: deanhystad
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 1,509 Sep-03-2022, 11:21 PM
Last Post: Winfried
  For Word, Count in List (Counts.Items()) new_coder_231013 6 2,574 Jul-21-2022, 02:51 PM
Last Post: new_coder_231013
  How to get list of exactly 10 items? Mark17 1 2,503 May-26-2022, 01:37 PM
Last Post: Mark17
  how to assign items from a list to a dictionary CompleteNewb 3 1,564 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Data pulled from SQL comes in brackets nickzsche 3 2,645 Jan-04-2022, 03:39 PM
Last Post: ibreeden
  For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game new_coder_231013 7 2,232 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020