Extract substring from list of file names in Python or R

Ursus Frost

My question is very similar to the following: How to get a Substring from list of file names. I'm a newb to Python and would prefer a similar solution for Python (or R). I'd like to look into a directory and extract a particular substring from each applicable file name and output it as a vector (preferred), list, or array. For example, assume I have directory with the following file names:

data_ABC_48P.txt
data_DEF_48P.txt
data_GHI_48P.txt
other_96.txt
another_98.txt

I would like to reference the directory and extract the following as a character vector (for use in R) or list:

"ABC", "DEF", "GHI"

I tried the following:

from os import listdir
from os.path import isfile, join
files = [ f for f in listdir(path) if isfile(join(path,f)) ]
import re
m = re.search('data_(.+?)_48P', files)

But I get the following error:

TypeError: expected string or buffer

files is of type list

In [10]: type(files)
Out[10]: list

Even though I ultimately want this character vector as an input to R code, we are trying to transition all of our "scripting" to Python and use R solely for data analysis, so a Python solution would be great. I'm also using Ubuntu, so a cmd line or bash script solution could work as well. Thanks in advance!

Avinash Raj

Use List comprehension like,

[re.search(r'data_(.+?)_48P', i).group(1) for i in files if re.search(r'data_.+?_48P', i)]

You need to iterate over the list contents inorder to grab the substrings you want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Extract substring from list of file names in Python or R

From Dev

Extract substring from 'list' of strings

From Dev

How to extract a dynamic substring from a list of strings in Python?

From Dev

extract substring from string, python

From Dev

Extract substring from filename in Python?

From Dev

Extract Substring from String Python

From Dev

Rename names from a list with file names without the ".csv" in R

From Dev

Reliably extract names of R functions from a text file

From Dev

How to extract sheet names from Excel file in R

From Dev

Extract subset from several file names using python

From Dev

Extract subset from several file names using python

From Dev

Extract multiple occurrences of a Substring from a text file

From Dev

Python, string slicing (getting file names from a list of file locations)

From Dev

how to extract a particular substring from a string in python

From Dev

Get a list of file names from HDFS using python

From Dev

list file names from a folder to a tkinter window, with python 3

From Dev

list file names from a folder to a tkinter window, with python 3

From Dev

Extract names from File using Ruby and Grep

From Dev

Extract Columns names from XML file

From Dev

VBA, extract file names from unformatted sheet

From Dev

Extract domain names from a file in Shell

From Dev

How can I sort a list of file names by some substring of the name?

From Dev

How can I sort a list of file names by some substring of the name?

From Java

Extract value from a list formatted like a JSON file in Python

From Dev

how to extract images from file and put them in a list using python

From Dev

In R, How to remove some unwanted charaters from the CSV file names and also extract dates?

From Dev

Python to print string from substring from list

From Dev

Python to print string from substring from list

From Dev

Extract a substring in R

Related Related

  1. 1

    Extract substring from list of file names in Python or R

  2. 2

    Extract substring from 'list' of strings

  3. 3

    How to extract a dynamic substring from a list of strings in Python?

  4. 4

    extract substring from string, python

  5. 5

    Extract substring from filename in Python?

  6. 6

    Extract Substring from String Python

  7. 7

    Rename names from a list with file names without the ".csv" in R

  8. 8

    Reliably extract names of R functions from a text file

  9. 9

    How to extract sheet names from Excel file in R

  10. 10

    Extract subset from several file names using python

  11. 11

    Extract subset from several file names using python

  12. 12

    Extract multiple occurrences of a Substring from a text file

  13. 13

    Python, string slicing (getting file names from a list of file locations)

  14. 14

    how to extract a particular substring from a string in python

  15. 15

    Get a list of file names from HDFS using python

  16. 16

    list file names from a folder to a tkinter window, with python 3

  17. 17

    list file names from a folder to a tkinter window, with python 3

  18. 18

    Extract names from File using Ruby and Grep

  19. 19

    Extract Columns names from XML file

  20. 20

    VBA, extract file names from unformatted sheet

  21. 21

    Extract domain names from a file in Shell

  22. 22

    How can I sort a list of file names by some substring of the name?

  23. 23

    How can I sort a list of file names by some substring of the name?

  24. 24

    Extract value from a list formatted like a JSON file in Python

  25. 25

    how to extract images from file and put them in a list using python

  26. 26

    In R, How to remove some unwanted charaters from the CSV file names and also extract dates?

  27. 27

    Python to print string from substring from list

  28. 28

    Python to print string from substring from list

  29. 29

    Extract a substring in R

HotTag

Archive