Python, write tuple into binary file - python

I'm trying to write tuple ('hola', 'cheese', '1235265'), ('hey', 'weird', '30193') getting from mysql DB, putting values into binary file
I saw it got DB table as tuple. Tried to convert into binary, didn't work well.
So i tried another with tuple -> string -> binary, still has an error...
is there any good ways to write tuple to binary file in Python?
for i in text_query:
query = "select * from " + i
curs.execute(query)
# Data Fetch from Mysql
rows = curs.fetchall()
results = [".".join(map(str, r)) for r in rows]
make_file(name,i,results)
conn.close()
def make_file(name, filename, rows):
if filename == 'student':
with open(name + '_' + filename + '.dat', 'wb') as fp:
for i in rows:
fp.write(bytearray(rows + '\n'))
elif filename == 'course':
with open(name + '_' + filename + '.dat', 'wb') as fp:
for i in rows:
fp.write(bytearray(rows + '\n'))
elif filename == 'course_taken':
with open(name + '_' + filename + '.dat', 'wb') as fp:
for i in rows:
fp.write(bytearray(rows + '\n'))
else:
return 0;

You can write a binary representation of your data to file easily enough, not that it would be a good idea, from an encoding point of view. Nor will it be easy to read back in, but this will work:
def make_text_file(name, filename, rows):
if filename == 'student': #
with open(name + '_' + filename + '.txt', 'w') as fp:
row_index = 0
fp.write('<record start>')
for i in rows:
row_index += 1
fp.write('<row {0}>{1}</row>'.format(row_index, i))
fp.write('<record end>\n')
def make_binary_file(name, filename, rows):
if filename == 'student':
with open(name + '_' + filename + '.dat', 'wb') as fp:
row_index = 0
for i in rows:
row_index += 1
fp.write(bytes((i), 'utf8'))
def test_things():
""" Generate some data to write to file """
rows = ('hola', 'cheese', '1235265')
#make_file('student', 'test_student.txt', rows)
make_text_file('test', 'student', rows)
rows = ('hey', 'weird', '30193')
make_binary_file('test', 'student', rows)
if __name__ == '__main__':
test_things()

Related

How to replace the last line ina text file?

I have a program that runs twice a day however I am rewriting it as I'm making a new update for my program. I'm running into a problem. The program writes about 2500 different text files each time it runs.
Each file is a csv and has five columns
date, data, data, data, data
Im wanting to delete the last row of the data to write the new information only if it has already ran that day.
with open('file.csv', 'r+') as file:
info = [line.split(',') for line in file]
for row in info:
if str(today) in row:
#need help here
this is the old one that I'm re-doing. I need to rework it as it will no longer work with the new program.
with open('file.csv', 'a+') as file:
file.write(str(today) + ',' +
str(data) + ',' +
str(data) + ',' +
str(data) + ',' +
str(data) + ',' + '\n')
Maybe this will help you
with open('file.csv', 'r+') as file:
info = [line.split(',') for line in file]
for idx, row in enumerate(info):
if str(today) in row:
# replace with new today data
info[idx] = [str(today), str(data), str(data), str(data), str(data)]
with open('file.csv', 'w+') as file:
for row in info:
file.write(str(today) + ',' +
str(data) + ',' +
str(data) + ',' +
str(data) + ',' +
str(data) + ',' + '\n')

Create two different xml files from single csv file using python

I am pretty new to python and I have one csv file and based on one condition I need to create two different xml files.
The condition based on which I need to create two xml is as follows:
If Primary spindle == 1 then data related to it will go to xml1 else xml2.
This is how my xml looks like as shown in image:
The code I am writing is as follows:
import csv
file = open(r'C:\Users\hu170f\MultiSpindleArchitechture\New folder\MAAP-S12_LH_UP_PASS-LN1-V1.csv')
csvreader = csv.reader(file)
xmlFile = r'C:\Users\hu170f\Documents\TEST\myData4.xml'
xmlFile1 = r'C:\Users\hu170f\Documents\TEST\myData5.xml'
#header = next(csvreader)
xmlData = open(xmlFile, 'w')
xmlData.write('<?xml version="1.0" encoding = "UTF-8"?>' + "\n")
# there must be only one top-level tag
xmlData.write('<MCD>' + "\n")
xmlData1 = open(xmlFile1, 'w')
xmlData1.write('<?xml version="1.0" encoding = "UTF-8"?>' + "\n")
# there must be only one top-level tag
xmlData1.write('<MCD>' + "\n")
#print(header)
rows = []
for row in csvreader:
rows.append(row)
#print(len(rows))
#print(len(row))
Field = " "
Value = " "
counter = 0
for i in rows:
tag = i
#print(tag)
#print(len(rows))
for j in range(len(tag)):
tag[j] = tag[j].replace(' ', '_')
if j == 0:
#print("Field = ", tag[j])
Field = tag[j]
counter = counter +1
else:
#print("Value = ", tag[j])
Value = tag[j]
if(counter%2 == 0):
xmlData.write(' ' + '<' + Field + '>' \
+ Value + '</' + Field + '>' + "\n")
else :
xmlData1.write(' ' + '<' + Field + '>' \
+ Value + '</' + Field + '>' + "\n")
xmlData.write('</MCD>' + "\n")
xmlData.close()
xmlData1.write('</MCD>' + "\n")
xmlData1.close()
#print(rows[6])
file.close()
I want both xml to contain common data from Header to LN and then data from WorkStep UUID till secondary Spindle based on the condition in each xml file.

