我有两个文件
文件1:
0,1,2,3,4,5,6,7,8,9,7585,10,11
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,14,15
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,13,14,15,16,17,18,19,20
0,1,2,3,4,5,6,7,8,9,10,11
文件2:
0,1,2,3,4,5
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,14,15,16,17,18
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,13,14,15,16,17,18,19,20,21,22
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
我正在尝试将索引10=7585
的所有行导出到Excel,
不管行的长度如何
这是我的代码:
my_path = r'c:\data\EKL\Desktop\my-files'
for file in os.listdir(my_path):
path_file = os.path.join(my_path, file)
with open(path_file, 'r') as output:
reader = csv.reader(output, delimiter = ',')
read = [row for row in reader if row]
for row in read:
if row[10] == '7585':
print(','.join(row[0:]))
这是我的结果:
Traceback (most recent call last):
File "C:\data\EKL\Desktop\Python\PythongMySQL\untitled2.py", line 14, in <module>
if row[10] == '7585':
IndexError: list index out of range
这是我在EXCEL工作表中想要的结果:
0,1,2,3,4,5,6,7,8,9,7585,10,11
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,14,15
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,13,14,15,16,17,18,19,20
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,14,15,16,17,18
0,1,2,3,4,5,6,7,8,9,7585,10,11,12,13,14,15,16,17,18,19,20,21,22
没有检查它是否正在运行,但我会尝试以下方法:
这对我很有用:
输出
更新-使用Excel导出
相关问题 更多 >
编程相关推荐