Using PyDev with an eclipse environment for Python 2.7 on OSX. Trying to count the element in the array and sum up the elements in the array. Getting an error on the index.
import numpy as np
import os
import sys
csv_file_object = fileName = os.path.join('train.csv')
print('Directory separator on your platform ({}): {}'.format(sys.platform, os.sep))
data=[]
for row in csv_file_object:
data.append(row)
data = np.array(data)
number_passengers = np.size(data[0::,0].astype(np.float))
number_survived = np.sum(data[0::,0].astype(np.float))
proportion_survivors = number_survived / number_passengers
Traceback (most recent call last):
File "/Users/scdavis6/Documents/Kaggle/Titanic1.py", line 14, in <module>
number_passengers = np.size(data[0::,0].astype(np.float))
IndexError: too many indices
Let me know if I can provide additional information.
Thank you.
Update:
I made the edits, but got another error about the module not being callable:
Traceback (most recent call last):
File "/Users/scdavis6/Documents/Kaggle/Titanic1.py", line 5, in <module>
csv_file_object = fileName = os.path('train.csv')
TypeError: 'module' object is not callable
Update:
I changed os.path('train.csv') to os.path.join('train.csv'), but got another error about not finding the .csv file.
Traceback (most recent call last):
File "/Users/scdavis6/Documents/Kaggle/Titanic1.py", line 9, in <module>
with open(fileName) as f:
IOError: [Errno 2] No such file or directory: 'train.csv'
Here's the absolute path for the .csv file and the python scripts.
import os
os.path.abspath("/Users/scdavis6/Desktop/train.csv")
'/Users/scdavis6/Desktop/train.csv'
import os
os.path.abspath("/Users/scdavis6/Documents/Kaggle/Titanic1.py")
'/Users/scdavis6/Documents/Kaggle/Titanic1.py'
Assuming that this is your actual code, the problem is that you never open the file. Your csv_file_object is still just the fileName, and thus your data is made up of the characters of that file name, resulting in a 1D numpy array.
Instead, you should open the file and create a csv.reader for it.
import csv
with open(fileName) as f:
reader = csv.reader(f)
data=[]
for row in reader:
data.append(row)
data = np.array(data)
Or shorter: data = np.array([row for row in csv.reader(f)])
Update: The new error you are getting is probably due to you accidentally changing
os.path.join('train.csv') to os.path('train.csv'), i.e., instead of calling the join function from the os.path module, you are (trying to) call the module itself.
Update: It seems your train.csv file is not in the same directory as your Python script, thus the script won't find the file if you just use the filename. You have to use the absolute path together with the filename:
fileName = os.path.join('/Users/scdavis6/Desktop', 'train.csv')
Or just fileName = '/Users/scdavis6/Desktop/train.csv'. Alternatively, move your train.csv file to the same directory as your Python script. This might indeed be the better and more robust option, unless you are using this file in multiple scripts in different directories.
Related
I am a beginner at python and trying to use a very simple shutil module (shutil.copy) to copy databases from multiple folders into a backup folder. I am getting the error below. Any help is appreciated.
# importing os module
import os
#import time module
import time
import datetime
# importing shutil module
import shutil
now = datetime.datetime.now()
timestamp = str(now.strftime("%Y%m%d_%H%M%S"))
source5 = "F:/SHARED/SOP/PRE GO LIVE/TEST CASES & SCENARIOS/MASTER/PRE_GO_LIVE_MASTER.accdb"
dest5 = "F:/SHARED/SOP/SB/Python/Destination/PRE_GO_LIVE_MASTER.accdb_"+timestamp+".accdb"
print("Before copying ")
DB5 = shutil.copy(source5,dest5)
print("After DATABASE has been copied")
Error:
Traceback (most recent call last):
File "C:\Users\sbasava1\Desktop\Python\Final_Attempt.py", line 101, in <module>
DB5 = shutil.copy(source5,dest5)
File "C:\Python27\lib\shutil.py", line 119, in copy
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 22] invalid mode ('rb') or filename:
Check your file path, use double backslashes \\ or a single forward slash / or make your string a raw string r"...".
# Original path - wouldn't work
path = "c:\location\directory"
# Raw string - would work
path = r"c:\location\directory"
# Double slashes - would work
path = "c:\\location\\directory"
# Forward slashes - would work
path = "c:/location/directory"
Consider learning about string literals
If this didn't help leave me a comment, it would also help to see the/part of the code you are working with!
Edit: Running your script didn't give me an issue:
Check if the directory you are trying to create a file in actually exists
I see following error when executing this python code. What is issue here?
I have used "sys.stdout.close()" still I see these errors.
#! /usr/bin/python
import sys
a = [ 10, 12, 13, 14]
sys.stdout=open("file.txt","w")
print("++++++++")
print("***xyz***")
print("++++++++")
sys.stdout.close()
for i in a:
print i
Output:
Traceback (most recent call last):
File "./test3.py", line 10, in <module>
print i
ValueError: I/O operation on closed file`
You are trying to write to stdout (your file) after closing it. At line 8 you close the file, and at line 10 you call print.
If you want to write the list a to the file you should close it after the for loop.
Consider using with open because you don't have to worry about closing it. If your list needs to be a list then consider pickling it instead of writing it to a file. Pickling serializes your data.
#!python3
# import module
from os import system
import pickle
# clear the screan
system('cls')
a = [ 10, 12, 13, 14]
# write a list to file, but it has to be written as a string
with open('file.txt', 'w') as wf:
wf.write(str(a))
# when you open your file up, the data is a string
with open('file.txt', 'r') as fp:
for item in fp:
print(item)
print(type(item))
# if you want to retain your data as a list, then pickle it
output = open('file.pkl', 'wb')
pickle.dump(a, output)
output.close()
# open up a pickled file
pkl_file = open('file.pkl', 'rb')
data = pickle.load(pkl_file)
print(data)
print(type(data))
pkl_file.close()
I am currently using the following versions
Python - 2.7.10 ( 32 bit , win)
AndroidViewClient - androidviewclient-10.7.1-py2.7.egg
I have a simple program as below
import sys
import os
try:
sys.path.insert(0, os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))
except:
pass
from com.dtmilano.android.viewclient import ViewClient
device, serialno = ViewClient.connectToDeviceOrExit()
vc = ViewClient(device=device, serialno=serialno)
device.takeSnapshot().save('Menu.png','PNG')
This is giving me the following error
*Traceback (most recent call last):
File "dump.py", line 14, in <module>
device.takeSnapshot().save('Menu.png','PNG')
File "C:\Python27\lib\site-packages\androidviewclient-10.7.1-py2.7.egg\com\dtmilano\android\adb\adbclient.py", line 678, in takeSnapshot
image = Image.open(stream)
File "C:\Python27\lib\site-packages\PIL\Image.py", line 2126, in open
% (filename if filename else fp))
IOError: cannot identify image file <cStringIO.StringI object at 0x023462A8>*
The Same snippet code - works for some devices and for some it doesnt
How can i figure out what is wrong with the devices where it doesnt work
Also please help me idetify any configuration issues as i am new to this
For each item in a list, I am modifying a file and then I want to run the python script, "ncall.py", which imports the modified file called, "new_basketparams.txt". I also want to make sure ncall.py completes its process before recalling it, but that is perhaps another topic. I am working on an ubuntu linux machine with python 2.7.
Here's the issue: My code seems to call ncall.py infinite times, stuck inside of the loop. I cannot even use a keyboard shortcut to kill the process and must close the window to kill the process. Here is my code and if anyone can help explain a solution, or guide me, that would be greatly appreciated:
import os
import re
datelist=['2014-05-16','2014-05-15','2014-05-14','2014-05-13','2014-05-12']
for date in datelist:
f=open('basketparams.txt')
g=open('new_basketparams.txt','w')
for line in f:
if "Start" in line:
line=line.split(";")
line[2]=re.sub('\d\d\d\d-\d\d-\d\d',date,line[2])
line=';'.join(line)
elif "End" in line:
line=line.split(";")
line[2]=re.sub('\d\d\d\d-\d\d-\d\d',date,line[2])
line=';'.join(line)
else:
pass
g.write(line)
os.system("python ncall.py")
Here are some diagnostics. The import MySQL error is strange because ncall works when called by itself from the terminal
'import site' failed; use -v for traceback
Traceback (most recent call last):
File "ncall.py", line 1, in <module>
import MySQLdb
ImportError: No module named MySQLdb
[]
calling ncall.py no. 1 2014-05-21 07:30:46.400011
new_basketparams.txt
Traceback (most recent call last):
File "ncall.py", line 15, in <module>
Price_Min=display.input[2]
IndexError: list index out of range
[]
calling ncall.py no. 1 2014-05-21 07:30:46.352573
new_basketparams.txt
Traceback (most recent call last):
File "ncall.py", line 15, in <module>
Price_Min=display.input[2]
IndexError: list index out of range
[]
calling ncall.py no. 1 2014-05-21 07:30:46.305043
new_basketparams.txt
Traceback (most recent call last):
File "ncall.py", line 15, in <module>
Price_Min=display.input[2]
IndexError: list index out of range
Now I am including a portion of code from ncall.py as well as display.py:
#ncall.py
import MySQLdb
import csv
import display
import time
from display import fileinput
from datetime import datetime, date, time, timedelta
import datelist
now0=datetime.now()
print fileinput
Start_Datetime='%s 14:00:00'%(datelist.date)
End_Datetime='%s 14:59:59'%(datelist.date)
Price_Min=display.input[2]
Price_Max=display.input[3]
Under=display.input[4]
#display.py
fileinput='new_basketparams.txt'
f=open(fileinput)
input=[]
for line in f:
print 'reading line in fileinput in display.py\'s loop: ', line
try:
line=line.split(';')
parameter=line[0]
Input=line[2]
x=Input.split('\r')
input.append(x[0])
except :
continue
I am getting an error when trying to import some data into my model. The error I'm getting is TypeError: complaining about the delimiter I'm using.
Below is my model for the CSV import, I am using the default delimiter suggested by the documentation.
class SkuCsvModel(CsvModel):
sku_num = models.CharField()
sku_category = models.ForeignKey(SkuCategory)
short_desc = models.CharField()
class Meta:
delimiter = ";"
dbModel = Sku
The CSV file I'm trying to use is below:
1365400;9;3/8 BALL VALVE
1401901;9;BRASS ELBOW
1406300;9;HOSE BARB, NPT
The code I'm testing in the manage.py shell is:
>>> from core.models import SkuCsvModel
>>> my_csv_list = SkuCsvModel.import_data(data = open("labconco.csv"))
And finally the error I'm getting is:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "E:\bin\Python27\lib\site-packages\adaptor\model.py", line 197, in import_data
return importer.import_data(data)
File "E:\bin\Python27\lib\site-packages\adaptor\model.py", line 466, in import_data
for line in csv.reader(data, delimiter=self.delimiter):
TypeError: "delimiter" must be an 1-character string
So I've been fiddling around with the django-adaptor tools, and this error is coming from the import_data() method of the CsvImporter, when I try and put a delimiter directly into the csv.reader(data, delimiter=';') this works fine and I'm able to see the file correctly. But no matter how I try and enter this import_data method sending in a ';' will generate an error.
Look at the snippet below. If I provide an integer as a delimiter, it fails with the same exception as in your example. If I provide a semicolon as a delimiter to csv.reader it works. This is basically what is done in model.CsvImporter.import_data() as you already found out.
>>> import csv
>>> import StringIO
>>> io = StringIO.StringIO('name;surname\nsascha;gottfried')
>>> for line in csv.reader(io, delimiter=10):
... print line
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: "delimiter" must be an 1-character string
>>> for line in csv.reader(io, delimiter=';'):
... print line
...
['name', 'surname']
['sascha', 'gottfried']
To debug the situation dump the current value of 'self.delimiter' to the console or similar before it will be passed as a delimiter to csv.reader(). It must be a different value and/or type than ';'. Looking at the code of django-adaptors you can validate your django-adaptors model definition with this base class method as well to debug. This call should print out what you defined as Meta.delimiter.
>>> from core.models import SkuCsvModel
>>> SkuCsvModel.has_class_delimiter()
But it is pretty fine to omit a delimiter definition and call 'import_from_file' on the model. Make sure there is no class delimiter defined. If so the importer runs a CSV sniff to detect the delimiter from the file you passed. If you provide the file you mentioned, the sniffer will detect a ';' and sets self.delimiter.
>>> from core.models import SkuCsvModel
>>> SkuCsvModel.has_class_delimiter()
None
>>> my_csv_list = SkuCsvModel.import_from_file(file = open("labconco.csv"))