-1

I have (let´s suppose only) two file_names:

file_name1 = 'stock1'
file_name2 = 'stock2'

The code below does what we expect:

for i in range(1,3,1):
    string_name = 'file_name{}'.format(i)
    print(string_name) 

Question: how should I change the string_name line above if my initial file_names are not:

file_name1 = 'stock1'
file_name2 = 'stock2'

but they are:

file_name1 = 'stock1.split('/')'
file_name2 = 'stock2.split('/)'

where the split is there for other purposes that do not present any trouble?

Thanks!

fskilnik
  • 13
  • 5
  • Where are all your filenames? Are they in a list? In a file? Also, do they all start with `C:/Users/Documents/`? – Mark May 22 '19 at 22:41
  • All filenames are exactly in the format shown in my example. Please take into account that the stock names are already being extracted successfully. The problem is with the number after the expression file_name, only. Thanks! (Now I understand your comment. I will correct the example... sorry.) – fskilnik May 22 '19 at 22:45
  • so you just want to output the filename without the forward slashes? – Vasu Deo.S May 22 '19 at 22:46
  • I could not edit my post. The real strings are like that: file_name1 = 'C:/Users/SomeName/Documents/AnotherName/PETR4_daily_22May19.csv' – fskilnik May 22 '19 at 22:47
  • Sorry if I wasn't clear, that's not exactly what I'm asking — you have all these paths — a collection a strings. How are they stored in your program? Do you have a list? -- `someList = ['C:/Users/Documents/PETR4_daily_22May19.csv', 'C:/Users/Documents/VALE3_daily_22May19.csv']`? A bunch of individual variables? Are you reading these from a file or from a directory listing? – Mark May 22 '19 at 22:48
  • Hi, Mark. Thanks for the help. It is just a sequence of lines in Jupyter notebook. There are approximately 20 file_names, that´s it! – fskilnik May 22 '19 at 22:50
  • This seems very different from your original question. What is your expected output now? Not the word preceding the underscore (e.g. 'PETR4')? – Evan May 22 '19 at 23:11

2 Answers2

1

This should work to extract the text in the filename preceding the underscore:

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)

Explanation: os.path.basename gets just the filename (without the file path), then .split('_') splits that filename on underscores, and the [0] returns the 0th (first) element of the list returned by split.

Evan
  • 1,790
  • 3
  • 23
  • 50
  • Yeah, that's better than a regex. – Mark May 22 '19 at 22:56
  • Thanks, Bird. It also worked! (I could not vote for the solutions because I still don´t have points for them...) Sorry. (Now I see I cannot put the right mark for both solutions, either. That´s a pitty.) – fskilnik May 22 '19 at 23:16
0

If you want the filenames:-

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:])

OUTPUT:-

VALE3_daily_22May19.csv
PETR4_daily_22May19.csv

Add your file_path's inside the list, and get their filenames.

If you want names of the stocks:-

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])

OUTPUT:-

VALE3
PETR4
Vasu Deo.S
  • 1,742
  • 1
  • 9
  • 23
  • Hi, Vasu. I got your idea, but I need something much more elementary... I will edit my question so that only the essential needed is presented. Sorry for the misunderstanding! – fskilnik May 22 '19 at 22:58
  • I just edited my answer, see if it works now? – Vasu Deo.S May 22 '19 at 23:00
  • Hi @Bird. Thanks for the help, but the problem was not extracting the names of the stocks, as mentioned... it was just a problem of dealing with a string with "file_name" always present, then a variable number (1, 2, ...) and then another part ".split('/')" also present in all strings... – fskilnik May 22 '19 at 23:08
  • Hi, Vasu. Perfect, thanks a lot! I will keep the question as it is now, because Prune sent a link to another very similar situation. I didn´t find that post before, by the way. – fskilnik May 22 '19 at 23:11