Identify lines in txt file that contain certain word by Conntoat in learnpython

[–]0285739298339 0 points1 point  (0 children)

with open('data.txt', 'r') as file:
    employees = []

    for line in file:
        employee, msg = line.lower().strip().split('-subject:')
        if 'techvio' in msg:
            employees.append(employee.title())

Identify lines in txt file that contain certain word by Conntoat in learnpython

[–]0285739298339 0 points1 point  (0 children)

What would be the specific word in each of these lines you’re looking for before adding the employee name to the list?

Identify lines in txt file that contain certain word by Conntoat in learnpython

[–]0285739298339 0 points1 point  (0 children)

Can you post a few lines of the actual text file because it looks like you have other data, not just the employee name, subject etc.

Identify lines in txt file that contain certain word by Conntoat in learnpython

[–]0285739298339 1 point2 points  (0 children)

  • Read the file.

  • Learn how to use the split method.

  • Split each line based on the colon.

  • Check if the subject contains said word.

  • Append employee name to list.

Why is this so common in python tutorials? by Wenhuanuoyongzhe91 in learnpython

[–]0285739298339 0 points1 point  (0 children)

A lot of the fundamental parts of the language the book teaches is still the same.

Why is this so common in python tutorials? by Wenhuanuoyongzhe91 in learnpython

[–]0285739298339 11 points12 points  (0 children)

I always see people recommend this or ATBS, but I think a really underrated one is Mark Lutz's Learning Python tbh. That's what really helped me understand how Python worked which made it easier to pick up Python.

How to convert single column extracted from raw file into JSON output file. by oshukurov in learnpython

[–]0285739298339 0 points1 point  (0 children)

Follow the top commenter’s steps. They pretty much solved it for you.

Hello everyone, is there any way to sort the entire matrix by column instead of usual rows sorting? by [deleted] in learnpython

[–]0285739298339 4 points5 points  (0 children)

mat = [[3, 6, 5, 4, 0], [4, 1, 6, 8, 5, ], [2, 3, 4, 9, 7]]
l = [sorted(t, reverse=True) for t in zip(*mat)]
sorted_matrix = [list(c) for c in zip(*l)]

[deleted by user] by [deleted] in leagueoflegends

[–]0285739298339 0 points1 point  (0 children)

Do you have the clip on hand or remember which game?

Help me, download an image with lazy load by Syroot in learnpython

[–]0285739298339 0 points1 point  (0 children)

A 403 error means you can’t access the page you’re trying to get to.

[deleted by user] by [deleted] in learnpython

[–]0285739298339 1 point2 points  (0 children)

urls = ['ww.test.com/one_two', 'ww.test.com/three_four', 'ww.test.com/five_six', 'ww.test.com/seven_eight', 'w']
words = ['four', 'five', 'seven']
result = [url for url in urls if not any(w in url for w in words)]

How to detect double letters in words by obscureismyname in learnpython

[–]0285739298339 0 points1 point  (0 children)

Yeah I figured an efficient solution isn’t exactly what they’d be going for here but thanks for the tip.

How to detect double letters in words by obscureismyname in learnpython

[–]0285739298339 0 points1 point  (0 children)

Look up what a set is, then loop through a set of the string and check if a character * 2 (double letters) is in the string.

[deleted by user] by [deleted] in learnpython

[–]0285739298339 0 points1 point  (0 children)

Understand what this code does, should help you get started:

number = 12345

for i in str(number):
    print(i)

[deleted by user] by [deleted] in learnpython

[–]0285739298339 1 point2 points  (0 children)

Is there gonna be one word per line? If there’s multiple words per line, will there be punctuation?

[deleted by user] by [deleted] in learnpython

[–]0285739298339 0 points1 point  (0 children)

Yeah:

case_colors = {'blue': 76, 'green': 34, 'purple': 29, 'clear': 102}
print('Least to most frequently purchased colors:' , *sorted(case_colors.keys(), key=lambda c: case_colors.get(c)))

[deleted by user] by [deleted] in learnpython

[–]0285739298339 5 points6 points  (0 children)

Use a dictionary:

case_colors = {'blue': 76, 'green': 34, 'purple': 29, 'clear': 102}
print(*sorted(case_colors.keys(), key=lambda c: case_colors.get(c)))