【问题标题】:appending list in a list from string inside the file从文件内的字符串中将列表附加到列表中
【发布时间】:2020-04-15 06:22:16
【问题描述】:

所以我有一些数据,我想要做的是创建每行的列表并将其附加到另一个列表中,但我无法想出任何关于如何仅打印第一或第二行的想法线。我尝试拆分整个数据,然后使用索引方法列出每一行,但这就是您手动执行的方式,但我想自动生成。因此,我们将不胜感激。

students = "joe 10 15 20 30 40 \
            bill 23 16 19 22 \
            sue 8 22 17 14 32 17 24 21 2 9 11 17 \
            grace 12 28 21 45 26 10 \
            John 14 32 25 16 89"


file_to_save = open("students.txt", "w")

file_to_save.writelines(students)

file_to_save.close()



file_to_read = open("students.txt", "r")

line = file_to_read.readline()

print(line)

file_to_read.close()

输出可能如下所示:

test = [[joe, 10, 15, 20, 30, 40], [bill, 23, 16, 19, 22], etc...]

【问题讨论】:

    标签: python python-3.x list file


    【解决方案1】:

    您可以使用正则表达式将数据拆分为列表

    >>> import re
    >>> lines = [x.strip() for x in re.findall(r'\w+[\s\d]+', students)]
    >>> lines
    ['joe 10 15 20 30 40', 'bill 23 16 19 22', 'sue 8 22 17 14 32 17 24 21 2 9 11 17', 'grace 12 28 21 45 26 10', 'John 14 32 25 16 89']
    

    【讨论】:

    • 感谢您的帮助,它确实有效,但我不允许使用任何库;D 再次感谢您的帮助。
    【解决方案2】:

    您可以为此使用 python itertools groupby

    from itertools import groupby
    students = "joe 10 15 20 30 40 \
            bill 23 16 19 22 \
            sue 8 22 17 14 32 17 24 21 2 9 11 17 \
            grace 12 28 21 45 26 10 \
            John 14 32 25 16 89"
    
    Data=students.split(' ')
    [list(g) for k,g in groupby(Data,key=lambda x:x == '') if not k]
    

    输出:

    [['joe', '10', '15', '20', '30', '40'], ['bill', '23', '16', '19', '22'], ['sue', '8', '22', '17', '14', '32', '17', '24', '21', '2', '9', '11', '17'], ['grace', '12', '28', '21', '45', '26', '10'], ['John', '14', '32', '25', '16', '89']]
    

    【讨论】:

    • 也感谢您的帮助,但这里也一样,我不允许使用库。感谢您花时间帮助我。
    【解决方案3】:

    问题是由于写入字符串造成的。您正在一行中输入数据,并且能够在一次调用中读取它。

    注意\ 与字符串中的 \n 不同。

    为了解决这个问题,我同意在将数据从@abhilb 添加到文件之前预处理数据的解决方案

    只需使用他的代码并包装我的代码即可解决您的问题。

    import re
    
    students = "joe 10 15 20 30 40 \
                bill 23 16 19 22 \
                sue 8 22 17 14 32 17 24 21 2 9 11 17 \
                grace 12 28 21 45 26 10 \
                John 14 32 25 16 89" ## Still a big chunk of line for your record. 
    
    
    lines = [x.strip() for x in re.findall(r'\w+[\s\d]+', students)] # Credit to @abhilb
    
    with open("students.txt", "w") as file_to_save: # Use this method to lose the hastle to close a file.. 
        for line in lines: # Lines Formed in the Students record as List of Strings... 
            print(line,file=file_to_save) # Saving them to the file with '\n' at the end..
    
    index_of_lines_to_read = {1,3} # Index of the line you want to print.
    
    with open("students.txt", "r") as file_to_read: # Use this method to lose the hastle to close a file..
        line = file_to_read.readline() # Reading first line...
        line_index = 1 # Keep check about the index we have read so far.. 
        while line: # Checking if we did not reach the EOF 
            if line_index in index_of_lines_to_read: # Checking if the current index needs to be printed else skip to next line.
                print(line,end="") # end="" compensate for the extra \n added using print above
            line = file_to_read.readline() # Read next line.. 
            line_index += 1 # Increment the current line index.
    

    输出

    joe 10 15 20 30 40 sue 8 22 17 14 32 17 24 21 2 9 11 17

    【讨论】:

      【解决方案4】:

      如果您想要读取一个文件然后只打印某一行,您可以尝试这样的操作:

      def print_file_line(file_path: str, line_number: int):
          with open(file_path) as file:
              lines = file.readlines()
              print(lines[line_number])
      
      print_file_line('students.txt', 1)
      
      >>> '[bill, 23, 16, 19, 22]'
      
      print_file_line('students.txt', 0)
      
      >>> '[joe, 10, 15, 20, 30, 40]'
      

      【讨论】:

      • 我试过了,我没有得到那个输出,仍然是整个数据。
      • 那么你在文件中的数据就是一长行,没有换行符。 @dante
      猜你喜欢
      • 2018-09-13
      • 2021-03-15
      • 1970-01-01
      • 2012-12-05
      • 1970-01-01
      • 2017-08-06
      • 2022-12-06
      • 2013-07-15
      • 2012-09-14
      相关资源
      最近更新 更多