-
-
Save MichelleDalalJian/4d630b054e647b2d61a5ed9bcc385f10 to your computer and use it in GitHub Desktop.
import re | |
hand = open("regex_sum_24962.txt") | |
x=list() | |
for line in hand: | |
y = re.findall('[0-9]+',line) | |
x = x+y | |
sum=0 | |
for z in x: | |
sum = sum + int(z) | |
print(sum) |
Actual data: http://py4e-data.dr-chuck.net/regex_sum_1867584.txt (There are 121 values and the sum ends with 650)
Need help for this question, sum ends with 650
Hey guys, I've been giving a try to the problem and I got with this solution. Hope it can run without a problem :D
#practice file import re
user=input('Enter a file: ') try: file=open(user) except: print('File cannot be open') exit()
x=[]
sum=0
for line in file:
line=line.rstrip()
num=re.findall('[0-9]+', line)
for n in num:
sum=sum + int(n)
print(sum)
Actually, i´ve got some problems with this exercise (don´t know why but from the beginning it was a problem to make MS/DOS read it properly), here I got an alternative solution that gives me the exact sum
import re file_name=r'-Put Acces Route-' with open(file_name,'r', encoding='utf-8') as f: x=list() for line in f: y = re.findall('([0-9]+)',line) x = x+y sum=0 for z in x: sum = sum + int(z) print(sum)
i added a parenthesis on the re.findall expression and it worked perfectly
Just run the code in Colab and upload your txt file by copy the content and save it as txt and call it in to the open function you should get your answer
import re
sum = 0
file = open('regex_sum_97406', 'r')
for line in file:
numbers = re.findall('[0-9]+', line)
if not numbers:
continue
else:
for number in numbers:
sum += int(number)
print(sum)
Here is the really working code
import re
m=list()
hand=open('regex_sum_1876332.txt')
for line in hand:
y=re.findall('[0-9]+',line)
for i in range(0,len(y)):
m.append(y[i])
sum=0
for i in range(0,len(m)):
sum=sum+int(m[i])
print(sum)
This week’s project will focus on extracting meaningful information from a typical log file.
Please use the file redhat.txt Download redhat.txtfor this assignment.
Your assignment is to develop a python script that:
Prompts the user for a file to process.
Open the file and iterate through each line of the file, catch and report any errors that occur.
For each line in the file:
If the line identifies a specific worm that was detected, then record the unique name of the worm.
Keep track of the number of occurrences of each unique worm.
Once all the lines have been processed produce a prettytable result that includes the name of each unique worm and number of occurrences that were identified. The prettytable should be sorted by highest occurring worms.
Your script must be commented in detail.
Submit one file logScan.py. In addition, submit a screenshot of successful execution of the script.
Any help as soon as possible
import re
hand = open("datafile.txt")
num_list = list()
for line in hand:
num= re.findall('[0-9]+', line)
num_list = num_list+num
sum=0
for x in num_list:
sum += int(x)
print(sum)