简体   繁体   English

从 csv 导入数据,其中每个列表被分成多行

[英]Importing data from csv where each list is split into many rows

Hi so I'm a bit stuck with this problem.嗨,所以我有点被这个问题困住了。 I've got a csv file, which looks something like this:我有一个 csv 文件,它看起来像这样:

[12  34 45 22 3 5
 34 33 2 67 5 55
 2 90 88 12 34]
[245  4 13]
[33 90 50 22 90 1
 23 44 876  10 7] ...

And so on.等等。 In other words, the csv file is split into lists of numbers separated either by a single space or double spaces and if the list of numbers exceeds a certain number of values (14 in my case), it continues the list on the next line until the list of numbers end.换句话说,csv 文件被拆分为由单个空格或双空格分隔的数字列表,如果数字列表超过一定数量的值(在我的情况下为 14),它会继续下一行的列表,直到数字列表结束。 The lists of numbers are not separated by commas, but each new list begins and ends with the square brackets.数字列表不以逗号分隔,但每个新列表都以方括号开头和结尾。

I want to import the csv file into a list of lists, which would look like this:我想将 csv 文件导入到列表列表中,如下所示:

[[12, 34, 45, 22, 3, 5, 34, 33, 2, 67, 5, 55, 2, 90, 88, 12, 34], 
[245, 4, 13], 
[33, 90, 50, 22, 90, 1, 23, 44, 876, 10, 7], 
[...]]

How could I achieve this?我怎么能做到这一点? I've tried np.loadtxt and pandas, but both treat every line as its own observation.我尝试过 np.loadtxt 和 pandas,但都将每一行都视为自己的观察。

Thanks in advance!提前致谢!

Edit: The numbers are actually separated either by a single space or double spaces.编辑:数字实际上由一个空格或双空格分隔。

The following should work:以下应该工作:

with open('myfile.csv') as f:
    t=f.read()
t=t.replace('\n', '').replace('  ', ' ').replace(' ', ',')
l=t.split(']')
l.pop()
l=[i.replace('[', '') for i in l] 
result=[[int(s) for s in k.split(',')] for k in l]
print(result)

Output:输出:

[[12, 34, 45, 22, 3, 5, 34, 33, 2, 67, 5, 55, 2, 90, 88, 12, 34], [245, 4, 13], [33, 90, 50, 22, 90, 1, 23, 44, 876, 10, 7]]

You can use the built in csv library and then just split the values per row:您可以使用内置的csv库,然后只拆分每行的值:

import csv

with open('test.csv', 'r') as testCsvFile:
    testCsv = csv.reader(testCsvFile)
    listOfLists = []
    for row in testCsv:
        listOfLists.append([int(val) for val in row[0][1:-1].split(' ')])
    print(listOfLists)


# Output
# [[12, 34, 45, 22, 3, 5, 34, 33, 2, 67, 5, 55, 2, 90, 88, 12, 34], [245, 4, 13], [33, 90, 50, 22, 90, 1, 23, 44, 876, 10, 7]]

Edit: Updated parsing to convert the values to int s编辑:更新解析以将值转换为int s

Is this what you are looking for:这是你想要的:

>>> with open("file.txt", "r") as f:
...     content = f.read().replace("\n", "")
... 
>>> content = [[int(i) for i in c.split(" ")] for c in content[1:-1].split("][")]
>>> content
[[12, 34, 45, 22, 3, 5, 34, 33, 2, 67, 5, 55, 2, 90, 88, 12, 34], [245, 4, 13], [33, 90, 50, 22, 90, 1, 23, 44, 876, 10, 7]]

First read in entire file as one string, stripping the first and last characters ( [ and ] ) as well as the newline characters ( \\n ).首先将整个文件作为一个字符串读取,去除第一个和最后一个字符( [] )以及换行符( \\n )。 Then split into chunks divided by ][ .然后分成由][划分的块。 Finally split each chunk by the space character and turn them into integers.最后通过空格字符分割每个块并将它们转换为整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据列表从 python 导入到 csv 时出现问题 - Problems with importing list of data from python to csv 如何以这种方式显示 CSV 中的 200 行数据 '' line[0].split(';') " - How to display 200 rows of data from CSV in this way '' line[0].split(';') " 将数据帧拆分为多个数据帧,其中每个帧仅包含数据不丢失的行和列 - Split dataframe into multiple dataframe where each frame contains only rows and columns where that data isn't missing 将数据从 CSV 文件导入字典,其中键是国家名称,值是 np.arrays - Importing data from a CSV-file into a dictionary, where the keys are country names and the values are np.arrays 从 python 中的 CSV 文件导入字典列表 - Importing a list of dictionaries from a CSV file in python Python:将来自csv文件的行数据放入列表中 - Python: putting rows data from a csv file into a list 从列表中将数据提取到CSV文件中的多行(Python3) - Extract data from a list to multiple rows in a CSV file (Python3) 如何创建一个列表,其中每个条目都是另一个包含 csv 文件信息的列表 - How to create a list where each entry is another list containing information from csv file 使用 python 将数据从 CSV 导入到 MySQL - Importing data from CSV to MySQL using python 从 .CSV 导入数据,在列内搜索 - Importing Data from .CSV, Searching within Columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM