I'm attempting to run an R script from python. I can see it's executing, but python is erroring out on a syntax error for the R script. The R script runs fine in RStudio, so I'm not sure what the issue might be. Any suggestions would be helpful.
This is my python code:
import sys
import subprocess
# other stuff going on
# execute R script which returns a document in a directory
subprocess.call([sys.executable, "C:/Users/path/sentiment_extraction.r"])
sentiment = pd.read_csv("new_sentiment_data.csv")
And here is my R script:
#! /usr/bin/Rscript
require(syuzhet)
today_news <- read.csv("C:/Users/path/Today_News.csv") # created from above python script
#Sentiments words table:
records <- as.character(today_news$News)
sentiment <- get_nrc_sentiment(records)
# other stuff that makes a file
Then this is the Python error I get:
File "C:/Users/path/sentiment_extraction.r", line 7
records <- as.character(today_news$News)
^
SyntaxError: invalid syntax
The data I use syuzhet on needs to be a character vector so I cast the column.
I figured it out. My parameters to run the R file were incorrect. I had to pass the path to Rscript.exe:
subprocess.call(["C:/Users/seeme/Documents/R/R-3.4.0/bin/Rscript",
"C:/Users/path/sentiment_extraction.r"])
Related
what's wrong with this line of code?
whenever I run it it shows syntax error 'mysql' check the corresponding server version.
import mysql.connector
import builtins
import importlib.util
import os
v=os.getcwd()
loc=""
for i in v:
if i == "\\":
loc += "/"
else:
loc += i
print(loc)
def crt():
m=mysql.connector.connect(host="localhost",user="root",passwd="****")
mc=m.cursor()
mc.execute("create database if not exists mydb")
crt()
m=mysql.connector.connect(host="localhost",user="root",passwd="****",database="mydb")
mc=m.cursor()
mc.execute("source "+loc+"/mydb.sql;")
normally for mysql when I apply the following code it works.
use mydb;
source C:\Users\15fri\OneDrive\Desktop\s3ts\mydb.sql
as for the for loop section in the previous code I converted the backslashes to forward slashes since some time the location in the code even works with forward slashes..
Your problem is probably that source is not an SQL command and that it's only available from within the mySQL CLI.
Try reading the script file and passing its contents as a string to mc.execute()
In response to your comment CrazY JoN, I'm no Python programmer but I was thinking of something like this:
sql_script_file = open("source "+loc+"/mydb.sql;",'r')
sql_script = sql_script_file.read()
sql_commands = sql_script.split(";") # assuming the commands in the script have to be executed one at a time
for cmd in sql_commands:
mc.execute(cmd)
I have a bash script which extracts a variable (an integer) calculated in a Python script. I then try to do an arithmetic operation on this variable, but Bash seems to have trouble with this.
Python script:
import sys
foo = 278736 # This number is obtained by reading a file using this Python script
print(foo)
sys.exit(0)
Bash script:
#!/bin/bash
var=`python python_script.py`
var2=$((var/100)) # Line which causes the problem
This produces this error:
-bash: 278736: syntax error: operand expected (error token is "278736")
It seems that somehow, bash doesn't know how to convert the string "278736", extracted from the Python script, to an integer. If I do an arithmetic operation with a normal Bash variable, it works fine:
var_test=278736
var2=$((var_test/100)) # Works fine
I have searched, but can't find anybody who has asked the same question. Does anyone know how to make this work?
I work interactively on data in a ipython console. I have a small script which I can use to Import new data, but normally I want to keep the old data in RAM too. So what I did is to add the following piece of code to the Import script:
try:
data_list.append(data)
except NameError:
data_list = [data]
I execute this script from the ipython console with run -i scriptname.py. This adds the new data to the data_list, or creates a new list if it doesn't exist. This works basically fine.
Now the question: in eclipse/pydev, I get an error sign, because the Parser recognizes that data_list has not be assigned before. Is there any way to make the error sign disappear?
I have the following python script which i want to run..
However, it keeps showing the error message on my command prompt whenever i attempt to run the script.
Error message:
File "xor.py", line 9
File = open(sys.argv[1], 'rb').read<>
SyntaxError: Invalid Syntax
The following is the command i executed in cmd:
python xor.py sample_output.txt 'what would the secret be?'
The following is the script:
# xor.py
import sys
from itertools import cycle
file = open(sys.argv[1], 'rb').read()
string = sys.argv[2]
sys.stdout.write(''.join(chr(ord(x)^ord(y)) for (x,y) in zip(file, cycle(string))))
You are not running the code you are editing, instead you are running a different file than the one you edited.
This is because there is no syntax error in the code that you have provided. However, there is a syntax error in the code in the error message:
File = open(sys.argv[1], 'rb').read<>
This ends with <>, not with (). I assumed this to be a transcription error, but you say that the error message really appears like this, although the code does not.
Hence: You are running a different file than the one you are editing.
You have .read<> when you probably intended .read()
First of all, "file" is already reserved; that is built-in keyword so unable to set as the name of variable.
And second, do not use <> instead of (). incorrect in grammar.
The problems might be clearly solved if you code like:
fd = open(sys.argv[1], 'rb').read()
I have tried to use this code: http://www.cgtk.co.uk/data/gemf/generate_efficient_map_file.py to create GEMF file from map tiles. Problem is, when I run this code with specific folder as a parameter, eg. "py generate_efficient_map_file.py Mapnik", I get error on line 6.
File "generate_efficient_map_file.py", line 6 file_size_limit = 2000000000L
And mistake is in word L. How to solve this, when there is a declaration error?
Thx
Try running it like this
python generate_efficient_map_file.py dirname
This works fine for me (no line 6 error - I don't have input data) on python 2.7.3. You can check your python version with
python -V