I have a data file with the 2nd column in the file being dates in the format '01/01/2007'. I am trying to convert this column into number format so that I can insert the data in the textfile into a mysql database. I keep getting these errors when I try to do so:
Traceback (most recent call last):
File "C:/Python27/numpy", line 5, in <module>
x = np.loadtxt(fname='xyz.txt', dtype=[('date', 'str', 12),('x','float')], converters={1:datestr2num}, delimiter=None, skiprows=0, usecols=None);
File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 713, in loadtxt
X.append(tuple([conv(val) for (conv, val) in zip(converters, vals)]))
File "C:/Python27/numpy", line 4, in datestr2num
return datetime.datetime.strptime(s,'"%m/%d/%y"')
File "C:\Python27\lib\_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '"01/01/2007"' does not match format '"%m/%d/%y"'
Can anyone please help me with this? Here is the code I am trying:
import numpy as np
import datetime
def datestr2num(s):
return datetime.datetime.strptime(s,'"%m/%d/%y"')
x = np.loadtxt(fname='xyz.txt', dtype= 'float', converters={1:datestr2num}, delimiter=None, skiprows=0, usecols=None);
print x;
'%y' is for two-digit years (e.g. '14'); you have four-digit years (e.g. '2014') so should be using '%Y' - see the documentation.
Related
I am storing data from Arduino to RaspberryPi. My code is working well, but the date-time and data are logging in the first column together (picture 1), although those are shown separately in the RaspberryPi display (picture 2). How can I log those in a seperate column? I attached my code here.
import time,datetime
from datetime import datetime
import csv
import serial
arduino_port = "/dev/ttyACM0"
baud = 9600
fileName="Arduino1.csv"
ser = serial.Serial(arduino_port, baud)
print("Connected to Arduino port:" + arduino_port)
file = open(fileName, "a")
print("Created file")
samples =float('inf')
print_labels = False
line = 0
print('Press Ctrl-C to quit...')
print('Time, CO2(ppm), Temp(C), Humi(%), Vis, IR, UV, pH')
print('-' *85)
while line <= samples:
d=datetime.now().strftime('%Y-%m-%d %H:%M:%S')
if print_labels:
if line==0:
print("Printing Column Headers")
else:
print("Line " + str(line) + ": writing...")
getData=str(ser.readline())
data=getData [1:] [1:-5]
print(d, data)
file = open(fileName, "a")
file.write(d+ data+ "\n")
line = line+1
file.close()
try adding a comma after the Date-time string to separate the data to another column? The date-time and data appear to be separate in the RaspberryPi, but not comma separated, thus both are in the same column.
Try this -
file.write(d + "," + data + "\n")
I am trying to extract data from a JSON file, and am still not clear about the error coming. My data is like this:
"Tracker":{"Sep 30, 2021":{"DC":4,"DN":"0:0",
DC = {}
for day, daily_data in read_content['Tracker'].items():
for value in daily_data['Disturbances Count']:
DC[datetime] = value
Im getting the following error
---------------------------------------------------------------------------
TypeError: 'int' object is not iterable
The solution for the above code only to add str
DC = {}
for day, daily_data in read_content['Tracker'].items():
for value in str(daily_data['Disturbances Count']):
DC[datetime] = value
While executing the following code i'm getting below error, Just for information matchObj here returns a tuple value ..
$ ./ftpParser3_re_dup.py
Traceback (most recent call last):
File "./ftpParser3_re_dup.py", line 13, in <module>
print("{0:<30}{1:<20}{2:<50}{3:<15}".format("FTP ACCOUNT","Account Type","Term Flag"))
IndexError: tuple index out of range
Code is below:
from __future__ import print_function
from signal import signal, SIGPIPE, SIG_DFL
signal(SIGPIPE,SIG_DFL)
import re
with open('all_adta', 'r') as f:
for line in f:
line = line.strip()
data = f.read()
# Making description & termflag optional in the regex pattern as it's missing in the "data_test" file with several occurrences.
regex = (r"dn:(.*?)\nftpuser: (.*)\n(?:description:* (.*))?\n(?:termflag:* (.*))")
matchObj = re.findall(regex, data)
print("{0:<30}{1:<20}{2:<50}{3:<15}".format("FTP ACCOUNT","Account Type","Term Flag"))
print("{0:<30}{1:<20}{2:<50}{3:<15}".format("-----------","------------","--------"))
for index in matchObj:
index_str = ' '.join(index)
new_str = re.sub(r'[=,]', ' ', index_str)
new_str = new_str.split()
# In below print statement we are using "index[2]" as index is tuple here, this is because
# findall() returns the matches as a list, However with groups, it returns it as a list of tuples.
print("{0:<30}{1:<20}{2:<50}{3:<15}".format(new_str[1],new_str[8],index[2],index[3]))
In the line print("{0:<30}{1:<20}{2:<50}{3:<15}".format("FTP ACCOUNT","Account Type","Term Flag")) you have mentioned 4 indices but given only 3 i.e. "FTP ACCOUNT","Account Type","Term Flag"
Remove the 4th index or add a new one
I'm trying to read a file and put contents in a list. I have done this mnay times before and it has worked but this time it throws back the error "list index out of range".
the code is:
with open("File.txt") as f:
scores = []
for line in f:
fields = line.split()
scores.append( (fields[0], fields[1]))
print(scores)
The text file is in the format;
Alpha:[0, 1]
Bravo:[0, 0]
Charlie:[60, 8, 901]
Foxtrot:[0]
I cant see why it is giving me this problem. Is it because I have more than one value for each item? Or is it the fact that I have a colon in my text file?
How can I get around this problem?
Thanks
If I understand you well this code will print you desired result:
import re
with open("File.txt") as f:
# Let's make dictionary for scores {name:scores}.
scores = {}
# Define regular expressin to parse team name and team scores from line.
patternScore = '\[([^\]]+)\]'
patternName = '(.*):'
for line in f:
# Find value for team name and its scores.
fields = re.search(patternScore, line).groups()[0].split(', ')
name = re.search(patternName, line).groups()[0]
# Update dictionary with new value.
scores[name] = fields
# Print output first goes first element of keyValue in dict then goes keyName
for key in scores:
print (scores[key][0] + ':' + key)
You will recieve following output:
60:Charlie
0:Alpha
0:Bravo
0:Foxtrot
I have an issue with Graphite, specifically with carbon-cache. At some point I had it running. now when coming back after a few weeks I tried to start graphite again. The django-webapp runs fine but it seems I have an issue with the carbon-cache backend. Graphite is installed in /opt/graphite and I run /opt/graphite/bin/carbon-cache.py start. This is the error I get:
root#stfutm01:/opt/graphite/bin# ./carbon-cache.py start
Starting carbon-cache (instance a)
Traceback (most recent call last):
File "./carbon-cache.py", line 30, in <module>
run_twistd_plugin(__file__)
File "/opt/graphite/lib/carbon/util.py", line 92, in run_twistd_plugin
runApp(config)
File "/usr/local/lib/python2.7/dist-packages/twisted/scripts/twistd.py", line 23, in runApp
_SomeApplicationRunner(config).run()
File "/usr/local/lib/python2.7/dist-packages/twisted/application/app.py", line 386, in run
self.application = self.createOrGetApplication()
File "/usr/local/lib/python2.7/dist-packages/twisted/application/app.py", line 446, in createOrGetApplication
ser = plg.makeService(self.config.subOptions)
File "/opt/graphite/lib/twisted/plugins/carbon_cache_plugin.py", line 21, in makeService
return service.createCacheService(options)
File "/opt/graphite/lib/carbon/service.py", line 127, in createCacheService
from carbon.writer import WriterService
File "/opt/graphite/lib/carbon/writer.py", line 34, in <module>
schemas = loadStorageSchemas()
File "/opt/graphite/lib/carbon/storage.py", line 123, in loadStorageSchemas
archives = [ Archive.fromString(s) for s in retentions ]
File "/opt/graphite/lib/carbon/storage.py", line 107, in fromString
(secondsPerPoint, points) = whisper.parseRetentionDef(retentionDef)
File "/usr/local/lib/python2.7/dist-packages/whisper.py", line 76, in parseRetentionDef
(precision, points) = retentionDef.strip().split(':')
ValueError: need more than 1 value to unpack
I see that it as an issue with the split retentionDef.strip().split(':'). My storage schema config file (/opt/graphite/conf/storage-schemas.conf) looks like:
[stats]
priority = 110
pattern = ^stats\..*
retentions = 10s:6h,1m:7d,10m:1y
[ts3]
priority = 100
pattern = ^skarp\.ts3\..*
retentions = 60s:1y,1h,:5y
Any hints where I should looking? Or does anybody know what I'm missing here?
I think the problem is the [ts3] rentions. "The retentions line can specify multiple retentions. Each retention of frequency:history is separated by a comma."
In ts3 it appears to be 3 retentions (comma-delimited), with the second not specifying a history and the last not specifying a frequency.
retentions = 60s:1y,1h,:5y
I think you may have meant:
retentions = 60s:1y,1h:5y
Which would be 60 second data for 1 year and 1 hour data for 5 years after that.