通过在文件内部进行数学运算来编辑文本文件
Editing a text file by doing math operation inside the file
我有一个这样的文本文件 (text.txt):
50 10 15 20 25
40 30 35 40 45
30 50 55 60 65
我已将此文件编辑如下:
lines = [line.rstrip('\n') for line in open('text.txt')]
output = open('a.txt', 'w')
for line in lines:
line = line.split()
output.write('Values({},{},{},{},{});\n'.format(line[0], line[1],
line[2], line[3], line[4]))
output.close()
这是新的文本文件:
Values(50,10,15,20,25);
Values(40,30,35,40,45);
Values(30,50,55,60,65);
现在我想通过以下方式对其组件进行数学运算来编辑文件(原始 text.txt):
我想从第一个中的所有组件中减去 10
列
我想从剩下的所有分量中减去 1
列
更具体地说,我正在看这个:
Values(40,9,14,19,24);
Values(30,29,34,39,44);
Values(20,49,54,59,64);
如何在这个文本文件中实现这个简单的数学运算来得到上面的结果作为输出?
谢谢!
试试这个:
lines = [line.rstrip('\n') for line in open('text.txt')]
output = open('a.txt', 'w')
for line in lines:
line = line.split()
# subtract 10 from all of the components in the first column
line[0] = int(line[0]) - 10
# subtract 1 from all of the components in the rest of the columns
line[1:] = [int(n) - 1 for n in line[1:]]
output.write('Values({},{},{},{},{});\n'.format(*line))
output.close()
您似乎正在读取 CSV 文件。使用pandas库处理文件会容易很多。
import pandas as pd
# read the CSV file with space as delimiter
df = pd.read_csv('the.txt', delimiter=' ', header = None)
df.head()
# there are two spaces, so a the number of columns is twice
# remove the columns with NaN values
df = df.dropna(axis=1, how='all')
df.head()
# subtract the column one by 10
df[0] = df[0].apply(lambda x: x - 10)
df.head()
# subtract the rest of the columns by 1
df[[2,4,6,8]] = df[[2,4,6,8]].apply(lambda x: x -1)
df.head()
# convert the dataframe to tuples
records = [tuple(x) for x in df.to_records(index=False)]
# Write the records to the the file
with open('a.txt','w') as outfile:
for record in records:
outfile.write('Values{};\n'.format(record))
我有一个这样的文本文件 (text.txt):
50 10 15 20 25
40 30 35 40 45
30 50 55 60 65
我已将此文件编辑如下:
lines = [line.rstrip('\n') for line in open('text.txt')]
output = open('a.txt', 'w')
for line in lines:
line = line.split()
output.write('Values({},{},{},{},{});\n'.format(line[0], line[1],
line[2], line[3], line[4]))
output.close()
这是新的文本文件:
Values(50,10,15,20,25);
Values(40,30,35,40,45);
Values(30,50,55,60,65);
现在我想通过以下方式对其组件进行数学运算来编辑文件(原始 text.txt):
我想从第一个中的所有组件中减去 10 列
我想从剩下的所有分量中减去 1 列
更具体地说,我正在看这个:
Values(40,9,14,19,24);
Values(30,29,34,39,44);
Values(20,49,54,59,64);
如何在这个文本文件中实现这个简单的数学运算来得到上面的结果作为输出? 谢谢!
试试这个:
lines = [line.rstrip('\n') for line in open('text.txt')]
output = open('a.txt', 'w')
for line in lines:
line = line.split()
# subtract 10 from all of the components in the first column
line[0] = int(line[0]) - 10
# subtract 1 from all of the components in the rest of the columns
line[1:] = [int(n) - 1 for n in line[1:]]
output.write('Values({},{},{},{},{});\n'.format(*line))
output.close()
您似乎正在读取 CSV 文件。使用pandas库处理文件会容易很多。
import pandas as pd
# read the CSV file with space as delimiter
df = pd.read_csv('the.txt', delimiter=' ', header = None)
df.head()
# there are two spaces, so a the number of columns is twice
# remove the columns with NaN values
df = df.dropna(axis=1, how='all')
df.head()
# subtract the column one by 10
df[0] = df[0].apply(lambda x: x - 10)
df.head()
# subtract the rest of the columns by 1
df[[2,4,6,8]] = df[[2,4,6,8]].apply(lambda x: x -1)
df.head()
# convert the dataframe to tuples
records = [tuple(x) for x in df.to_records(index=False)]
# Write the records to the the file
with open('a.txt','w') as outfile:
for record in records:
outfile.write('Values{};\n'.format(record))