Search code examples
pythonstripreadlinesstartswith

Adding stripped line to string/array in Python


I am a fairly new programmer.

I am currently trying to find data from .txt files and add them to a string or array, which will then eventually be added to a .csv file.

The data I am looking at are currently present in this form, numerous times at random intervals within each .txt file:

' Line Flux: 3.0008e-19 +/- 2.6357e-21 [W/cm^2]'

Hence, after reading several ways around accessing this I have come up with a code that doesn't produce any errors, but doesn't print anything either:

cwd = os.getcwd()

def open_txt():
    flux = {}
    for file in cwd:
        if file.endswith('.txt'):
            f = open(file,'r')
            lines = f.readlines()
            for line in lines:
                if line.startswith(' Line Flux:'):
                    line.strip(' Line Flux:                        ' + '[W/cm^2]')
                    flux.append(line)
                    print flux

open_txt()

Is there anything glaringly obvious that I am doing incorrectly?

Thanks for reading. Any helpful replies would be greatly appreciated.


Solution

  • getcwd returns a string, so i think this is where your bug is. You are iterating through each letter of the string. Perhaps you need listdir.

    You may want to check this link too.


    If that is not the case you can try inserting the "print marker" and see if it opens a file at all

    cwd = os.getcwd()
    
    def open_txt():
    
        # This has to be a list, not a dict.
        flux = []
        for file in cwd:       
    
            if file.endswith('.txt'):
                # Check loop is entered, with this print marker
                print 'it opened file: %s'% file
    
                f = open(file,'r')
                lines = f.readlines()
                for line in lines:
                    if line.startswith(' Line Flux:'):
                        line.strip(' Line Flux:                        ' + '[W/cm^2]')
                        flux.append(line)
                        print flux
    
    open_txt()
    

    Also, strip is removing all characters that you provided to it. Including / : e. strip('ab'+'cz') is equivalent to strip('acbz')

    Instead you can use regular expressions:

    import re
    
    my_str = ' Line Flux: 3.0008e-19 +/- 2.6357e-21 [W/cm^2]'
    
    pattern = re.compile(r'Line Flux: (.*?)\[W/cm\^2\]')
    result = re.findall(pattern, my_str)
    
    print result
    

    Parenthesis in the pattern indicates which part of the match to be returned.