【问题标题】:How to select specific lines and re write it on a single file?如何选择特定行并将其重新写入单个文件?
【发布时间】:2020-10-05 13:41:40
【问题描述】:

我有一个.txt文件,里面有一个实验的电压、电流和辐射讲座的一些数据,显示如下。

我真的是 Python 的新手,所以我想知道是否有任何函数或库可以帮助我只选择一些行,特别是那些显示在第二行的值为正且高于 let's说,10,然后将其重新写入一个新文件。

此外,如果您能向我推荐任何文学作品以深入了解它,我将不胜感激。提前谢谢!

【问题讨论】:

    标签: python json pandas csv dataset


    【解决方案1】:

    你可以使用 readlines()

    with open('file_name','r') as f:
        lines = f.readlines() #this will create a list with each element as a line
    
    with open('file2','w') as f2:
        f2.write(lines[0]) #this will write 1st line to a new file
        f2.writelines(lines) #this will write all the lines in new file
    

    没有什么比official documentation更适合学习了,
    如果这还不够check it out

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 1970-01-01
      • 2021-07-11
      • 2021-07-03
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      相关资源
      最近更新 更多