I have a CSV file with about 2000 records.
Each record has a string, and a category to it:
This is the firs
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.
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']]
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
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
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)
result = []
for line in text.splitlines():
result.append(tuple(line.split(",")))