I want to be able to turn csv file into a list of lists . my csv file is like that :
['juridiction', 'audience', 'novembre'],['récapitulatif', 'information', 'important', 'octobre'],['terrain', 'entent', 'démocrate'],['porte-parole', 'tribunal', 'monastir', 'farid ben', 'déclaration', 'vendredi', 'octobre', 'télévision', 'national', 'mère', 'fillette', 'an', 'clandestinement', 'italie', 'juge', 'instruction', 'interrogatoire', 'père'],['disposition', 'décret', 'vigueur', 'premier', 'octobre'],['décret', 'loi', 'numéro', '2022', 'octobre', 'disposition', 'spécial', 'amélioration', 'efficacité', 'réalisation', 'projet', 'public', 'priver', 'jort', 'vendredi', 'octobre'],['avocat', 'rahal jallali', 'déclaration', 'vendredi', 'octobre', 'tap', 'militant', 'membre', 'section', 'bardo', 'ligue', 'droit', 'homme', 'membre', 'association', 'damj', 'saif', 'ayadi', 'jeune', 'juge', 'instruction', 'tribunal', 'instance'],...
into
list1 = [['juridiction', 'audience', 'novembre'],['récapitulatif', 'information', 'important', 'octobre'],['terrain', 'entent', 'démocrate'],['porte-parole', 'tribunal', 'monastir', 'farid ben', 'déclaration', 'vendredi', 'octobre', 'télévision', 'national', 'mère', 'fillette', 'an', 'clandestinement', 'italie', 'juge', 'instruction', 'interrogatoire', 'père'],['disposition', 'décret', 'vigueur', 'premier', 'octobre'],['décret', 'loi', 'numéro', '2022', 'octobre', 'disposition', 'spécial', 'amélioration', 'efficacité', 'réalisation', 'projet', 'public', 'priver', 'jort', 'vendredi', 'octobre'],['avocat', 'rahal jallali', 'déclaration', 'vendredi', 'octobre', 'tap', 'militant', 'membre', 'section', 'bardo', 'ligue', 'droit', 'homme', 'membre', 'association', 'damj', 'saif', 'ayadi', 'jeune', 'juge', 'instruction', 'tribunal', 'instance'],...]]
Ive try to solve this but no success :
import csv
from itertools import zip_longest
with open('/content/drive/MyDrive/tokens.csv') as csvfile:
rows = csv.reader(csvfile)
res = list(zip_longest(*rows))
list1 = [list(filter(None.__ne__, l)) for l in res]
print(res2)
but the output is :
[["['juridiction'"], [" 'audience'"], [" 'novembre']"], ["['récapitulatif'"], [" 'information'"], [" 'important'"], [" 'octobre']"], ["['terrain'"], [" 'entent'"], [" 'démocrate']"],...
CodePudding user response:
If your file really consists of only one long line, then here's a couple of options:
Use eval
: You need to add the brackets for the outer list.
with open("data.csv", "r") as file:
data = eval("[" file.read().strip() "]")
Use json
: You need to (1) add the outer brackets, and (2) replace the '
with "
to make the string json compliant.
import json
with open("data.csv", "r") as file:
data = json.loads("[" file.read().strip().replace("'", '"') "]")
Use string manipulation: You need to (1) remove the brackets at the edges, then (2) remove the '
s, then (3) .split
anlong "],["
, and finally (4) .split
the parts along ", "
.
with open("data.csv", "r") as file:
data = [
string.split(", ")
for string in file.read().strip().strip("[]").replace("'", "").split("],[")
]
(Replace data.csv
with your file path.)
CodePudding user response:
Call list.append
:
with open('file.csv') as csvfile:
rows = csv.reader(csvfile)
list1 = []
for row in rows:
list1.append(row)