简体   繁体   English

使用python向列添加数字

[英]Adding a number to a column with python

I am very new to python and I would be grateful for some guidance with the following. 我是python的新手,我将非常感谢以下的一些指导。 I have a text file with over 5 million rows and 8 columns, I am trying to add "15" to each value in column 4 only. 我有一个超过500万行和8列的文本文件,我试图只为第4列中的每个值添加“15”。

For example: 例如:

  10  21  34  12  50  111  234  21  7
  21  10  23  56  80   90  221  78 90

Would be changed to: 将改为:

  10  21  34  12  **65**  111  234  21  7
  21  10  23  56  **95**   90  221  78 90

My script below allows me to isolate the column, but when I try to add any amount to it i return "TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'" 我下面的脚本允许我隔离列,但当我尝试向其添加任何数量时,我返回“TypeError:不支持的操作数类型为+:'NoneType'和'int'”

file = open("file.txt")
column = []

for line in file:
    column.append(int(line.split("\t")[3]))

print column

Any advice would be great. 任何建议都会很棒。

try this to get you started -- there are many better ways using libraries but this will show you some better file handling basic methods anyway. 试试这个让你入门 - 有许多更好的方法使用库,但这会向你展示一些更好的文件处理基本方法。 works for the data you posted -- as long as the delimiter in your files is double space (" ") and that everything can be cast to an int. 适用于您发布的数据 - 只要文件中的分隔符是双倍空格(“”)并且所有内容都可以强制转换为int。 If not..... 如果不.....

Also -- note the correct way to start a script is with: 另外 - 请注意启动脚本的正确方法是:

if __name__ == "__main__":

this is because you wont generally want any code to execute if you are making a library... 这是因为如果要创建库,通常不需要执行任何代码...

__author__ = 'charlie'

in_filename = "in_file.txt"
out_filename = "out_file.txt"
delimiter = "  "

def main():

    with open(in_filename, "r") as infile:
        with open(out_filename, "w") as outfile:
            for line in infile:

                ldata = line.split(delimiter)

                ldata[4] = str(int(ldata[4]) + 15)

                outfile.write(delimiter.join(ldata))


if __name__ == "__main__":
    main()

With Pandas : 与熊猫:

import pandas as pd

df = pd.read_clipboard(header=None)
df[4] += 15

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM