【问题标题】:Storing lines into array and print line by line将行存储到数组中并逐行打印
【发布时间】:2020-10-25 03:37:07
【问题描述】:

当前代码:

filepath = "C:/Bg_Log/KLBG04.txt"
with open(filepath) as fp:
    lines = fp.read().splitlines()
    with open(filepath, "w") as fp:
        for line in lines:
            print("KLBG04",line,line[18], file=fp)

输出:

KLBG04 20/01/03 08:09:13 G0001 G

需要灵活地移动列并使用数组或列表操作如下所示的日期

KLBG04 03/01/20 G0001 G 08:09:13

【问题讨论】:

    标签: python arrays sorting readlines


    【解决方案1】:

    您没有提供示例数据,但我认为这可能有效:

    filepath = "C:/Bg_Log/KLBG04.txt"
    with open(filepath) as fp:
        lines = fp.read().splitlines()
        with open(filepath, "w") as fp:
            for line in lines:
                ln = "KLBG04 " + line + " " + line[18]  # current column order
                sp = ln.split()  # split at spaces
                dt = '/'.join(sp[1].split('/')[::-1])  # reverse date
                print(sp[0],dt,sp[3],sp[-1],sp[-2]) # new column order
                # print("KLBG04",line,line[18], file=fp)
    

    【讨论】:

    • 谢谢@mike67。该代码对我来说非常有效,但是由于我是编程新手,所以我不能发表太多评论
    【解决方案2】:

    先尝试split() 行,然后按所需顺序打印列表

    from datetime import datetime # use the datetime module to manipulate the date
    
    filepath = "C:/Bg_Log/KLBG04.txt"
    with open(filepath) as fp:
        lines = fp.read().splitlines()
        with open(filepath, "w") as fp:
            for line in lines:
                date, time, venue = line.split(" ") # split the line up
                date = datetime.strptime(date, '%y/%m/%d').strftime('%d/%m/%y') # format your date
                print("KLBG04", date, venue, venue[0], time, file=fp) # print in your desired order
    

    【讨论】:

      【解决方案3】:

      为什么不将输出本身存储为字符串并使用split()方法在每个空格处拆分字符串,然后对索引1(将包含日期的索引)使用另一种拆分方法并拆分在每个/ 再次调用它(这样你就可以操纵日期)。

      
      for line in lines:
              String output ="KLBG04",line,line[18], file=fp   # Rather than printing store the output in a string #
      x = output.split(" ")
      date_output = x[1].split("/")
      # Now you can just manipulate the data around and print how you want to #
      
      

      【讨论】:

      • 我建议无论何时处理时间,使用日期和时间操作模块,例如datetime,以获得更简洁、更易理解的代码。
      • 是的,这将是解决此问题的更好方法
      【解决方案4】:

      试试这个 `

      for line in lines:
          words = line.split()  # split every word
          date_values = words[0].split('/') # split the word that contains date
          
          #create a dictionary as follows
          date_format = ['YY','DD','MM']
          date_dict = dict(zip(date_format, date_values))
          
          #now create a new variable with changed format
          new_date_format = date_dict['MM'] + '/' + date_dict['DD'] + '/' + date_dict['YY']
          print(new_date_format)
          
          #replace the first word [index 0 is having date] with new date format
          words[0] = new_date_format
          
          #join all the words to form a new line
          new_line = ' '.join(words)
          print("KLBG04",new_line,line[18])
      

      `

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-09
        • 1970-01-01
        相关资源
        最近更新 更多