将CSV导入列表Python列表

时间:2016-04-27 07:28:15

标签: python csv

Python新手在这里。

我有一个CSV文件,其中包含这种格式的数字

9143.680696, 427953.500000, 11919.104475, 11908.727555, 1.000871, 0.029506, 15.546608, 93, 121, 123, 7
7704.773182, 330297.500000, 19186.759308, 19170.146116, 1.000867, 0.029426, 14.302257, 93, 121, 123, 7

我需要读取文件,使列表像这样

[
[[9143.680696, 427953.500000, 11919.104475, 11908.727555, 1.000871, 0.029506, 15.546608, 93, 121, 123], [7]],
[[7704.773182, 330297.500000, 19186.759308, 19170.146116, 1.000867, 0.029426, 14.302257, 93, 121, 123], [7]]
]

每行的最后一个数字存储在一个不同的列表中,就像这里的7一样。

我已经在这里研究了一些答案,但发现它们作为字符串存储在列表中,与我正在处理的问题不兼容。

提前感谢您的帮助。

6 个答案:

答案 0 :(得分:3)

你可以这样试试,

foreach(KeyValuePair<string, TextBox> textbox in DTB)
{
    Numbers[textbox.Key] = Convert.ToDecimal(textbox.Value.Text);
}

如果您想要>>> csv = '''9143.680696, 427953.500000, 11919.104475, 11908.727555, 1.000871, 0.029506, 15.546608, 93, 121, 123, 7 7704.773182, 330297.500000, 19186.759308, 19170.146116, 1.000867, 0.029426, 14.302257, 93, 121, 123, 7''' >>> [[line.split(',')[0:-1], [line.split(',')[-1]]] for line in csv.splitlines()] [[['9143.680696', ' 427953.500000', ' 11919.104475', ' 11908.727555', ' 1.000871', ' 0.029506', ' 15.546608', ' 93', ' 121', ' 123'], [' 7']], [['7704.773182', ' 330297.500000', ' 19186.759308', ' 19170.146116', ' 1.000867', ' 0.029426', ' 14.302257', ' 93', ' 121', ' 123'], [' 7']]] 项,可以使用float

map

漂亮的印刷品:

>>> data = csv.splitlines()
>>> data = [map(float, line.split(',')) for line in csv.splitlines()]
>>> [[items[:-1], items[-1]] for items in data]
[[[9143.680696, 427953.5, 11919.104475, 11908.727555, 1.000871, 0.029506, 15.546608, 93.0, 121.0, 123.0], 7.0], [[7704.773182, 330297.5, 19186.759308, 19170.146116, 1.000867, 0.029426, 14.302257, 93.0, 121.0, 123.0], 7.0]]

答案 1 :(得分:1)

CSV libraries通常将字段读为字符串,因此您需要显式转换字段。来自csv模块的文档:

  

从csv文件读取的每一行都作为字符串列表返回。没有   执行自动数据类型转换。

>>> import csv
>>> with open('eggs.csv', 'rb') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
...     for row in spamreader:
...         <process row>

同样,CSV库会平等地处理所有字段,因此您需要显式地将最后一个字段包装在列表中。

例如:

a = ["1.23", "2.34", "10", "100", "1000"]

>>> map(float, a[0:2]) + map(int, a[2:4]) + [[int(a[4])]]

[1.23, 2.34, 10, 100, [1000]]

答案 2 :(得分:0)

只需使用[]运算符即可获取列表的左右部分:

import csv
...
list = []
with open(filename, "rb") as fd:
    reader = csv.reader(fd, delimiter = ",")
    for row in reader:
        left = list(map(lambda x: float(x), row[:-1]))
        right = list(map(lambda x: float(x), row[-1:]))
        list.append([ left, right ])

答案 3 :(得分:0)

您需要遍历列表并将其转换为整数。还要以所需的列表格式存储它们。

示例:

import csv
l = list()
with open('data.csv', 'r') as csvfile:
   reader = csv.reader(csvfile, delimiter=',')
   for row in reader:
   l.append([[float(row[:-1])]+[float(row[-1])]])
print(l)

答案 4 :(得分:0)

你可以试试这个, 将输入文件名视为input.csv

import csv
new_list = []
with open('input.csv') as inp:
    csv_reader = csv.reader(inp, delimiter=',')
    for line in csv_reader:
        new_list.append([map(float, line[:-1])] + [map(float, line[-1:])])
来自ipython的

演示

In [1]: import csv

In [2]: new_list = []

In [3]: with open('input.csv') as inp:
   ...:     csv_reader = csv.reader(inp, delimiter=',')
   ...:     for line in csv_reader:
   ...:         new_list.append([line[:-1]] + [line[-1:]])
   ...:         

In [4]: new_list
Out[4]: 


    [[[9143.680696,
   427953.5,
   11919.104475,
   11908.727555,
   1.000871,
   0.029506,
   15.546608,
   93.0,
   121.0,
   123.0],
  [7.0]],
 [[7704.773182,
   330297.5,
   19186.759308,
   19170.146116,
   1.000867,
   0.029426,
   14.302257,
   93.0,
   121.0,
   123.0],
  [7.0]]]

答案 5 :(得分:0)

不使用外部模块的最简单方法:

更新:我将新的float(...)方法替换为简单的convert(...)转换,该方法尝试生成一个浮点数并返回原始字符串(或者可以执行其他操作)如果令牌不是数字,则不是抛出异常。

def convert(value_str):
    try:  # try to convert it to a float:
        return float(value_str)
    except ValueError:  # if it is not a valid float literal, return the original string:
        return value_str

with open("file.csv") as csvfile:
    split_lines = [line.split(",") for line in csvfile]
    data = [[[convert(n) for n in line[:-1]], [convert(line[-1])]] for line in split_lines]

    print(data)

从问题(手动格式化)输出示例数据:

[
  [ [9143.680696, 427953.5, 11919.104475, 11908.727555, 1.000871, 0.029506, 15.546608, 93.0, 121.0, 123.0], [7.0] ],
  [ [7704.773182, 330297.5, 19186.759308, 19170.146116, 1.000867, 0.029426, 14.302257, 93.0, 121.0, 123.0], [7.0] ]
]