Python export to file via ofile without bracket characters - python

I successfully simplified a python module that imports data from a spectrometer
(I'm a total beginner, somebody else wrote the model of the code for me...)
I only have one problem: half of the output data (in a .csv file) is surrounded by brackets: []
I would like the file to contain a structure like this:
name, wavelength, measurement
i.e
a,400,0.34
a,410,0.65
...
but what I get is:
a,400,[0.34]
a,410,[0.65]
...
Is there any simple fix for this?
Is it because measurement is a string?
Thank you
import serial # requires pyserial library
ser = serial.Serial(0)
ofile = file( 'spectral_data.csv', 'ab')
while True:
name = raw_input("Pigment name [Q to finish]: ")
if name == "Q":
print "bye bye!"
ofile.close()
break
first = True
while True:
line = ser.readline()
if first:
print " Data incoming..."
first = False
split = line.split()
if 10 <= len(split):
try:
wavelength = int(split[0])
measurement = [float(split[i]) for i in [6]]
ofile.write(str(name) + "," + str(wavelength) + "," + str(measurement) + '\n')
except ValueError:
pass # handles the table heading
if line[:3] == "110":
break
print " Data gathered."
ofile.write('\n')

do this:
measurement = [float(split[i]) for i in [6]]
ofile.write(str(name) + "," + str(wavelength) + "," + ",".join(measurement) + '\n')
OR
ofile.write(str(name) + "," + str(wavelength) + "," + split[6] + '\n')

Related

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.

open() method doesn't open specified .txt file

I am made a file where i can reference functions and i thought that it would be fun to make a program to add text to the file, and when i try to open the file, it doesn't show any errors, but when i go and check the file there's nothing there.
My code:
ime_funk = input("Ime funkcije: ")
x = 0
funk = ""
while True:
vseb_funk = input("Kopiraj eno, pa po eno vrstico funkcije, ko si končal napiši končano: ")
if vseb_funk == "končano":
break
else:
funk += "\n "+vseb_funk
mark = open("test.txt", "a")
mark.write("\n" + ime_funk + "\n" + funk)
Don't pay attention to the variable names and strings, as that's not important.
Also I am using replit if that's important.
I have no idea why it doesn't work.
i have tried mark = open("test.txt", "w") but same story.
you need to add this line to your code
mark.close()
or what you can do I replace this part of the code
mark = open("test.txt", "a")
mark.write("\n" + ime_funk + "\n" + funk)
with this code:
with open("test.txt", "a") as mark:
mark.write("\n" + ime_funk + "\n" + funk)
A file write in python does not happen untill you call file.flush(). This is usually called automatically when you close the file but in your example you are never closing the file. Try:
ime_funk = input("Ime funkcije: ")
x = 0
funk = ""
while True:
vseb_funk = input("Kopiraj eno, pa po eno vrstico funkcije, ko si končal napiši končano: ")
if vseb_funk == "končano":
break
else:
funk += "\n "+vseb_funk
mark = open("test.txt", "a")
mark.write("\n" + ime_funk + "\n" + funk)
mark.close()
or even better try using the with statement:
with open("test.txt", "a") as mark:
mark.write("\n" + ime_funk + "\n" + funk)
this way close() is called automatically

Updates to text file are not being parsed using python

I'm parsing data from a text file ('placlog.txt') that is continuously being updated. As I run the code everything prints as expected, but if there are any updates to the placlog file while the code is running it is not printed.
The placlog file is being updated by a third-party program and I am using the above code to read the file and print any updates.
Once formatted, the text should be sent via a Telegram API. This part is also working initially.
import urllib.parse
import time
import requests
import os
def post_to_telegram(msg):
#print(msg)
base_url = 'https://api.telegram.org/bot&text="{}'.format(msg)
requests.get(base_url)
def check_url_inMsgList(stringToMatch, msgList):
for i in msgList:
if (stringToMatch in i):
return False
return True
try:
f = open("oldFile.txt", "r")
msgList = f.read().split("\n")
f.close()
except:
f = open("oldFile.txt", "w")
msgList = []
f.close()
selections = []
urr = ""
name = ""
pie = ""
ourLines = 2400
url_found = 0
name_found = 0
pie_found = 0
while (True):
file1 = open('placlog.txt', 'r')
Lines = file1.readlines()
file1.close()
while (True):
# print("-------------------------------")
if (ourLines == len(Lines)):
break
elif (ourLines > len(Lines)):
ourLines = 0
else:
txt = Lines[ourLines].strip()
tlist = txt.split("&")
ourLines = ourLines + 1
for subtxt in tlist:
if "eventurl=" in subtxt:
a = subtxt[9:len(subtxt) - 3]
url = "www.awebsite.com/%23" + a.replace("%23", "/")
#url = url.replace("%23", "#")
for i in range(10):
if "F" + str(i) + "/" in url:
url = url.split("F" + str(i) + "/")[0] + "F" + str(i) + "/"
urr = url
url_found = 1
elif "bit=" in subtxt:
name = urllib.parse.unquote(subtxt[4:len(subtxt)])
name_found = 1
elif "pie\":" in subtxt:
a = subtxt.split("price")[1]
pie = a.split("\"")[2]
pie = float(pie)
pie = round(pie, 1)
pie = str(pie)
pie_found = 1
selections.append(url + name + pie)
msg = (url + " " + name + " " + pie)
stringToFind = url + " " + name
if (check_url_inMsgList(stringToFind, msgList)):
post_to_telegram(msg)
msgList.append(msg)
print(msg)
f = open("oldFile.txt", "a+")
f.write(msg + "\n")
f.close()
time.sleep(0.5)
elif "minodds=" in subtxt:
a = subtxt.split("minodds=")[1]
pie = a.split("&")[0]
pie = float(pie)
rie = round(pie, 1)
pie = str(pie)
pie_found = 1
selections.append(url + name + pie)
msg = (url + " " + name + " " + pie)
stringToFind = url + " " + name
if (check_url_inMsgList(stringToFind, msgList)):
post_to_telegram(msg)
msgList.append(msg)
print(msg)
f = open("oldFile.txt", "a+")
f.write(msg + "\n")
f.close()
time.sleep(0.5)
time.sleep(1)
I would recommend using watchdog, and seeing if that helps your situation. It can monitor for file system changes, so you could define a function which is executed when the placlog.txt file is changed/updated.
A good guide can be found here: http://thepythoncorner.com/dev/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/
From that guide, you can simply change the functions defined to suit your needs i.e.
def on_modified(event):
if event.src_path == "path/to/placlog.txt":
with open('placlog.txt', 'r') as placlog:
lines = file1.readlines()
Could you try this out and see if it helps? I still recommend the with statement for file i/o since you always want your file to close no matter what.
This link might also be useful since they are also monitoring a single .txt file: Python Watchdog - src_path inconsistent
watchdog documentation: https://pythonhosted.org/watchdog/
Note: Deleted the old answer since you clarified the question.

replacing a string in a text file

I wrote a script that will open my text file search for a certain word, then select the line that contains this word ans split it into three parts, then it chooses the part which is a number and add 1 to it, so every time I run the script one is added to this number. here is the script:
#!/usr/bin/env python
inputFile = open('CMakeLists.txt', 'r')
version = None
saved = ""
for line in inputFile:
if "_PATCH " in line:
print "inside: ", line
version = line
else:
saved += line
inputFile.close()
inputFile = open('CMakeLists.txt', 'w')
x = version.split('"')
print "x: ", x
a = x[0]
b = int(x[1]) + 1
c = x[2]
new_version = str(a) + '"' + str(b) + '"' + str(c)
print "new_version: ", new_version
inputFile.write(str(saved))
inputFile.write(str(new_version))
inputFile.close()
but my problem is that the new number is being written at the end of the file, I want it to stay in its original place. Any ideas ?
thanks
The problem is that you write the new version number after the original file (without the version line):
inputFile.write(str(saved))
inputFile.write(str(new_version))
You could fix it by saving the lines before and after the line that contains the version separately and then save them in the right order:
#!/usr/bin/env python
inputFile = open('CMakeLists.txt', 'r')
version = None
savedBefore = ""
savedAfter = ""
for line in inputFile:
if "_PATCH " in line:
print "inside: ", line
version = line
elif version is None:
savedBefore += line
else:
savedAfter += line
inputFile.close()
inputFile = open('CMakeLists.txt', 'w')
x = version.split('"')
print "x: ", x
a = x[0]
b = int(x[1]) + 1
c = x[2]
new_version = str(a) + '"' + str(b) + '"' + str(c)
print "new_version: ", new_version
inputFile.write(savedBefore)
inputFile.write(str(new_version))
inputFile.write(savedAfter)
inputFile.close()
Note: you might need to add some extra text with the version line to make it have the same format as the original (such as adding "_PATCH").
There is a lots to say on your code.
Your mistake is that you're writing your "saved" lines and after you are writing your modified version. Hence, this modified line will be written at the end of the file.
Moreover, I advice you to use with statements.
lines = []
with open('CmakeLists.txt', 'r') as _fd:
while True:
line = _fd.readline()
if not line:
break
if '_PATCH ' in line:
a, b, c = line.split('"')
b = int(b) + 1
line = '{} "{}" {}'.format(a, b, c)
lines.append(line)
with open('CmakeLists.txt', 'w') as _fd:
for line in lines:
_fd.write(line)
This code is untested and may contains some error... also, if your input file is huge, putting every lines in a list can be a bad idea.
#!/usr/bin/env python
inputFile = open('CMakeLists.txt', 'r')
version = None
saved = ""
for line in inputFile:
if "_PATCH " in line:
print "inside: ", line
version = line
x = version.split('"')
print "x: ", x
a = x[0]
b = int(x[1]) + 1
c = x[2]
new_version = str(a) + '"' + str(b) + '"' + str(c)
saved += new_version
else:
saved += line
inputFile.close()
inputFile = open('CMakeLists.txt', 'w')
inputFile.write(str(saved))
inputFile.close()
if a certain line is found, update its content and add to saved, once for loop ends, just write saved to file

Cannot write to text file python 2.7 invalid syntax?

My code works perfectly, but I want it to write the values to a text file. When I try to do it, I get 'invalid syntax'. When I use a python shell, it works. So I don't understand why it isn't working in my script.
I bet it's something silly, but why wont it output the data to a text file??
#!/usr/bin/env python
#standard module, needed as we deal with command line args
import sys
from fractions import Fraction
import pyexiv2
#checking whether we got enough args, if not, tell how to use, and exits
#if len(sys.argv) != 2 :
# print "incorrect argument, usage: " + sys.argv[0] + ' <filename>'
# sys.exit(1)
#so the argument seems to be ok, we use it as an imagefile
imagefilename = sys.argv[1]
#trying to catch the exceptions in case of problem with the file reading
try:
metadata = pyexiv2.metadata.ImageMetadata(imagefilename)
metadata.read();
#trying to catch the exceptions in case of problem with the GPS data reading
try:
latitude = metadata.__getitem__("Exif.GPSInfo.GPSLatitude")
latitudeRef = metadata.__getitem__("Exif.GPSInfo.GPSLatitudeRef")
longitude = metadata.__getitem__("Exif.GPSInfo.GPSLongitude")
longitudeRef = metadata.__getitem__("Exif.GPSInfo.GPSLongitudeRef")
# get the value of the tag, and make it float number
alt = float(metadata.__getitem__("Exif.GPSInfo.GPSAltitude").value)
# get human readable values
latitude = str(latitude).split("=")[1][1:-1].split(" ");
latitude = map(lambda f: str(float(Fraction(f))), latitude)
latitude = latitude[0] + u"\u00b0" + latitude[1] + "'" + latitude[2] + '"' + " " + str(latitudeRef).split("=")[1][1:-1]
longitude = str(longitude).split("=")[1][1:-1].split(" ");
longitude = map(lambda f: str(float(Fraction(f))), longitude)
longitude = longitude[0] + u"\u00b0" + longitude[1] + "'" + longitude[2] + '"' + " " + str(longitudeRef).split("=")[1][1:-1]
## Printing out, might need to be modified if other format needed
## i just simple put tabs here to make nice columns
print " \n A text file has been created with the following information \n"
print "GPS EXIF data for " + imagefilename
print "Latitude:\t" + latitude
print "Longitude:\t" + longitude
print "Altitude:\t" + str(alt) + " m"
except Exception, e: # complain if the GPS reading went wrong, and print the exception
print "Missing GPS info for " + imagefilename
print e
# Create a new file or **overwrite an existing file**
text_file = open('textfile.txt', 'w')
text_file.write("Latitude" + latitude)
# Close the output file
text_file.close()
except Exception, e: # complain if the GPS reading went wrong, and print the exception
print "Error processing image " + imagefilename
print e;
The error I see says:
text_file = open('textfile.txt','w')
^
SyntaxError: invalid syntax
File open is within the first try block. It is outside the second try except block. Move it outside the first try except block or increase the indent to include them within the first try block. It should work fine there.
Also move(increase the indent) the two print statements within the try as well.
This will work for you:
#!/usr/bin/env python
#standard module, needed as we deal with command line args
import sys
from fractions import Fraction
import pyexiv2
#checking whether we got enough args, if not, tell how to use, and exits
#if len(sys.argv) != 2 :
# print "incorrect argument, usage: " + sys.argv[0] + ' <filename>'
# sys.exit(1)
#so the argument seems to be ok, we use it as an imagefile
imagefilename = sys.argv[1]
#trying to catch the exceptions in case of problem with the file reading
try:
metadata = pyexiv2.metadata.ImageMetadata(imagefilename)
metadata.read();
#trying to catch the exceptions in case of problem with the GPS data reading
try:
latitude = metadata.__getitem__("Exif.GPSInfo.GPSLatitude")
latitudeRef = metadata.__getitem__("Exif.GPSInfo.GPSLatitudeRef")
longitude = metadata.__getitem__("Exif.GPSInfo.GPSLongitude")
longitudeRef = metadata.__getitem__("Exif.GPSInfo.GPSLongitudeRef")
# get the value of the tag, and make it float number
alt = float(metadata.__getitem__("Exif.GPSInfo.GPSAltitude").value)
# get human readable values
latitude = str(latitude).split("=")[1][1:-1].split(" ");
latitude = map(lambda f: str(float(Fraction(f))), latitude)
latitude = latitude[0] + u"\u00b0" + latitude[1] + "'" + latitude[2] + '"' + " " + str(latitudeRef).split("=")[1][1:-1]
longitude = str(longitude).split("=")[1][1:-1].split(" ");
longitude = map(lambda f: str(float(Fraction(f))), longitude)
longitude = longitude[0] + u"\u00b0" + longitude[1] + "'" + longitude[2] + '"' + " " + str(longitudeRef).split("=")[1][1:-1]
## Printing out, might need to be modified if other format needed
## i just simple put tabs here to make nice columns
print " \n A text file has been created with the following information \n"
print "GPS EXIF data for " + imagefilename
print "Latitude:\t" + latitude
print "Longitude:\t" + longitude
print "Altitude:\t" + str(alt) + " m"
except Exception, e: # complain if the GPS reading went wrong, and print the exception
print "Missing GPS info for " + imagefilename
print e
# Create a new file or **overwrite an existing file**
text_file = open('textfile.txt', 'w')
text_file.write("Latitude" + latitude)
# Close the output file
text_file.close()
except Exception, e: # complain if the GPS reading went wrong, and print the exception
print "Error processing image " + imagefilename
print e;
Can be you are tabulating wrong?... The lines:
print " \n A text file has been created with the following information \n"
print "GPS EXIF data for " + imagefilename
appears to be wrong tabulated
EDIT: The code you posted -the one of the trace- is wrong tabulated, too.

Categories

Resources