Create a List that contain each Line of a File

前端 未结 8 1508
无人及你
无人及你 2020-12-23 16:52

I\'m trying to open a file and create a list with each line read from the file.

   i=0
   List=[\"\"]
   for Line in inFile:
      List[i]=Line.split(\",\")
         


        
8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 17:27

    Please read PEP8. You're swaying pretty far from python conventions.

    If you want a list of lists of each line split by comma, I'd do this:

    l = []
    for line in in_file:
        l.append(line.split(','))
    

    You'll get a newline on each record. If you don't want that:

    l = []
    for line in in_file:
        l.append(line.rstrip().split(','))
    

提交回复
热议问题