Skip to content Skip to sidebar Skip to footer

How To Get Only A Part From A String In Python

I have a list with the following strings: total 71708 -rw-rw-rw- 1 gpatwprd tmwdprd 221 Nov 19 12:36 20181116.Something name.6781773.CSV -rw-rw-rw- 1 gpatwprd

Solution 1:

The following code will print a list of all the required entries and also a list that has only names of required files. 'data.txt' is the file which has the data to be used. Hope this helps!

fileContent = open('data.txt','r');
readLinesfromContent = fileContent.readlines();

splitLinesforSpace = []
for line in readLinesfromContent:
    splitLinesforSpace.append(line.split())

requiredEntries = []
namesofRequiredFiles = []
for idx, x in enumerate(splitLinesforSpace):
    if len(x) == 10:
        if x[8].startswith('20181116'):
            if x[9].endswith('.CSV') or x[9].endswith('.XLSX'):
                requiredEntries.append(readLinesfromContent[idx])
                x[9] = x[9].split('.')
                #Following gives only names of files
                namesofRequiredFiles.append(x[9][0])

print(requiredEntries)
print(namesofRequiredFiles)

Post a Comment for "How To Get Only A Part From A String In Python"