I cannot let the list write in txt (python)

I want to use writelines() to let the list write in txt, but after running, there's nothing in my txt. What's wrong with my code?
Help me if you could. Thank you!
example list(records): [['flower', '200'], ['ham', '60'], ['van', '150']]
I want to write in the txt as below:
flower 200
ham 60
van 50
my code:
def save(initial_money, records): # I need both so don't change.
with open('records.txt', 'w') as f:
first_line = str(initial_money) + "\n"
f.write(first_line)
return initial_money
L = []
for rec, amt in records:
all_rec = str(rec) + " " + str(amt) + "\n"
L.append(all_rec)
f.writelines(records) # must use writelines
return records
This should do:
def save(initial_money, records):
with open('records.txt', 'w') as f:
first_line = str(initial_money) + "\n"
f.write(first_line)
for rec, amt in records:
f.write(str(rec) + " " + str(amt) + "\n")
The first return closes the function, you don't need second return either, records is available as argument.
If you insist on using writelines, you can modify it like below:
def save(initial_money, records):
with open('records.txt', 'w') as f:
first_line = str(initial_money) + "\n"
f.write(first_line)
L = []
for rec, amt in records:
L.append(str(rec) + " " + str(amt) + "\n")
f.writelines(L)
I didn't succeed to understand what you need two arguments.
here is my code:
def save(initial_money,records):
first_line = str(initial_money) + "\n"
with open('records.txt', 'w') as f:
f.writelines(first_line)
for items in records:
f.writelines(f"{items[0]} {items[1]} ")
f.writelines('\n')
records = [['flower', '200'], ['ham', '60'], ['van', '150']]
initial_money = 0
save(initial_money, records)

Python API call JSON to CSV iteration overwriting data

New to Python...trying to perform an API call and output the data to CSV.
Works fine for a single variable being passed but when I parse through a list the last item in the list is the only output I receive.
Looking to find the best way to approach this. Not sure if it's just an issue of logic or I need to find a way to consistently append to the file and remove results when it's rerun.
list = open(infile, "r")
print("-------------Attempting to Query Data----------------")
for item in list:
try:
if inp == 1:
eval_string = "&value=" + item
elif inp == 2:
eval_string = "&value__regexp=.*." + item + "*"
else:
print("Invalid input!")
result = requests.get(url + "api_key=" + api + "&username=" + user + eval_string + "&limit=" + str(limit) + "&status=" + status)
data = result.json()
if result.status_code == 200:
with open(outfile, 'w', newline='') as data_file:
writer = csv.writer(data_file)
count = 0
for obj in data['objects']:
if count == 0:
header = 'value', 'confidence', 'type', 'source', 'date', 'status'
writer.writerow(header)
count += 1
writer.writerow([obj['value'], obj['confidence'], obj['type'], obj['source'], obj['date'], obj['status']])
else:
print("-------------Failed to connect - check API config info or that site is up----------------")
except OSError:
print("Failed to query.")
print("-------------Results returned in " + outfile + "----------------")

Create an interactive command loop using inheritance python34

