在python 3.3中将csv作为列表导入

在python 3.3中将csv作为列表导入,python,list,csv,python-3.x,import,Python,List,Csv,Python 3.x,Import,我有一个包含2个字符串和1个整数的列表 list_of_ee = [["a","m",15],["w","p",34]] 我使用此代码将其导出到csv文件 import csv myfile = open("pppp.csv", 'wb') with open("pppp.csv", "w",newline='') as myfile: wr = csv.writer(myfile, quoting=csv.QUOTE_NONE) wr.writerows(list_of_e

我有一个包含2个字符串和1个整数的列表

list_of_ee = [["a","m",15],["w","p",34]]
我使用此代码将其导出到csv文件

import csv

myfile = open("pppp.csv", 'wb')
with open("pppp.csv", "w",newline='') as myfile:
    wr = csv.writer(myfile, quoting=csv.QUOTE_NONE)
    wr.writerows(list_of_ee)
结果是

a m 15
w p 34
我用这个代码导出它

data = csv.reader(open('pppp.csv','r', newline=''))
data = list(data)
print(data)
结果是

[['a', 'm', '15'], ['w', 'p', '34']]
但是我想在
list\u of_ee
我想把它们添加到ee的列表中(因为当程序启动时,里面没有数据)
list\u of_ee

没有CSV内置方式来完成您的要求,因为CSV格式没有数据类型的概念;也就是说,它不区分字符串和整数

您可以
尝试
这样做:

for lst in list_of_ee:
    for i,l in enumerate(lst):
        try:
            lst[i] = int(l)
        except:
            pass

将子列表中的数字元素转换为整数。

由于CSV格式没有数据类型的概念,因此没有CSV内置方式来执行您的要求;也就是说,它不区分字符串和整数

您可以
尝试
这样做:

for lst in list_of_ee:
    for i,l in enumerate(lst):
        try:
            lst[i] = int(l)
        except:
            pass

将子列表中的数字元素转换为整数。

由于CSV格式没有数据类型的概念,因此没有CSV内置方式来执行您的要求;也就是说,它不区分字符串和整数

您可以
尝试
这样做:

for lst in list_of_ee:
    for i,l in enumerate(lst):
        try:
            lst[i] = int(l)
        except:
            pass

将子列表中的数字元素转换为整数。

由于CSV格式没有数据类型的概念,因此没有CSV内置方式来执行您的要求;也就是说,它不区分字符串和整数

您可以
尝试
这样做:

for lst in list_of_ee:
    for i,l in enumerate(lst):
        try:
            lst[i] = int(l)
        except:
            pass

将子列表中的数字元素转换为整数。

如果使用CSV,则会得到字符串。请参阅文档:

“不执行自动数据类型转换。”

@hcwhsa为您指出了一个答案,说明了如何使用
ast.literal\u eval()
猜测类型。这并不难:

import ast
import csv

def convert_type(s):
    try:
        return ast.literal_eval(s)
    except (ValueError, SyntaxError):
        return s

def convert_csv_row(lst):
    return [convert_type(x) for x in lst]

data = csv.reader(open('pppp.csv','r', newline=''))
converted = [convert_csv_row(row) for row in data]
print(converted)
但实际上,如果您想保留类型,为什么还要使用CSV?除非您将数据导出到电子表格或其他什么东西,否则我建议您使用JSON格式

import json

list_of_ee = [["a","m",15],["w","p",34]]

with open("test.json", "wt") as f:
    f.write(json.dumps(list_of_ee))

with open("test.json", "rt") as f:
    s = f.read()
    lst = json.loads(s)

print(lst)
assert list_of_ee == lst
JSON是将数据导出到其他程序的好方法

但是,如果您只是想为自己的Python程序保存数据,那就更容易了:只需使用
pickle


使用
pickle
您需要以二进制模式而不是文本模式写入和读取文件。同样,使用
pickle
您可以保存几乎任何Python本机类型,不仅仅是JSON支持的基本类型。但几乎只有Python程序读取
pickle
文件。

如果使用CSV,则会得到字符串。请参阅文档n:

“不执行自动数据类型转换。”

@hcwhsa为您指出了一个答案,说明了如何使用
ast.literal\u eval()
猜测类型。这并不难:

import ast
import csv

def convert_type(s):
    try:
        return ast.literal_eval(s)
    except (ValueError, SyntaxError):
        return s

def convert_csv_row(lst):
    return [convert_type(x) for x in lst]

data = csv.reader(open('pppp.csv','r', newline=''))
converted = [convert_csv_row(row) for row in data]
print(converted)
但实际上,如果您想保留类型,为什么还要使用CSV?除非您将数据导出到电子表格或其他什么东西,否则我建议您使用JSON格式

import json

list_of_ee = [["a","m",15],["w","p",34]]

with open("test.json", "wt") as f:
    f.write(json.dumps(list_of_ee))

with open("test.json", "rt") as f:
    s = f.read()
    lst = json.loads(s)

print(lst)
assert list_of_ee == lst
JSON是将数据导出到其他程序的好方法

但是,如果您只是想为自己的Python程序保存数据,那就更容易了:只需使用
pickle


使用
pickle
您需要以二进制模式而不是文本模式写入和读取文件。同样,使用
pickle
您可以保存几乎任何Python本机类型,不仅仅是JSON支持的基本类型。但几乎只有Python程序读取
pickle
文件。

如果使用CSV,则会得到字符串。请参阅文档n:

“不执行自动数据类型转换。”

@hcwhsa为您指出了一个答案,说明了如何使用
ast.literal\u eval()
猜测类型。这并不难:

import ast
import csv

def convert_type(s):
    try:
        return ast.literal_eval(s)
    except (ValueError, SyntaxError):
        return s

def convert_csv_row(lst):
    return [convert_type(x) for x in lst]

data = csv.reader(open('pppp.csv','r', newline=''))
converted = [convert_csv_row(row) for row in data]
print(converted)
但实际上,如果您想保留类型,为什么还要使用CSV?除非您将数据导出到电子表格或其他什么东西,否则我建议您使用JSON格式

import json

list_of_ee = [["a","m",15],["w","p",34]]

with open("test.json", "wt") as f:
    f.write(json.dumps(list_of_ee))

with open("test.json", "rt") as f:
    s = f.read()
    lst = json.loads(s)

print(lst)
assert list_of_ee == lst
JSON是将数据导出到其他程序的好方法

但是,如果您只是想为自己的Python程序保存数据,那就更容易了:只需使用
pickle


使用
pickle
您需要以二进制模式而不是文本模式写入和读取文件。同样,使用
pickle
您可以保存几乎任何Python本机类型,不仅仅是JSON支持的基本类型。但几乎只有Python程序读取
pickle
文件。

如果使用CSV,则会得到字符串。请参阅文档n:

“不执行自动数据类型转换。”

@hcwhsa为您指出了一个答案,说明了如何使用
ast.literal\u eval()
猜测类型。这并不难:

import ast
import csv

def convert_type(s):
    try:
        return ast.literal_eval(s)
    except (ValueError, SyntaxError):
        return s

def convert_csv_row(lst):
    return [convert_type(x) for x in lst]

data = csv.reader(open('pppp.csv','r', newline=''))
converted = [convert_csv_row(row) for row in data]
print(converted)
但实际上,如果您想保留类型,为什么还要使用CSV?除非您将数据导出到电子表格或其他什么东西,否则我建议您使用JSON格式

import json

list_of_ee = [["a","m",15],["w","p",34]]

with open("test.json", "wt") as f:
    f.write(json.dumps(list_of_ee))

with open("test.json", "rt") as f:
    s = f.read()
    lst = json.loads(s)

print(lst)
assert list_of_ee == lst
JSON是将数据导出到其他程序的好方法

但是,如果您只是想为自己的Python程序保存数据,那就更容易了:只需使用
pickle


使用
pickle
时,您需要以二进制模式而不是文本模式写入和读取文件。同样,使用
pickle
时,您可以保存几乎任何Python本机类型,不仅仅是JSON支持的基本类型。但几乎只有Python程序读取
pickle
文件。

这可能会有帮助:kojiro ya i没有我没有从这个可能的副本中得到任何好的回答:@kojiro ya我没有从这个可能的副本中得到任何好的回答:@kojiro ya我没有从这个可能的副本中得到任何好的回答:@kojiro ya我没有从那得到任何好的回答所以还有其他方法吗?如果你没有,我你可以这样做:
lst[i]=int(l)如果l.isdigit()或者l
那么还有其他方法吗?如果你不喜欢
试试
/
除了