Python Forum
Having trouble with a string...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Having trouble with a string...
#1
I have written the contents of a dictionary to a text file. Each key stores a list. I'm having trouble reading the list, however. I only want what was the second item on the list. Here's some code and the contents of the text file that the script reads from:
def write_dumps(self):
        dmp = self.scan_dumps()
        i = 0
        with open('practice_gui_2.ini', 'w') as f:
            while i < len(dmp):
                s = str(dmp[i])
                f.write(f'{s}\n')
                i += 1

    def get_size(self):
        '''Reads fro  Ini created by "scan_dumps()" and gets total size
        of all .DMP files to display in the GUI.'''
        with open('practice_gui_2.ini', 'r') as f:
            s = f.readline()
            l = s.find(',')
            ...
['C:\\Games\\Steam\\dumps\\assert_steam.exe_20181011101734_1.dmp', 451628]
['C:\\Users\\Mark\\AppData\\Local\\CrashDumps\\StaxRip.exe.1008.dmp', 23099764]
['C:\\Users\\Mark\\AppData\\Local\\CrashDumps\\StaxRip.exe.2520.dmp', 22764743]
['C:\\Users\\Mark\\AppData\\Local\\CrashDumps\\StaxRip.exe.3180.dmp', 22698987]
['C:\\Users\\Mark\\AppData\\Local\\CrashDumps\\StaxRip.exe.3800.dmp', 23833275]
['C:\\Users\\Mark\\AppData\\Local\\CrashDumps\\StaxRip.exe.4268.dmp', 24042924]
['C:\\Users\\Mark\\AppData\\Local\\CrashDumps\\StaxRip.exe.6068.dmp', 22672263]
['C:\\Users\\Mark\\AppData\\Local\\CrashDumps\\StaxRip.exe.6204.dmp', 23633874]
['C:\\Users\\Mark\\AppData\\Local\\CrashDumps\\StaxRip.exe.6500.dmp', 21915672]
['C:\\Users\\Mark\\AppData\\Local\\CrashDumps\\StaxRip.exe.7360.dmp', 22779161]
['C:\\Users\\Mark\\AppData\\Local\\CrashDumps\\StaxRip.exe.8052.dmp', 22445275]
['C:\\Windows\\System32\\config\\systemprofile\\AppData\\Local\\CrashDumps\\lsass.exe.796.dmp', 844132]


I can't figure out how to find the comma and then read only the numbers after the comma. I don't want the brackets or the spaces.
Thanks in advance for any help.

-malonn
Reply
#2
I think I have it figured. Here's what works for me if anyone is interested and if anyone can see room for improvement:
with open('practice_gui_2.ini', 'r') as f:
            tsize = 0
            for line in f:
                print(line)
                lc = line.find(',')
                stn = ''
                for d in line[lc + 2::]:
                    if d.isdigit():
                        stn += d
                tsize += int(stn)
        print(tsize)
Reply
#3
You can use join to make the code simpler:

stn = ''.join([char.isdigit() for char in line[lc + 2:]])
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
with open('practice_gui_2.ini', 'r') as f:
    tsize = 0
    for line in f:
        data = line.split(",")
        print data[1]
Reply
#5
(Oct-16-2018, 02:53 PM)ichabod801 Wrote: You can use join to make the code simpler:

stn = ''.join([char.isdigit() for char in line[lc + 2:]])
Gives the following error TypeError: sequence item 0: expected str instance, bool found

(Oct-16-2018, 02:57 PM)marienbad Wrote:
with open('practice_gui_2.ini', 'r') as f:
    tsize = 0
    for line in f:
        data = line.split(",")
        print data[1]
Thanks, but that approach keep leading whitespace and trailing bracket.
Reply
#6
Just add:

dd = data[1]
		
		print dd[1:-2]
at the end to remove them
Reply
#7
Well, sorry. That was bad.

stn = ''.join([char for char in line[lc + 2:] if char.isdigit()])
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(Oct-16-2018, 05:03 PM)marienbad Wrote: Just add:

dd = data[1]
		
		print dd[1:-2]
at the end to remove them
Works like a charm. Thanks.

(Oct-16-2018, 06:37 PM)ichabod801 Wrote: Well, sorry. That was bad.

stn = ''.join([char for char in line[lc + 2:] if char.isdigit()])
Works like a charm as well, thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 386 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Trouble converting JSON String to Dictionary RBeck22 7 5,064 Mar-28-2019, 12:12 PM
Last Post: RBeck22

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020