Home > OS >  What is the python code to store a single column of csv values as a list of tuples (without trailing
What is the python code to store a single column of csv values as a list of tuples (without trailing

Time:05-29

I have a csv file containing a single row of values. I want to save it as a list of tuples like the one below. Whenever I try doing so, it stores it with a trailing comma within each tuple.

What I want:

[('mdl_analytics_models'),('mdl_assignment_upgrade'), ('mdl_assignment'), ('mdl_user')]

and what I'm getting instead:

[('mdl_analytics_models',), ('mdl_assignment_upgrade',), ('mdl_assignment',), ('mdl_user',)]

suppose my csv file looks like this:

| tablename |
| -------- |
| mdl_analytics_models    | 
| mdl_assignment_upgrade   | 
|mdl_assignment|
|mdl_user|

I've tried these code snippets from similar questions which are giving me the output with the trailing commas:

import pandas as pd
# Create a dataframe from csv
df = pd.read_csv('data.csv', delimiter=',')
# Create a list of tuples for Dataframe rows using list comprehension
list_of_tuples = [tuple(row) for row in df.values]
# Print list of tuple
print(list_of_tuples)

with open('data.csv', 'r') as read_obj:
    # pass the file object to reader() to get the reader object
    csv_reader = reader(read_obj)
    # Pass reader object to list() to get a list of lists
    data = list(zip(csv_reader))

CodePudding user response:

You're currently reading all of the columns (even through there's one), and being returned singular tuples.

The parenthesis in your "wanted output" don't mean anything without the comma.

If you really only wanted the first column rather than all columns, you need to index the dataframe

df = pd.read_csv('data.csv', delimiter=',')
# list of strings 
tables = list(df["tablename"])
# list of single tuples 
table_tuples = [ (t,) for t in df["tablename"] ] 

To get parenthesis within the strings, format it (or use pandas apply function over the column first), then collect

tables = [ f"({t})" for t in df["tablename"] ] 
  • Related