从python中的行中删除引号

时间:2018-05-28 14:43:07

标签: python-3.x

我有一个包含相同数据的文件:

from functools import reduce
import numpy as np
mushroom = genfromtxt('dane/mushroom.csv', delimiter=',' ,dtype = str)

features = mushroom[:,range(1,23)]
classes = mushroom[:,0]

def toBinaryFeatures(features):
    COLUMNS = features.shape[1]
    v = [x + str(i % COLUMNS) for i, x in enumerate(features.flatten())]
    l = features.tolist() 
    uv = list(set(v)) # unique values of all features

    mv = {} # mapping to unique powers of 2
    for i,x in enumerate(uv):
        mv[x] = 2**i

    as_numbers = [reduce((lambda x, y: x | y), [mv[x + str(i)] for i, x in enumerate(row)]) for row in l]
    TO_BIN = "{0:0" + str(len(mv)) +"b}"
    flattened_features = [[int(char) for char in TO_BIN.format(number)] for number in as_numbers]
    return np.array(flattened_features)

我试图阅读:

{'name': 'Atucha', 'location': (-34.0, -59.167), 'active_reactors': 1},
    {'name': 'Embalse', 'location': (-32.2333, -64.4333), 'active_reactors': 1},
    {'name': 'Armenia', 'location': (40.167, 44.133), 'active_reactors': 1},
    {'name': 'Br', 'location': (51.217, 5.083), 'active_reactors': 1},
    {'name': 'Doel', 'location': (51.333, 4.25), 'active_reactors': 4},
    {'name': 'Tihange', 'location': (50.517, 5.283), 'active_reactors': 3}

但是在这一步之后,我在列表对象的所有行之前和之后看到非常奇怪的引号:

import csv listok=[] with open('Uni_coord.csv', 'r') as csvfile: reader = csv.reader(csvfile) for row in reader: listok.extend(row)

所需的输出看起来像一个简单的列表对象,每行没有引号:

["{'name':  '', 'location': (), 'NAME': '')}"]

如何在文件中删除或更正确地读取数据?谢谢!

1 个答案:

答案 0 :(得分:1)

@Janek ,您可以在阅读 Uni_coord.csv 后尝试使用以下代码获取词典列表。

  

注意:您的 Uni_coord.csv 文件不包含实际CSV文件应包含的格式的数据,但您仍然可以像读一个普通的文件。

     

问题是你不必在这里使用 csv 模块。

import json

lines =""
with open("Uni_coord.csv") as f:
    for line in f.readlines():
        lines += line.strip()

lines +=  "[" + lines + "]"
my_list = json.loads(lines)

print(l)

# Pretty printing list of dictionaries (it is useful to view the contents of huge list)
print(json.dumps(my_list, indent=4))