【发布时间】:2018-09-05 16:37:34
【问题描述】:
我一直在尝试遍历文本文件以在其他目录中创建一些具有相同名称但具有其他值的文本文件。这是代码
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
根据我的计划,代码应该访问每个文本文件,对其每一行进行一些数学运算,然后创建一个存储数学结果的文本文件。 当我运行代码时,它会引发
numbers.append(int(i))
ValueError: invalid literal for int() with base 10: 'f'
我试图寻找答案,但我认为它们不适合这种情况
编辑:我提供文本文件示例
0 16 6
-1 6 9
0 11 11
0 17 7
0 7 12
0 12 12
-1 19 4
【问题讨论】:
-
你
filename中的数据都是数字吗?还是里面也有文字?似乎您正在将文本转换为 int -
@Abdullah 它们都是数字,包括正整数和负整数。
filename由数字组成,但是当我读取filename的每一行时,它被读取为字符串,这就是您提供numericdata = line.split(' ') numbers = [] for i in numericdata: numbers.append(int(i))的原因 -
你可以在这里发布
filename列表/数据吗?还是执行该拆分操作后的 numericdata 列表? -
@AbdullahAhmedGhaznavi 我编辑了问题
-
我已经检查了所有这些数字的循环,它不会给我错误。通过看到你的错误,我会建议你调试你的文件名循环并检查
f在列表中的位置,它在 int 转换时给你错误
标签: numpy valueerror opencv-python