I'm trying to see how I can structure a script in a way that I can use the inheritance method. I'm fairly new to python. And my problem is using variables in one class from another class-def. I just recently learned about the super function and I don't think I'm using it right because it keeps printing and recalculating everything that it's pulling from.
Let's say I have a bunch of messages coming in a text file delimited by commas that give me different information. I want to be able to take that text file and...
be able to read the content delimited by commas (done)
tell me how many of each type of message there are (done)
then create a class called messages that has defs for each type of message with its respective calculations and variables it creates in those instances (done)
create class to print and write those calculations and variables in the client and xls (partially done due to my issue)
create class to convert xls to csv and kml (somewhat done)
Here is a toy structure of what I'm working with
import bunch of stuff
data = [] #empty because we will store data into it
#Reads a CSV file and return it as a list of rows
def read_csv_file(filename):
"""Reads a CSV file and return it as a list of rows."""
for row in csv.reader(open(filename)):
data.append(row)
return data
with open(path_in + data_file) as csvfile:
read_it = list(csv.reader(csvfile, delimiter=','))
#Counts the number of times a GPS command is observed
def list_msg_type_countdata):
"""Counts the number of times a GPS command is observed.
Returns a dictionary object."""
msg_count = dict()
for row in data:
try:
msg_count[row[0]] += 1
except KeyError:
msg_count[row[0]] = 1
return msg_count
print(list_msg_type_count(read_it))
print ("- - - - - - - - - - - - -")
class CreateWorkbook:
def openworkbook(self, data):
global output_filename
output_filename = input('output filename:')
global workbook
workbook = xlsxwriter.Workbook(path_out + output_filename + '_' + command_type +'.xlsx')
self.worksheet = workbook.add_worksheet()
#formatting definitions
global bold
bold = workbook.add_format({'bold': True})
global date_format
date_format = workbook.add_format({'num_format': "m/d/yyyy hh:mm:ss"})
global time_format
time_format = workbook.add_format({'num_format': "hh:mm:ss"})
def closeworkbook_gprmc(self, data):
print('closeworkbook')
#pull data from process_msg1
(i1, i2, i3) = messagetype.process_msg1(data)
#sets up the header row
self.worksheet.write('A1','item1',bold)
self.worksheet.write('B1', 'item2',bold)
self.worksheet.write('C1', 'item3',bold)
self.worksheet.autofilter('A1:C1') #dropdown menu created for filtering
# Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
for r, row in enumerate(data, start=1): #where you want to start printing results inside workbook
for c, col in enumerate(data):
self.worksheet.write_column(r,0, i1)
self.worksheet.write_column(r,1, i2)
self.worksheet.write_column(r,2, i3)
workbook.close()
f.close()
print('XLSX file named ' + output_filename + '_' + command_type +' was created')
def closeworkbook_msg2(self, data):
#pull data from process_msg2
(i1, i2, i3, i4) = messagetype.process_msg2(data)
#sets up the header row
self.worksheet.write('A1','item1',bold)
self.worksheet.write('B1', 'item2',bold)
self.worksheet.write('C1', 'item3',bold)
self.worksheet.write('C1', 'item4',bold)
self.worksheet.autofilter('A1:C1') #dropdown menu created for filtering
# Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
for r, row in enumerate(data, start=1): #where you want to start printing results inside workbook
for c, col in enumerate(data):
self.worksheet.write_column(r,0, i1)
self.worksheet.write_column(r,1, i2)
self.worksheet.write_column(r,2, i3)
self.worksheet.write_column(r,3, i4)
workbook.close()
f.close()
print('XLSX file named ' + output_filename + '_' + command_type + ' was created')
class ConvertFile
def convert2csv(self, data):
# set path to folder containing xlsx files
os.chdir(path_out)
# find the file with extension .xlsx
xlsx = glob.glob(output_filename + '_' + command_type + '.xlsx')
# create output filenames with extension .csv
csvs = [x.replace('.xlsx','.csv') for x in xlsx]
# zip into a list of tuples
in_out = zip(xlsx,csvs)
# loop through each file, calling the in2csv utility from subprocess
for xl,csv in in_out:
out = open(csv,'w')
command = 'c:/python34/scripts/in2csv %s\\%s' % (path_out,xl)
proc = subprocess.Popen(command,stdout=out)
proc.wait()
out.close()
print('CSV file named ' + output_filename + '_' + command_type + ' was created')
def convert2kml(self, data):
#Input the file name.
h = open(path_out + output_filename + '_' + command_type + '.csv')
with h as csvfile2:
data2 = csv.reader(csvfile2,delimiter=',')
next(data2)
#Open the file to be written.
g = open(output_filename + '_' + command_type +'.kml','w')
g.write("<?xml version='1.0' encoding='UTF-8'?>\n")
g.write("<kml xmlns='http://earth.google.com/kml/2.1'>\n")
g.write("<Document>\n")
g.write(" <name>" + output_filename + '_' + command_type + '.kml' +"</name>\n")
for row in data2:
g.write(" <Placemark>\n")
g.write("<TimeStamp><when>" + str(row[0]) + "</when></TimeStamp>\n")
g.write(" <Point>\n")
g.write(" <coordinates>" + str(row[2]) + "," + str(row[1]) + "</coordinates>\n")
g.write(" </Point>\n")
g.write(" </Placemark>\n")
g.write("</Document>\n")
g.write("</kml>\n")
g.close()
h.close()
print('and ' + output_filename + '_' + command_type +'.kml was created,too!')
class MessageType:
def process_msg1(self,data)
item1 = []
item2 = []
item3 = []
print('printing stuff')
for r in data:
if row[0] == 'msg type1'
item1.append('calculations')
item2.append('calculations')
item3.append('calculations')
print('calculations done')
return(array(item1),array(item2),array(item3))
def process_msg2(self,data)
item1 = []
item2 = []
item3 = []
item4 = []
print('printing stuff')
for r in data:
if row[0] == 'msg type1'
item1.append('calculations')
item2.append('calculations')
item3.append('calculations')
item4.append('calculations')
print('calculations done')
return(array(item1),array(item2),array(item3),array(item4))
class PrintMSG(MessageType):
def process_msg1(self, data):
(i1, i2, i3) = super(PrintMSG, self).process_msg1(data)
print('printing plus plotting using variables from class Message')
def process_msg2(self, data):
(i1, i2, i3,i4) = super(PrintMSG, self).process_msg2(data)
print('printing plus plotting using variables from class Message')
#processing piece
keep_asking = True
while keep_asking:
command_type = input("What message type do you want to look at?")
if command_type == 'msg type1':
createworkbook = CreateWorkbook()
createworkbook.openworkbook(data)
msg = MessageType()
print_msg = PrintMSG()
print_msg.process_msg1(data)
createworkbook.closeworkbook_msg1(data)
convert2csv(data)
convert2kml(data)
elif command_type == 'msg type2':
createworkbook = CreateWorkbook()
createworkbook.openworkbook(data)
msg = MessageType()
print_msg = PrintMSG()
print_msg.process_msg2(data)
createworkbook.closeworkbook_msg2(data)
convert2csv(data)
convert2kml(data)
else:
print("Invalid type:", command_type)
wannalook = input('Want to look at another message or no?')
if not wannalook.startswith('y'):
keep_asking = False
Class definition
The code is kind of big and there are many things that do not work or could be improved. As a starter, take the class CreateWorkbook. You need always use self, as the first argument for methods. (There are a few exceptions but they are not relevant here.) To be able to use variables defined in one method in another, you need to prefix them with self.:
class CreateWorkbook:
def openworkbook(self, data):
self.output_filename = input('output filename:')
self.workbook = xlsxwriter.Workbook(path_out + output_filename + '_' + command_type +'.xlsx')
self.worksheet = workbook.add_worksheet()
def closeworkbook_msg1(self, data):
#sets up the header row
self.worksheet.write('A1','item1',bold)
self.worksheet.write('B1', 'item2',bold)
self.worksheet.write('C1', 'item3',bold)
self.worksheet.autofilter('A1:C1') #dropdown menu created for filtering
# Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
for r, row in enumerate(data, start=1): #where you want to start printing results inside workbook
for c, col in enumerate(data):
self.worksheet.write_column(r,0, i1)
self.worksheet.write_column(r,1, i2)
self.worksheet.write_column(r,2, i3)
self.workbook.close()
print('XLSX file named ' + output_filename + '_' + command_type +' was created')
def closeworkbook_msg2(self, data):
#sets up the header row
self.worksheet.write('A1','item1',bold)
self.worksheet.write('B1', 'item2',bold)
self.worksheet.write('C1', 'item3',bold)
self.worksheet.write('C1', 'item4',bold)
self.worksheet.autofilter('A1:C1') #dropdown menu created for filtering
# Create a For loop to iterate through each row in the XLS file, starting at row 2 to skip the headers
for r, row in enumerate(data, start=1): #where you want to start printing results inside workbook
for c, col in enumerate(data):
self.worksheet.write_column(r,0, i1)
self.worksheet.write_column(r,1, i2)
self.worksheet.write_column(r,2, i3)
self.worksheet.write_column(r,3, i4)
self.workbook.close()
print('XLSX file named ' + output_filename + '_' + command_type + ' was created')
Reading csv
This doesn't make much sense:
f = open(path_in + data_file)
read_it = read_csv_file(path_in + data_file)
with f as csvfile:
readCSV = csv.reader(csvfile,delimiter=',')
I would interpret it as something like this:
with open(path_in + data_file) as csvfile:
read_it = list(csv.reader(csvfile, delimiter=','))

Categories

Resources