【问题标题】:I want to read file and write another file. Basically, I want to do some arithmetic and write few other columns我想读取文件并写入另一个文件。基本上,我想做一些算术并写一些其他的专栏
【发布时间】:2016-12-06 20:14:16
【问题描述】:

我有一个类似的文件

2.0 4 3
0.5 5 4
-0.5 6 1
-2.0 7 7
.......

实际文件很大

我想阅读并添加几列,第一列,column(4) = column(2) * column(3),第二列添加将是column 5 = column(2)/column(1) + column(4),所以结果应该是

2.0 4 3 12 14
0.5 5 4 20 30
-0.5 6 1 6 -6
-2.0 7 7 49 45.5
.....

我想写在不同的文件中。

with open('test3.txt', encoding ='latin1') as rf: 
     with open('test4.txt', 'w') as wf:
        for line in rf:
            float_list= [float(i) for i in line.split()]
            print(float_list)

但到目前为止,我只有这个。我只能创建列表,但不确定如何在列表上执行算术并创建新列。我想我完全不在这儿了。我只是python的初学者。任何帮助将不胜感激。谢谢!

【问题讨论】:

  • 您在行尾有新值,因此将新值附加到列表float_list.append()。稍后使用' '.join() 生成行。或者使用csv模块,空格作为分隔符。
  • @juanpa.arrivillaga:你建议我怎么做?我对 python 以及一般编程都很陌生。如果你能具体一点,那将非常有帮助。谢谢。

标签: python-3.x file-io


【解决方案1】:

我会重用您的公式,但会移动索引,因为它们在 python 中从 0 开始。 我会用新的计算扩展读取的 column 浮点列表,然后写回行,空格分隔(在列表理解中转换回 str

所以,循环的内部可以写成:

with open('test3.txt', encoding ='latin1') as rf:
     with open('test4.txt', 'w') as wf:
        for line in rf:    
           column= [float(i) for i in line.split()]  # your code
           column.append(column[1] * column[2])  # add column
           column.append(column[1]/column[0] + column[3])  # add another column
           wf.write(" ".join([str(x) for x in column])+"\n")  # write joined  strings, separated by spaces

【讨论】:

  • 法布尔:感谢您的回复。这工作得很好。我遇到的唯一问题是它会跳过第一行。你想对此发表评论吗?再次感谢!
  • 这将消耗第一行和第二行。有没有办法不消耗任何线?
  • 对不起,我看错了。我不明白为什么它会消耗第一行:打开文件,迭代行。它不会跳过任何行。这不合逻辑。再次检查您的输入文件。
  • 我发现了我的错误。很抱歉浪费您的时间。我在代码本身中有两次“for line in rf:”。谢谢你的帮助!!!
【解决方案2】:

类似的东西 - 见代码中的 cmets

with open('test3.txt', encoding ='latin1') as rf: 
     with open('test4.txt', 'w') as wf:
        for line in rf:
            float_list = [float(i) for i in line.split()]

            # calculate two new columns
            float_list.append(float_list[1] * float_list[2])
            float_list.append(float_list[1]/float_list[0] + float_list[3])

            # convert all values to text
            text_list =  [str(i) for i in float_list]

            # concatente all elements and write line
            wf.write(' '.join(text_list) + '\n')

【讨论】:

    【解决方案3】:

    尝试以下方法:

    map() 用于将列表中的每个元素转换为float,最后再次用于将每个float 转换为str,以便我们将它们连接起来。

    with open('out.txt', 'w') as out: 
        with open('input.txt', 'r') as f:
            for line in f:
                my_list = map(float, line.split())
                my_list.append(my_list[1]*my_list[2])
                my_list.append(my_list[1] / my_list[0] + my_list[3])
                my_list = map(str, my_list)
                out.write(' '.join(my_list) + '\n')
    

    【讨论】:

    • 在 python 3 中不起作用,因为map 返回的是一个迭代器,而不是一个列表。
    • @Jean-FrançoisFabre Python 3 未在此问题中标记。如果代码是 Python 3,我认为 OP 应该指定。
    猜你喜欢
    • 2019-09-12
    • 2022-01-14
    • 2020-12-28
    • 2021-01-23
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多