Hello all, I found a thread similar, but it was closed... so here is the code and my question...

I am wondering how to change the code from creating files to creating variables?

this is the code from the thread on here....
and below it is the code that i am personally using....

thanks all!

a = 0
while a < 10:
a = a + 1
if a == 4 or a == 6:
# create file for instance 'output4.dat'
# only strings concatinate
output = open('output'+str(a)+'.dat', 'w')
# write the value to file (needs to be a string)
output.write('%s' % (a))
output.close()

and here is my code...

import re
f=open("c:\\textofemail.txt", "r")
f.close()
processedString = re.sub(r"\n\s*\n*", "\n", fileString) #removes blank lines
s=processedString
a=s[s.index("Rate:"):s.index("Best,")] #parses string for desired content
finalStr = re.sub(r"\Rate:\s?","",a)
f=open("c:\\newtextofemail2.txt", "w")
f.write(finalStr) #write new file of cleaned and parsed data
f.close()

#this section will chunk the file into 130 character lengths
f = open('C:\\newtextofemail2.txt')
chunks = []
while "file is not empty":
if not chunk:
break
chunks.append(chunk)
f.close()

# write each chunk to own file
for i,name in enumerate(chunks):
g = open("C:\\newchunkedmultiples"+str(i+1)+".txt","w")
g.write(name+"\n")
g.close()

print "CHUNKED!  LOL" #just for fun :)

it would be better for me to have variables stored in memory instead of files written, so i can send them to twilio later in my program.

thanks

Edited by xopenex: posted wrong code... this one writes separate files for each chunk

2
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by pyTony

id really like to do away with creating any of the new files that are in there.. and just store the data as variables throughout the entire code.

thanks again

edited original post and tried to delete this one.. :)

Edited by xopenex: attempt to delete this post because i &quot;edited&quot; original post

ok, this is an edit and im adding new code... i realized that the chunks are stored as variables already.. (duh! lol).. so i just need to figure how to call them at a 10 second delay and send them through twilio with a loop until all chunks are sent.. twilio needs to make a new api conneciton for every message.. here is some code...

from twilio.rest import TwilioRestClient

# Find these values at https://twilio.com/user/account
account_sid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
auth_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client = TwilioRestClient(account_sid, auth_token)

message = client.sms.messages.create(to="phone number", from_="twilio number",
body="Hello There!")

i would like to change the Hello There to my chunk variables... and then loop/send them every 10 seconds until all chunks have been sent..

thanks guys.. long day, and a complete newbie also!

hello,

ive got the time.sleep() figured out... next i will try and put it in the twilio poriton..

# write each chunk to own file
for i,name in enumerate(chunks):
g = open("C:\\newchunkedmultiples1"+str(i+1)+".txt","w")
g.write(name+"\n")
g.close()
time.sleep(2.0)

cool, well ive got this working now.... the text comes through with the variable.. so if i figure out how to get my chunk variables in there and a loop... then i'm done! (well kinda done lol)

from twilio.rest import TwilioRestClient

age=33

# Find these values at https://twilio.com/user/account
account_sid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
auth_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client = TwilioRestClient(account_sid, auth_token)

message = client.sms.messages.create(to="phone number", from_="twilio number",
body="Hello there, I am %d years old." % age)

so... to recap.. im trying to figure out how to repeat/loop this code until all my chunks have been texted!

Still gonna check a couple of things, clean it up, and make more streamlined... but at this point it works! Any help on cleaning code, or more efficient ways, would be greatly appreciated!

So what it does.. is take a file... parse, strip, chunk, then send in twilio... maybe can be helpful for someone. :)

from twilio.rest import TwilioRestClient
import time
import sys
import re

f=open("c:\\textofemail.txt", "r")
f.close()

emailWoBl = re.sub(r"\n\s*\n*", "\n", fileString) #removes blank lines
emailWoWs = re.sub("\s+" , " ", emailWoBl) #removes extra WS and replace w/ single Space

s=emailWoWs

a=s[s.index("Rate:"):s.index("Best,")] #parses string for desired content
finalStr = re.sub(r"\Rate:\s?","",a)

f=open("c:\\newtextofemail.txt", "w")
f.write(finalStr) #write new file of cleaned and parsed data,
f.close()

#this section will chunk the file into 110 character lengths
f = open('C:\\newtextofemail.txt')
chunks = []
while "file is not empty":
if not chunk:
break
chunks.append(chunk)
f.close()

# write each chunk to own file and send each chunk in SMS
for i,name in enumerate(chunks):
g = open("C:\\newchunkedmultiples"+str(i+1)+".txt","w")
g.write(name+"\n")

# Find these values at https://twilio.com/user/account
account_sid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
auth_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
client = TwilioRestClient(account_sid, auth_token)

message = client.sms.messages.create(to="phone number", from_="twilio number",
body="%s" % (name+"\n"))
g.close()
time.sleep(7.0)

print "SMS SENT GO DRIVER GO!!!" #just for fun :)

hehe, i think there is some repetitive and mislabeled content up there... but its late and ive had a long long day... will get back tomorrow with better more accurate work. :)

No more updates.... until tomorrow, I promise! But i just couldn't sleep!

import re
import sys
import time
from twilio.rest import TwilioRestClient

file1=open("c:\\textofemail.txt", "r")
file1.close()

split=string.split()                           # what white space?  LOL
joined=" ".join(string.split())                # removes xtra WS and empty lines
parsed=joined

processed=parsed[parsed.index("Rate:"):parsed.index("Best,")] #parses string
final = re.sub(r"\Rate:\s?","",processed)

file2=open("c:\\newtextofemail.txt", "w")
file2.write(final)                             #writes file of cleaned and parsed data
file2.close()

file3 = open('C:\\newtextofemail.txt')         # this section chunks the
chunks = []                                    # file to 120 character lengths
while "file is not empty":
if not chunk:
break
chunks.append(chunk)
file3.close()

# writes each chunk to own file and SMS's it
for i,name in enumerate(chunks):
file4 = open("C:\\newchunkedmultiples"+str(i+1)+".txt","w")
file4.write(name+"\n")

account_sid = "xxxxxxxxxxxxxxxxxxxxxx"             # Find these values at
auth_token = "xxxxxxxxxxxxxxxxxxxxxx"              # https://twilio.com/user/account
client = TwilioRestClient(account_sid, auth_token)

message = client.sms.messages.create(to="number", from_="twilio number",
body="%s" % (name+"\n"))

file4.close()
time.sleep(1.0)

print "SMS SENT BABY, GO DRIVER GO!!!" # just for fun :)

starting to look better yes? :)

could you not use single instance of client? How the client gets the file name for the chunk of message? Good start at least to remove those ugly cryptic variable names, string is kind of so so as name still.

Edited by pyTony: n/a