I have been trying to iterate through text files to create some text files with the same name in other directory, but with other values. Here is the code
我一直在尝试遍历文本文件,以在其他目录中创建一些名称相同的文本文件,但使用其他值。这是代码
import numpy as np
import cv2, os
from glob import glob
path_in = 'C:/Users/user/labels'
path_out = 'C:/Users/user/labels_90'
for filename in os.listdir(path_in):
if filename.endswith('txt'):
filename_edited = []
for line in filename:
numericdata = line.split(' ')
numbers = []
for i in numericdata:
numbers.append(int(i))
c,x,y = numbers
edited = [c, y, (19-x)]
filename_edited.append(edited)
filename_edited_array = np.array(filename_edited)
cv2.imwrite(os.path.join(path_out,filename),filename_edited_array)
continue
else:
continue
According to my plan, the code should access each text file, do some math with its each line, then create a text file storing the results of math. When I run the code, it raises
根据我的计划,代码应该访问每个文本文件,对每一行进行一些数学运算,然后创建一个文本文件来存储数学结果。当我运行代码时,它会上升
numbers.append(int(i))
ValueError: invalid literal for int() with base 10: 'f'
I tried to look for answers but they do not suit to this situation I think
我试图寻找答案,但他们不适合我认为的这种情况。
EDIT: I am providing text file example
编辑:我正在提供文本文件示例
0 16 6
-1 6 9
0 11 11
0 17 7
0 7 12
0 12 12
-1 19 4
1 个解决方案
#1
1
That's because with for line in filename
you're not reading the file, you are iterating over the string containing the name of the file.
这是因为在filename中,您没有读取文件,而是在包含文件名称的字符串上进行迭代。
In order to read or write to a file you have to open it (and possibly to close it at the end of the operations).
为了读取或写入文件,您必须打开它(并可能在操作结束时关闭它)。
The best and the most pythonic way to do it is to use the construct with
, which closes it automatically at the end of the operations:
最好和最python化的方法是使用构造,它在操作结束时自动关闭:
import numpy as np
import cv2, os
from glob import glob
path_in = 'C:/Users/user/labels'
path_out = 'C:/Users/user/labels_90'
for filename in os.listdir(path_in):
if filename.endswith('txt'):
filename_edited = []
# open the file in read mode
with open(filename, 'r') as f:
for line in f:
numericdata = line.split(' ')
numbers = []
for i in numericdata:
numbers.append(int(i))
# blah blah...
# blah blah...
#1
1
That's because with for line in filename
you're not reading the file, you are iterating over the string containing the name of the file.
这是因为在filename中,您没有读取文件,而是在包含文件名称的字符串上进行迭代。
In order to read or write to a file you have to open it (and possibly to close it at the end of the operations).
为了读取或写入文件,您必须打开它(并可能在操作结束时关闭它)。
The best and the most pythonic way to do it is to use the construct with
, which closes it automatically at the end of the operations:
最好和最python化的方法是使用构造,它在操作结束时自动关闭:
import numpy as np
import cv2, os
from glob import glob
path_in = 'C:/Users/user/labels'
path_out = 'C:/Users/user/labels_90'
for filename in os.listdir(path_in):
if filename.endswith('txt'):
filename_edited = []
# open the file in read mode
with open(filename, 'r') as f:
for line in f:
numericdata = line.split(' ')
numbers = []
for i in numericdata:
numbers.append(int(i))
# blah blah...
# blah blah...