我有(仅假设)两个文件名:
file_name1 = 'stock1'
file_name2 = 'stock2'
下面的代码符合我们的期望:
for i in range(1,3,1):
string_name = 'file_name{}'.format(i)
print(string_name)
问题:如果我的初始文件名不是,我应该如何更改上面的string_name行:
file_name1 = 'stock1'
file_name2 = 'stock2'
但是它们是:
file_name1 = 'stock1.split('/')'
file_name2 = 'stock2.split('/)'
为了其他目的而存在拆分的地方而不会造成任何麻烦?
谢谢!
答案 0 :(得分:1)
这应该可以提取下划线之前的文件名中的文本:
c(if (T) list(a=1), if (T) list(b=2), if (T) list(d=4))
# $a
# [1] 1
# $b
# [1] 2
# $d
# [1] 4
c(if (T) list(a=1), if (FALSE) list(b=2), if (T) list(d=4))
# $a
# [1] 1
# $d
# [1] 4
说明:UPDATE `Pipas` SET `bolStatus`= (SELECT IF(NEW.progreso = NEW.campos_totales, '1', '0'))
WHERE Pipas.pipa_id = NEW.pipa_id
仅获取文件名(不包含文件路径),然后import os
files = ['C:/Users/Documents/PETR4_daily_22May19.csv', 'C:/Users/Documents/VALE3_daily_22May19.csv']
for file in files:
x = os.path.basename(file).split('_')[0]
print(x)
在下划线处拆分该文件名,而os.path.basename
返回列表的第0个(第一个)元素通过拆分返回。
答案 1 :(得分:0)
如果需要文件名:-
list_containing_filenames = ['C:/Users/Documents/VALE3_daily_22May19.csv', 'C:/Users/Something/Documents/AnotherSomething/PETR4_daily_22May19.csv']
for x in list_containing_filenames:
print(*"{}".format(x).split('/')[-1:])
输出:-
VALE3_daily_22May19.csv
PETR4_daily_22May19.csv
在列表中添加您的file_path,并获取其文件名。
如果需要股票名称:-
list_containing_filenames = ['C:/Users/Documents/VALE3_daily_22May19.csv', 'C:/Users/Something/Documents/AnotherSomething/PETR4_daily_22May19.csv']
for x in list_containing_filenames:
print("".join("{}".format(x).split('/')[-1:]).split("_")[0])
输出:-
VALE3
PETR4