在Python中将列作为列表,集合和字典提取

时间:2018-04-07 13:34:37

标签: python python-3.x csv tabular

给定一个csv文件,其中一些列包含列表,集合或字典 一个具有以下结构:

| user_id| items                   | methods        | dict_col      |
|--------|-------------------------|----------------|---------------|
| ID01   | [potato, apple, potato] | {card, cash}   | {F: [AB, CD]} |
| ID02   | [carrots, papaya]       | {bitcoin, card}| {F: [AB, CD]} |

有没有办法以表格方式在Python中摄取它,其中维护这些列中的值的类型?

如果没有,将它们转换回list,set,dictionaries的最佳方法是什么?

问题源于这样一个事实:一旦有一个具有这种结构的DataFrame并且它被保存到csv中,当用pandas.read_csv()加载csv时,这些列中的值不再是列表,set或词典。

在代码下面重新创建上面描述的场景。

import pandas as pd

# Create dummy example
df = pd.DataFrame({'user_id': ['ID01', 'ID02'], 'items': [['potato',   'apple', 'potato'],['carrots', 'papaya']],
              'methods': [{'card', 'cash'}, {'bitcoin', 'card'}],
               'dict_col': [{'F': ['AB', 'CD']}, {'F': ['AB', 'CD']}]})
df[['user_id', 'items', 'methods', 'dict_col']]

type(df.iloc[0]['dict_col']) # Return a dict

df.to_csv('dummy_table.csv', index = False)
# Reload the table
df_loaded = pd.read_csv('dummy_table.csv')

"""
Line below returns a str and not a dict as in the original dataframe.   How we go back to the original datatypes 
(e.g. list, dict, set)in a pythonic way?
"""
type(df_loaded.iloc[0]['dict_col'])

在Kyle J.评论尝试使用cvs.DictReader 之后尝试 我尝试使用DictReader,但目标未得到满足。但是,我不确定这是凯尔的想法。

尝试使用DictReader

import csv
import pandas as pd
df = pd.DataFrame()
with open('dummy_table.csv', newline = '') as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        df = pd.concat([df, pd.DataFrame(row, index = [0])], axis = 0)
type(df.iloc[0]['dict_col']) # Still a str

1 个答案:

答案 0 :(得分:1)

如果标准csv模块以相同的方式执行,特别是为了解决您的问题,您应该尝试

   ********mainFile.py********

   from dbconfig import*
   import pymysql
   from datetime import datetime, date, time
   import sys

  db = get_mysql_param();
  cnx = pymysql.connect(user=db['user'], password=db['password'],
                        host=db['host'],
                        database=db['database'])

cursor = cnx.cursor()

meetId = input("MeetId: ")

query = """
     SELECT DISTINCT e.eventId as eventId, e.Title as title, concat(s.fname, 
       " ", s.lname) as sName, 
       if (Isnull(p.Comment), '', comment) as comment FROM Event e, Swimmer 
       s, Participation p, Meet m WHERE 
       s.SwimmerId = p.SwimmerId AND p.EventId = e.EventId AND 
       m.MeetId = %s AND e.MeetId = m.MeetId ORDER by e.eventId
       """


cursor.execute(query,(meetId, ))



******dbconfig.py file*******
      import configparser 

      #  simplistic and no error handling.
     def get_mysql_param(filename='dbconfig.ini', section='mysql'):

           config = configparser.ConfigParser()
           config.read(filename)

           return config['mysql']

******dbconfig.ini file*****
[mysql]
host = localhost
database = clystms
user = **********
password = *********