I am trying to run the script csv2json.py in the Command Prompt, but I get this error:
C:\Users\A\Documents\PROJECTS\Django\sw2>csv2json.py csvtest1.csv wkw1.Lawyer
Converting C:\Users\A\Documents\PROJECTS\Django\sw2csvtest1.csv from CSV to JSON as C:\Users\A\Documents\PROJECTS\Django\sw2csvtest1.csv.json
Traceback (most recent call last):
File "C:\Users\A\Documents\PROJECTS\Django\sw2\csv2json.py", line 37, in <module>
f = open(in_file, 'r' )
IOError: [Errno 2] No such file or directory: 'C:\\Users\\A\\Documents\\PROJECTS\\Django\\sw2csvtest1.csv'
Here are the relevant lines from the snippet:
31 in_file = dirname(__file__) + input_file_name
32 out_file = dirname(__file__) + input_file_name + ".json"
34 print "Converting %s from CSV to JSON as %s" % (in_file, out_file)
36 f = open(in_file, 'r' )
37 fo = open(out_file, 'w')
It seems that the directory name and file name are combined. How can I make this script run?
Thanks.
Edit:
Altering lines 31 and 32 as answered by Denis Otkidach worked fine. But I realized that the first column name needs to be pk and each row needs to start with an integer:
for row in reader:
if not header_row:
header_row = row
continue
pk = row[0]
model = model_name
fields = {}
for i in range(len(row)-1):
active_field = row[i+1]
So my csv row now looks like this (including the header row):
pk, firm_url, firm_name, first, last, school, year_graduated
1, http://www.graychase.com/aabbas, Gray & Chase, Amr A, Babas, The George Washington University Law School, 2005
Is this a requirement of the django fixture or json format? If so, I need to find a way to add the pk numbers to each row. Can I delete this pk column? Any suggestions?
Edit 2
I keep getting this ValidationError: "This value must be an integer". There is only one integer field and that's the pk. Is there a way to find out from the traceback what the line numbers refer to?
Problem installing fixture 'C:\Users\A\Documents\Projects\Django\sw2\wkw2\fixtures\csvtest1.csv.json': Traceback (most recent call last):
File "C:\Python26\Lib\site-packages\django\core\management\commands\loaddata.py", line 150, in handle
for obj in objects:
File "C:\Python26\lib\site-packages\django\core\serializers\json.py", line 41, in Deserializer
for obj in PythonDeserializer(simplejson.load(stream)):
File "C:\Python26\lib\site-packages\django\core\serializers\python.py", line 95, in Deserializer
data[field.attname] = field.rel.to._meta.get_field(field.rel.field_name).to_python(field_value)
File "C:\Python26\lib\site-packages\django\db\models\fields\__init__.py", line 356, in to_python
_("This value must be an integer."))
ValidationError: This value must be an integer.
+ is used incorrectly here, the proper way to combine directory name and file name is using os.path.join(). But there is no need to combine directory where script is located with file name, since it's common to pass relative path to current working directory. So, change lines 31-32 to the following:
in_file = input_file_name
out_file = in_file + '.json'
from os import path
in_file = path.join(dirname(__file__), input_file_name )
out_file = path.join(dirname(__file__), input_file_name + ".json" )
[...]
You should be using os.path.join rather than just concatenating dirname() and filenames.
import os.path
in_file = os.path.join(dirname(__file__), input_file_name)
out_file = os.path.join(dirname(__file__), input_file_name + ".json")
will fix your problem, though depending on what exactly you're doing, there's probably a more elegant way to do it.
Related
i have this problem: i'm tryna list all filenames from a directory and then print them with numbers on the left to let users select the file to use. Numbers are there because they match with the index of the position of filenames in the list. When i select a specific string filename in the list and pass it to
`f = open (filename, "r")
data = json.loads(f.read())`
i get:
Traceback (most recent call last):
File "test.py", line 170, in
data = json.loads(f.read())
AttributeError: 'str' object has no attribute 'loads'
full code:
jsons=glob.glob("*.json")
print(type(jsons))
print(type(jsons[0]))
n=0
if(jsons):
for json in jsons:
print(str(n) + ' - ' + json)
n=n+1
jsonselection=int(input('select your settings file: '))
filename=(" ".join(jsons[jsonselection].split()))
print()
print('your selection: ' + filename)
else:
sys.exit(colored('no json files available.','red'))
f = open (filename, "r")
data = json.loads(f.read())
actually if i pass to the method a random variable defined by me like name='file' it works.. i just can't understand why. Thanks in advance for the help
That would most likely be because of this line of code.
jsons=glob.glob("*.json")
and then you have
for json in jsons:
I am trying to add date in my file name but it is not giving me the correct result. Here is my code.
dates=str(date.today())
f = open('C:Study\CSV'+dates+'output.txt','a')
f.write('\n' + results)
f.close
the file name is generated as CSV2018-01-25output.txt. how to create the file name without the word CSV?
The error is here:
f = open('C:Study\CSV'+dates+'output.txt','a')
Because "CVS" in this case is your file name, i presume you wanna this:
2018-01-25output.txt
In this case try this:
name = 'C:Study\',dates+"output.txt"
f = open( name'a')
I'm trying to make a number of text files within a loop and naming them with respect to their number like data1.txt, data2.txt and so forth.
I = 0
while I < 4:
file_name = "data" + str(I) + ".txt"
with open(file_name, 'w') as L:
L.write('stuffIWannaWrite')
I += 1
But when I run this code, it says that the file cannot be found:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'data0.txt'
any help?
EDIT
i'm working on a virtualenv for a scraping project..
the problem only arises when the file name is in iteration like,
file_name = "data" + str(I) + ".txt" in which I is being iterated ,
the code works fine on a simple file name like..
file = open("try.txt", 'w')
file.write(main_stuff)
i.e text file is being created..
I guess this code will do the trick.
import numpy as np
list1=list(np.arange(10))
for num in list1:
with open('data%d.txt'%num,'a') as in_file:
in_file.write("stuff you want to write")
I have two files in a directory. One is a .CSV file and other is a Python script. Python code looks like this:
from pyx import *
import csv
import re
import sys
def write():
name = raw_input('Enter the name of .dat file: ') + '.dat'
file = open(name, "w")
for i in range(0, len(x_lista)-1):
file.write(x_lista[i])
file.write(" ")
file.write(y_lista[i])
file.write("\n")
file.close()
def read_CSV(x_lista, y_lista):
currency = raw_input('Enter the name of input .CSV file: ') + '.CSV'
#print currency
with open(currency, 'rb') as f:
reader = CSV.reader(f)
lista = list(reader)
print lista
if(currency == 'Frank' or 'USD'):
factor = 4
else:
factor = 3
for i in range (3, len(lista)-factor):
temp = (re.split(r'[";"]', (';'.join(lista[i]))))
temp1 = temp[0]
x_lista.append(temp1)
temp1 = temp[1]
y_lista.append(temp1)
print x_lista, y_lista
x_lista = []
y_lista = []
read_CSV(x_lista, y_lista)
write()
It takes what's in .CSV and by splitting/joining lists it produces a .DAT file consisting of two columns of data. Well... it does on Windows. However, when I try to compile it on Ubuntu I get this:
Enter the name of input .CSV file: Euro
Traceback (most recent call last):
File "nwb.py", line 46, in <module>
read_CSV(x_lista, y_lista)
File "nwb.py", line 22, in read_CSV
with open(currency, 'rb') as f:
IOError: [Errno 2] No such file or directory: 'Euro.CSV'
What would be the solution?
In Unix system file names are case sensitive.
For example: Euro.CSV and Euro.csv are different file names. Maybe the error is shown because of that
I'm trying to create a file with a unique file name for every time my script runs. I am only intending to do this to every week or month. so I chose to use the date for the file name.
f = open('%s.csv', 'wb') %name
is where I'm getting this error.
Traceback (most recent call last):
File "C:\Users\User\workspace\new3\stjohnsinvoices\BabblevoiceInvoiceswpath.py", line 143, in <module>
f = open('%s.csv', 'ab') %name
TypeError: unsupported operand type(s) for %: 'file' and 'str'
it works if I use a static filename, is there an issue with the open function, that means you can't pass a string like this?
name is a string and has values such as :
31/1/2013BVI
Many thanks for any help.
You need to put % name straight after the string:
f = open('%s.csv' % name, 'wb')
The reason your code doesn't work is because you are trying to % a file, which isn't string formatting, and is also invalid.
you can do something like
filename = "%s.csv" % name
f = open(filename , 'wb')
or f = open('%s.csv' % name, 'wb')
Very similar to peixe.
You don't have to mention the number if the variables you add as parameters are in order of appearance
f = open('{}.csv'.format(name), 'wb')
Another option - the f-string formatting (ref):
f = open(f"{name}.csv", 'wb')
And with the new string formatting method...
f = open('{0}.csv'.format(name), 'wb')
Even better are f-strings in python 3!
f = open(f'{name}.csv', 'wb')
import hashlib
filename = file_for_download
with open(filename, "rb") as f:
bytes = f.read() # read entire file as bytes
msg_hash = hashlib.sha256(bytes).hexdigest();
print(f"MSG_HASH = {msg_hash}")