简体   繁体   English

使用 Python 更改文本文件中的特定列值

[英]Change specific column values from a text file using Python

I have a text file as following:我有一个文本文件如下:

1  1  2  1  1e8
2  1  2  3  1e5
3  2  3  2  2000
4  2  5  6  1000
5  2  4  3  1e4
6  3  6  4  5000
7  3  5  2  2000
8  3  2  3  5000
9  3  4  5  1e9
10 3  2  3  1e6

My question is that how can I change one column(for example divide all the data from the last column to 900) and save the whole data in a new file?我的问题是如何更改一列(例如将最后一列的所有数据除以 900)并将整个数据保存在新文件中? Thanks in advance提前致谢

for each line of your file,对于文件的每一行,

    vals = line.split(' ')
    my_val = float(vals[4])/900
    output  = open('filename', 'wb')
    output.write(my_val +'\n')

You can use numpy library.您可以使用 numpy 库。 The code below should do it.下面的代码应该可以做到。

import numpy as np

# assume the data is in.txt file
dat = np.loadtxt('in.txt')

# -1 means the last column
dat[:, -1] = dat[:, -1]/900

# write the result to the out.txt file
# fmt is the format while writing to the file
# %.2f means that it will save it to the 2 digits precision
np.savetxt('out.txt', dat, fmt='%.2f')

Using a simple iteration.使用简单的迭代。

res = []
with open(filename, "r") as infile:
    for line in infile:                        # Iterate each line 
        val = line.split()                     # Split line by space
        res.append( str(float(val[-1])/900) )  # Use negative index to get last element and divide.

with open(filename, "w") as outfile:          #Open file to write
    for line in res:
        outfile.write(line+"\n")              #Write Data

You can try the following:您可以尝试以下操作:

import numpy as np
#Suppose your text data name is "CH3CH2OH_g.txt"
file = np.loadtxt('CH3CH2OH_g.txt')

z=  (file[:, -1]/900)
np.savetxt('LogCH3CH2OH.txt', file, fmt='%.2f')

暂无
暂无

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

相关问题 使用Python从文本文件读取特定的列值 - Read specific column value from a text file using Python 从大文本文件中删除特定项目(0 值和乘以 *0 的值)并使用 Python 将其写入新的文本文件 - Remove specific items (0 values and values multiplied by *0) from a large text file and write it to a new text file using Python 使用python更改CSV文件中的列的值 - Change values of a column in CSV file using python 如何 append 使用另一个特定列作为 Python 中的索引,从目录中的每个文件中获取一列值 - How to append a column of values from each file in directory using another specific column as an index in Python 如何使用python更改文本文件中特定键的特定值 - How to change a specific value of a specific key in text file using python 使用python csv根据csv文件中特定列的不同值打印与另一列中的最小值相关的所有行 - Print all rows related to minimum values from another column based on distinct values of a specific column from csv file using python csv 使用 Python 从文本文件中获取值 - Get values from a text file using Python 使用python在csv文件中以特定方式添加列值 - to add column values in a specific manner in a csv file using python 尝试从python中的列.csv文件打印特定值 - Trying to print specific values from a column .csv file in python Python,Pandas。 从特定列创建文本文件 - Python, Pandas. create a text file from a specific column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM