Python import csv to list

后端 未结 13 1096
后悔当初
后悔当初 2020-11-22 06:15

I have a CSV file with about 2000 records.

Each record has a string, and a category to it:

This is the firs         


        
相关标签:
13条回答
  • 2020-11-22 06:54

    Extending your requirements a bit and assuming you do not care about the order of lines and want to get them grouped under categories, the following solution may work for you:

    >>> fname = "lines.txt"
    >>> from collections import defaultdict
    >>> dct = defaultdict(list)
    >>> with open(fname) as f:
    ...     for line in f:
    ...         text, cat = line.rstrip("\n").split(",", 1)
    ...         dct[cat].append(text)
    ...
    >>> dct
    defaultdict(<type 'list'>, {' CatA': ['This is the first line', 'This is the another line'], ' CatC': ['This is the third line'], ' CatB': ['This is the second line', 'This is the last line']})
    

    This way you get all relevant lines available in the dictionary under key being the category.

    0 讨论(0)
  • 2020-11-22 06:57

    Updated for Python 3:

    import csv
    
    with open('file.csv', newline='') as f:
        reader = csv.reader(f)
        your_list = list(reader)
    
    print(your_list)
    

    Output:

    [['This is the first line', 'Line1'], ['This is the second line', 'Line2'], ['This is the third line', 'Line3']]
    
    0 讨论(0)
  • 2020-11-22 06:59

    Update for Python3:

    import csv
    from pprint import pprint
    
    with open('text.csv', newline='') as file:
        reader = csv.reader(file)
        res = list(map(tuple, reader))
    
    pprint(res)
    

    Output:

    [('This is the first line', ' Line1'),
     ('This is the second line', ' Line2'),
     ('This is the third line', ' Line3')]
    

    If csvfile is a file object, it should be opened with newline=''.
    csv module

    0 讨论(0)
  • 2020-11-22 07:00

    If you are sure there are no commas in your input, other than to separate the category, you can read the file line by line and split on ,, then push the result to List

    That said, it looks like you are looking at a CSV file, so you might consider using the modules for it

    0 讨论(0)
  • 2020-11-22 07:00

    You can use the list() function to convert csv reader object to list

    import csv
    
    with open('input.csv') as csv_file:
        reader = csv.reader(csv_file, delimiter=',')
        rows = list(reader)
        print(rows)
    
    0 讨论(0)
  • 2020-11-22 07:01
    result = []
    for line in text.splitlines():
        result.append(tuple(line.split(",")))
    
    0 讨论(0)
提交回复
热议问题