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
Related
I cannot import mysql.connector in python 2.7.12. I cannot upgrade my python version, because some other servers are running on it.
>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\mysql\connector\__init__.py", line 53, in <module>
from .connection import MySQLConnection
File "C:\Python27\lib\site-packages\mysql\connector\connection.py", line 518
f"This connection is using {tls_version} which is now "
^
SyntaxError: invalid syntax
This is the error I am getting even though I installed mysql-connector-python. Can someone please help me with this?
As I stated in my comment, you're most likely using a more recent mysql-connector-python.
From the list of version in PyPI, this might be the last one that supports 2.7:
https://pypi.org/project/mysql-connector-python/8.0.23/
This docs discusses that 2.7 was removed in 8.0.24:
Using cmd on windows 10 to run Python 2.7.16. When I run a .py file which imports scipy.py it comes up with the error:
"File "C:\Python27\scipy.py", line 18, in
from .python import string_types
ValueError: Attempted relative import in non-package"
As string_types should already be included in python, I don't understand why this error is happening and cant seem to get passed it. Does anyone know how to fix this?
you tried,
>>> from . import MyModule #MyModule.py
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Attempted relative import in non-package
>>>
here . mean your current path
try,
from MyModule import *
now you can use string_types
or
import MyModule as m
m.string_types
I found a problem in the python program.I don't know what I have to do.
Cordially.
import pyautogui
im1 = pyautogui.screenshot()
im2 = pyautogui.screenshot('my_screenshot.png')
Traceback (most recent call last):
File "C:/Users/LENOVO/Desktop/yyyu.py", line 1, in <module>
import pyautogui
File "C:\Python27\lib\site-packages\pyautogui\__init__.py", line 84, in <module>
import pyscreeze
SyntaxError: 'return' with argument inside generator (__init__.py, line 168)
You probably get this error because you use python 2, try to use python 3
I am new to python and I am trying to work on a project with deep learning and want to use graphlab library. I use sublime text for coding on windows 10. My code is only this line:
import graphlab
I get this error msg:
Traceback (most recent call last):
File "test.py", line 1, in
import graphlab
File "graphlab__init__.py", line 59, in
from graphlab.data_structures.sgraph import Vertex, Edge
File "graphlab\data_structures__init__.py", line 25, in
from . import sframe
File "graphlab\data_structures\sframe.py", line 19, in
from ..connect import main as glconnect
File "graphlab\connect\main.py", line 26, in
from ..cython.cy_unity import UnityGlobalProxy
ImportError: No module named cy_unity
Try using graphlab.get_dependencies() in your interpreter.
I think I made a mistake when installing, i re-installed and it worked fine. Thanks anyway
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.