I have a text file from which I need to extract some values, but several times I get more than one value between the beginning and the last line that starts with 0089 (CONVENIO) in the quadrant.
With my code I can only make it write in the txt the title I defined and it keeps repeating the first agreement found, but I need it to scroll through the text and bring me new information from the other quadrants.
I need the loop as it can have multiple covenants, for this reason I can't directly "anchor" the lines in a size forecast.
import re
import os
inicio = (' YM-INFRA-CASH MANAGMENT DEST.: 001-0001-CENTRAL ')
lista = []
contador = 3
banco = ' 0089'
convenio = ''
with open(caminho + '/Downloads/TESTE.txt', 'r') as arquivo:
for line in arquivo:
if line.strip() == inicio.strip():
localizar = arquivo.readlines()
inicio = localizar[contador]
van = inicio[13:17]
nomevan = inicio[20:50].strip()
inicio = localizar[contador + 1]
ag = inicio[13:17]
nomeag = inicio[20:50].strip()
inicio = localizar[contador + 2]
cliente = inicio[13:50].strip()
contadorum = 9
while localizar[contadorum][1:5] == '0033':
convenio = localizar[contadorum][1:22].strip()
narqrem = localizar[contadorum][22:34].strip()
bytesrem = localizar[contadorum][34:51].strip()
narqret = localizar[contadorum][51:63].strip()
bytesret = localizar[contadorum][63:81].strip()
totalbytes = localizar[contadorum][81:99].strip()
percrateio = localizar[contadorum][99:112].strip()
print(van, nomevan)
print(ag, nomeag)
print(cliente)
print(convenio, narqrem, bytesrem, narqret, bytesret, totalbytes, percrateio)
lista.append(convenio + narqrem + bytesrem + narqret + bytesret + totalbytes + percrateio +'\n')
with open(caminho + '/Downloads/testefim.txt', 'w') as consolidado:
consolidado.write('CONVENIO' + ';' + 'N ARQ REMES' + ';' + 'BYTES REMES' + ';' + 'N ARQ.RET.' + ';' + 'BYTES RET.' + ';' + 'TOTAL BYTES' + ';' + '% RATEIO' + '\n')
for linha in lista:
consolidado.write(convenio + ';' + narqrem + ';' + bytesrem + ';' + narqret + ';' + bytesret + ';' + totalbytes + ';' + percrateio + '\n')
consolidado.close()
else:
pass
arquivo.close()
Related
The File I'm editing
45940736:1330:0.266667:1602990684:10023084.555000
48545806:16000000:0.000000:9999999999:0.000000
1191125008:1185:37.408333:1602991293:10282893.310000
116776982:1811:0.637500:1602990076:10022476.137000
My Code
f = open("values", "r+")
lines = f.readlines()
for i in lines:
if str(IDS) in i:
spl = i.split(":")
asset_id = spl[0]
value = spl[1]
volume = spl[2]
updated = spl[3]
item_age = spl[4]
new_line = asset_id + ":" + "this is a test" + ":"+ volume + ":" + updated + ":" + item_age
old_line = i
print(new_line)
How would I replace the line while in the for loop instead of doing
lines[0] = new_line
ff = open('values', 'w')
ff.writelines(lines)
I can't do that because some files will have 2k values and I need to change all of them.
Use enumerate() so you get the index in the list, so you can replace the element.
for index, i in enumerate(lines):
if str(IDS) in i:
spl = i.split(":")
asset_id = spl[0]
value = spl[1]
volume = spl[2]
updated = spl[3]
item_age = spl[4]
new_line = asset_id + ":" + "this is a test" + ":"+ volume + ":" + updated + ":" + item_age
old_line = i
lines[index] = new_line
I am looking to extract tweets and write them to a CSV file, however, I cannot figure out how to get it to generate a file. I am using Tweepy to extract the tweets. I would like the CSV file to contain the following cells: User, date, tweet, likes, retweets, total, eng rate, rating, tweet id
import tweepy
import csv
auth = tweepy.OAuthHandler("", "")
auth.set_access_token("", "")
api = tweepy.API(auth)
try:
api.verify_credentials()
print("Authentication OK")
except:
print("Error during authentication")
def timeline(username):
tweets = api.user_timeline(screen_name=username, count = '100', tweet_mode="extended")
for status in (tweets):
eng = round(((status.favorite_count + status.retweet_count)/status.user.followers_count)*100, 2)
if (not status.retweeted) and ('RT #' not in status.full_text) and (eng <= 0.02):
print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: Low' + ',Tweet ID: ' + str(status.id))
elif (not status.retweeted) and ('RT #' not in status.full_text) and (0.02 < eng <= 0.09):
print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: Good' + ',Tweet ID: ' + str(status.id))
elif (not status.retweeted) and ('RT #' not in status.full_text) and (0.09 < eng <= 0.33):
print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: High' + ',Tweet ID: ' + str(status.id))
elif (not status.retweeted) and ('RT #' not in status.full_text) and (0.33 < eng):
print (status.user.screen_name + ',' + str(status.created_at) + ',' + status.full_text + ",Likes: " + str(status.favorite_count) + ",Retweets: " + str(status.retweet_count) + ',Total: ' + str(status.favorite_count + status.retweet_count) + ',Engagement rate: ' + str(eng) + '%' + 'Rating: Very High' + ',Tweet ID: ' + str(status.id))
tweet = timeline("twitter")
with open('tweet.csv', 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
writer.writerow([tweet])
You can look at https://docs.python.org/3/library/csv.html for the info on how to generate a csv file in Python. Quick exmaple:
import csv
with open('some_output.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["field1", "field2", "field3"])
Your function get_tweets does not return a value but you are trying to retrieve a value from that function which would result in None. Also it looks like tweet value will be list of strings. writerow method from csv.writer should get list of items and not list of lists. I have modified your code to address those issues. Let me know if it works.
def get_tweets(username):
tweets = api.user_timeline(screen_name=username, count=100)
tweets_for_csv = [tweet.text for tweet in tweets]
print(tweets_for_csv)
return tweets_for_csv
tweet = get_tweets("fazeclan")
with open('tweet.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(tweet)
This is my code to create a hashtag file. The issue is it does not put the # for the first hashtag and at he end it puts a double hashtag like below.
passiveincome, #onlinemarketing, #wahmlife, #cash, #entrepreneurlifestyle, #makemoneyonline, #makemoneyfast, #entrepreneurlifestyle, #mlm, #mlm
How do I get the code to remove the double output and put the # at the beginning?
import random, os, sys
basepath = os.path.dirname(sys.argv[0]) + "/"
outputpath = "C:/Users/matth/OneDrive/Desktop/Create hashtags/"
paragraphsmin = 9
paragraphsmax = 9
sentencemin = 1
sentencemax = 1
keywords = []
for line in open(basepath + "/base.txt", "r"):
keywords.append(line.replace("\n",""))
keywordlist = []
keyword = open(basepath + "/text-original.txt", "r")
for line in keyword:
keywordlist.append(line.replace("\n", "\n"))
def type(name):
value = name[random.randint(0,len(name)-1)]
return value
"""
def xyz(num):
s1 = '' + type(keywordlist).strip()
return eval('s' + str(num))
"""
def s1():
return '' + type(keywordlist).strip()
def randomSentence():
sent = eval("s" + str(random.randint(1,1)) + "()")
return sent
for keyword in keywords:
outputfile = open(outputpath + keyword.replace(" ", " ") + ".txt", "w")
outputfile.write('')
for p in range(1,random.randint(paragraphsmin,paragraphsmax) + 1):
outputfile.write('')
for s in range(1,random.randint(sentencemin,sentencemax) + 1):
sentence = randomSentence()
if str(sentence)[0] == "\"":
outputfile.write("" + str(sentence)[0] + str(sentence)[1] + str(sentence)[2:] + " ")
else:
outputfile.write("" + str(sentence)[0] + str(sentence)[1:] + ", #")
outputfile.write('')
outputfile.write(sentence.replace("", "") + "")
outputfile.close()
Try replacing
outputfile.write("" + str(sentence)[0] + str(sentence)[1:] + ", #")
with
outputfile.write("#" + str(sentence)[0] + str(sentence)[1:] + ", ")
Very long and annoying question, ill try to do my best to explain it.
I have a file containing data such as
Sarah;Brown;s.brown#gmail.com;0715123451;1;24;0;0
Joe;Blogg;j.bloggs#gmail.com;0749814574;1;60;0;0
Andrew;Smith;a.smith#gmail.com;0718451658;1;45;0;0
Ray;Charles;r.charles#gmail.com;0715451589;1;40;0;0
Kevin;White;k.white#gmail.com;0749858748;1;20;0;0
Samantha;Collins;s.collins#gmail.com;0715243568;1;10;0;0
Frank;Jones;f.jones#gmail.com;0719487516;2;10;0;0
Liam;Blair;l.blair#gmail.com;0729857614;2;4;0;0
Pat;Phillips;p.phillips#gmail.com;071574216;2;17;0;0
John;Brown;j.brown#gmail.com;0798452648;2;11;0;0
Peter;Bond;p.bond#gmail.com;0798415758;6;4;0;0
Edward;Costello;e.costello#gmail.com;0712474588;2;45;0;0
Iain;Wilkins;i.wilkins#gmail.com;0715497211;2;23;0;0
Time;Pratchett;t.pratchett#gmail.com;0784975135;3;48;0;0
Eleanor;House;e.house#gmail.com;0799871542;3;9;0;0
Gergory;Davies;g.davies#gmail.com;0719475847;3;22;0;0
Tina;Turner;t.turner#gmail.com;0749857123;3;17;0;0
Sally;Stevens;s.stevens#gmail.com;077154198;3;30;0;0
John;Lennon;j.lennon#gmail.com;0704910874;3;29;0;0
The first element is the name, second is surname, third is email address, 4rth is phone number, 5th is division number, 6th is points scored, the remaining two don't matter for this issue.
What I have been asked to do is to check the top two scorers (6ths element) and the bottom two scorers in a division (5th element). When they have been identified, the top two should be promoted (go up a division), the bottom 2 should be demoted (go down a division). I have written this piece of crap:
def rollDivision2():
lst = [line.strip().split(';') for line in open('players.txt','r').readlines()] # creates nested list
totalPoints = []
for i in range(len(lst)):
if lst[i][4] == "2": # checking divisions this is why i need 6 different function
totalPoints.append(int(lst[i][5])) # creates lists from all scores for the division chosen
#----------------------------------------------- figuring out best two scores and writing
maxPoints = max(totalPoints)
for person in lst:
if person[4] == "2" and person[5] == str(maxPoints): #this is why i need 6 different function
biggest = person # creating variable with name of person that has the biggest score
biggestStr = biggest[0] + ";" + biggest[1] + ";" + biggest[2] + ";" + biggest[3] + ";" + biggest[4] + ";" + biggest[5] + ";" + biggest[6] + ";" + biggest[7] + "\n" #puts that lists into a string
break #personWithMostPoints is the whole line of player with most points
secondMaxPoints = secondLargest(totalPoints) #this is why i need 6 different function
for person in lst:
if person[4] == "2" and person[5] == str(secondMaxPoints): #checking for most points in the division
secondBiggest = person
secondBiggestStr = secondBiggest[0] + ";" + secondBiggest[1] + ";" + secondBiggest[2] + ";" + secondBiggest[3] + ";" + secondBiggest[4] + ";" + secondBiggest[5] + ";" + secondBiggest[6] + ";" + secondBiggest[7] + "\n"
break
lineToWriteBiggest = biggest[0] + ";" + biggest[1] + ";" + biggest[2] + ";" + biggest[3] + ";" + "1" + ";" + biggest[5] + ";" + biggest[6] + ";" + biggest[7] + "\n"
lineToWriteSecondBiggest = secondBiggest[0] + ";" + secondBiggest[1] + ";" + secondBiggest[2] + ";" + secondBiggest[3] + ";" + "1" + ";" + secondBiggest[5] + ";" + secondBiggest[6] + ";" + secondBiggest[7] + "\n"
#----------------------------------------------- figuring out best two scores
#----------------------------------------------- figuring out worst two scores
minPoints = min(totalPoints)
for person in lst:
if person[4] == "2" and person[5] == str(minPoints): #this is why i need 6 different function
least = person
leastStr = least[0] + ";" + least[1] + ";" + least[2] + ";" + least[3] + ";" + least[4] + ";" + least[5] + ";" + least[6] + ";" + least[7] + "\n"
break #personWithMostPoints is the whole line of player with most points
secondLeastPoints = secondSmallest(totalPoints) #method defined in utility functions
for person in lst:
if person[4] == "2" and person[5] == str(secondLeastPoints): #this is why i need 6 different function
secondLeast = person
secondLeastStr = secondLeast[0] + ";" + secondLeast[1] + ";" + secondLeast[2] + ";" + secondLeast[3] + ";" + secondLeast[4] + ";" + secondLeast[5] + ";" + secondLeast[6] + ";" + secondLeast[7] + "\n"
break
lineToWriteLeast = least[0] + ";" + least[1] + ";" + least[2] + ";" + least[3] + ";" + "3" + ";" + least[5] + ";" + least[6] + ";" + least[7] + "\n"
lineToWriteSecondLeast = secondLeast[0] + ";" + secondLeast[1] + ";" + secondLeast[2] + ";" + secondLeast[3] + ";" + "3" + ";" + secondLeast[5] + ";" + secondLeast[6] + ";" + secondLeast[7] + "\n"
f = open("players.txt","a")
f.write(lineToWriteBiggest)
f.write(lineToWriteSecondBiggest)
f.write(lineToWriteLeast)
f.write(lineToWriteSecondLeast)
f.close()
f = open("players.txt",'r') # Input file
t = open("temp.txt", 'w') #Temp output file
for line in f:
if line != biggestStr and line != secondBiggestStr and line != leastStr and line != secondLeastStr and line != "\n":
t.write(line) #writes all lines apart from the original line (one that needs to be deleted)
f.close()
t.close()
os.remove("players.txt") #deletes players
os.rename('temp.txt', 'players.txt') #new file with modified info is renamed to players
It is very ugly and impractical, moreover, I have to have 6 of these functions (as there are 6 divisions) which makes the program ridiculously overweight.
If anyone could help me, whether it be how I could use this function just once to check all 6 division instead of having to write 6 individual ones.
I'm sorry you had to see this, I just don't know what else to do at this point.
Any help would be very appreciated.
Many thanks
lines = list(open("some.txt"))
for division,items in itertools.groupby(lines,lambda line:int(line.split(";")[4])):
sorted_people = sorted(items,key=lambda item:int(item.split(";")[-1])
print "DIVISION:",division
print "TOP TWO:",sorted_people[-2:]
print "BOTTOM TWO:",sorted_people[:2]
Check this out -
def convert(arr):
return [int(x) if x.isnumeric() else x for x in arr]
def find_top_two(division):
cur_div = [x for x in arr if x[4] == division]
cur_div.sort(key = lambda T : T[5], reverse = True)
return cur_div[:2]
def find_bottom_two(division):
cur_div = [x for x in arr if x[4] == division]
cur_div.sort(key = lambda T : T[5])
return cur_div[:2]
with open('players.txt', 'r') as f:
arr = f.readlines()
arr = [convert(s.strip('\n').split(';')) for s in arr]
print('Top 2 of division 2 :\n', find_top_two(2))
print('Bottom 2 of division 2 :\n', find_bottom_two(2))
Output -
Top 2 of division 2 :
[['Edward', 'Costello', 'e.costello#gmail.com', 712474588, 2, 45, 0, 0], ['Iain', 'Wilkins', 'i.wilkins#gmail.com', 715497211, 2, 23, 0, 0]]
Bottom 2 of division 2 :
[['Liam', 'Blair', 'l.blair#gmail.com', 729857614, 2, 4, 0, 0], ['Frank', 'Jones', 'f.jones#gmail.com', 719487516, 2, 10, 0, 0]]
Not the best and most efficient code, but this should work. I'll leave you to fill in the blanks.
def getPlayersOfDivision(playerInfo, division):
result = []
for player in playerInfo:
if int(player[4]) == division:
result.append(player)
def findPlayer(playerInfo, firstName, lastName):
for i,player in enuemrate(playerInfo):
if player[0] == firstName and player[1] = lastName:
return i
def get2BestPlayers(playerInfo):
pass
def get2WorstPlayers(playerInfo):
pass
def changePlayerDivision(playerInfo, pos, upOrDown):
pass
with open('players.txt', 'r') as f:
playerInfo = f.readlines()
playerInfo = [line.strip().split(';') for line in playerInfo]
numDivisions = ???
for i in range(numDivisions):
divisionPlayers = getPlayersOfDivision(playerInfo, i)
bestPlayers = get2BestPlayers(divisionPlayers)
worstPlayers = get2WorstPlayers(divisionPlayers)
newPlayerInfo = changePlayerDivision(playerInfo, findPlayer(playerInfo, divisionPlayer[bestPlayers[0]][0], divisionPlayer[bestPlayers[0]][1]), +1)
newPlayerInfo = changePlayerDivision(playerInfo, findPlayer(playerInfo, divisionPlayer[bestPlayers[1]][0], divisionPlayer[bestPlayers[1]][1]), +1)
newPlayerInfo = changePlayerDivision(playerInfo, findPlayer(playerInfo, divisionPlayer[worstPlayers[0]][0], divisionPlayer[worstPlayers[0]][1]), -1)
newPlayerInfo = changePlayerDivision(playerInfo, findPlayer(playerInfo, divisionPlayer[worstPlayers[1]][0], divisionPlayer[worstPlayers[1]][1]), -1)
I need to encrypt 3 .bin files which contain 2 keys for Diffie-Hellman. I have no clue how to do that, all I could think of was what I did in the following Python file. I have an example what the output should look like but my code doesn't seem to produce the right keys. The output file server.ini is used by a client to connect to a server.
import base64
fileList = [['game_key.bin', 'Game'], ['gate_key.bin', 'Gate'], ['auth_key.bin', 'Auth']]
iniList = []
for i in fileList:
file = open(i[0], 'rb')
n = list(file.read(64))
x = list(file.read(64))
file.close()
n.reverse()
x.reverse()
iniList.append(['Server.' + i[1] + '.N "' + base64.b64encode("".join(n)) + '"\n', 'Server.' + i[1] + '.X "' + base64.b64encode("".join(x)) + '"\n'])
iniList[0].append('\n')
#time for user Input
ip = '"' + raw_input('Hostname: ') + '"'
dispName = 'Server.DispName ' + '"' + raw_input('DispName: ') + '"' + '\n'
statusUrl = 'Server.Status ' + '"' + raw_input('Status URL: ') + '"' + '\n'
signupUrl = 'Server.Signup ' + '"' + raw_input('Signup URL: ') + '"' + '\n'
for l in range(1, 3):
iniList[l].append('Server.' + fileList[l][1] + '.Host ' + ip + '\n\n')
for l in [[dispName], [statusUrl], [signupUrl]]:
iniList.append(l)
outFile = open('server.ini', 'w')
for l in iniList:
for i in l:
outFile.write(i)
outFile.close()
The following was in my example file:
# Keys are Base64-encoded 512 bit RC4 keys, as generated by DirtSand's keygen
# command. Note that they MUST be quoted in the commands below, or the client
# won't parse them correctly!
I also tried it without inverting n and x