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')
Related
Here is the code and I am trying to see what I have done wrong. I am new to python functions and linking external files so it would be nice if you could explain your code.
def get_data(filename):
records = []
with open(filename) as readfile:
lines = readfile.readlines()
for line in lines:
# variable line contains:
str_rec = line.split(",")
pname = str_rec[0]
price = int(str_rec[1])
quantity = int(str_rec[2])
records.append([pname, price, quantity])
#caution: indentation
return records
hell= get_data(data.txt)
print(hell)
data.txt is a link to another file that I am trying to pass as an argument.
open(filename) takes the filename as a string, so you should pass the name as a string, not the actual file.
hell= get_data("data.txt")
So how can I ask the user to provide me with an input file and an output file?
I want the content inside the input file provided by the user to print into the output file the user provided. In this case, the user would put in this
Enter the input file name: copyFrom.txt
Enter the output file name: copyTo.txt
inside the input file is just the text "hello world".
Thanks. Please keep it as simple as you can if possible
If you just want to copy the file, shutil’s copy file does the loop implicitly:
import os
from shutil import copyfile
openfile = input('Enter the input file name:')
outputfile = input('Enter the output file name:')
copyfile(openfile, outputfile)
This this post How do I copy a file in Python? for more detail
Here is an example that should work in Python3. The input and output file names would need to include the full path (i.e. "/foo/bar/file.txt"
import os
input_file = input('Enter the input file name: ')
output_file = input('Enter the output file name: ')
def update_file(input_file, output_file):
try:
if os.path.exists(input_file):
input_fh = open(input_file, 'r')
contents = input_fh.readlines()
input_fh.close()
line_length = len(contents)
delim = ''
if line_length >= 1:
formatted_contents = delim.join(contents)
output_fh = open(output_file, 'w')
output_fh.write(formatted_contents)
output_fh.close()
print('Update operation completed successfully')
except IOError:
print(f'error occurred trying to read the file {input_fh}')
update_file(input_file, output_file)
You can do this...
import os
openfile = input('Enter the input file name:')
outputfile = input('Enter the output file name:')
if os.path.isfile(openfile):
file = open(openfile,'r')
output = open(outputfile,'w+')
output.write(file.read())
print('File written')
exit()
print('Origin file does not exists.')
To input the input-file and output-file names, simply use the input(s) function where s is the input message.
To get the "content inside the input file provided by the user to print into the output file," that would mean reading the input file and writing the read data into the output file.
To read the input file, use f = open(input_filename, 'r'), where the first argument is the filename and the second argument is the open mode where 'r' means read. Then letting readtext be the read text information of the input file, use readtext = f.read(): this returns the entire text content of f.
To output the read content to the output file, use g = open(output_filename, 'w'), noting that now the second argument is 'w', meaning write. To write the data, use g.write(readtext).
Please note that an exception will be raised if the input file is not found or the output file is invalid or not possible as of now. To handle these exceptions, use a try-except block.
This is effectively a file-copying operation in Python. shutil can serve as a useful alternative.
First you have to read the file and save it to some variable (here rd_data):
if os.path.exists(input_file_name):
f = open(input_file_name,"r")
rd_data = f.read()
f.close()
Then you have to write the variable to other file:
f = open(output_file_name,"w")
f.write(rd_data)
f.close()
The full code is given below:
import os
input_file_name = input("Enter file name to read: ")
output_file_name = input("Enter file name to write: ")
if os.path.exists(input_file_name):
f = open(input_file_name,"r")
rd_data = f.read()
f.close()
f = open(output_file_name,"w")
f.write(rd_data)
f.close()
super new to Python, and looking for some guidance. I'm trying to
loop through hundreds of text files in a folder (one for each store), and generate a CSV file with the store ID (given in the title of the text document i.e. xxx2902ncjc), and various parameters about the store (i.e. maxPeople=31, or space_temp=78, etc.). Each text file may have difference parameters depending on the location, so I've captured all of the unique variables in the third for loop below. I've captured all of the store IDs in the second for-loop. That's all I've gotten so far.
Challenges that I'm seeing are 1) figuring out how to import this all to Excel, 2) Finding someway to store IDs (which are at this point a slice of each filename) with the correct parameters 3) Finding a way to have excel match up the Store ID and the parameters to the variables.
I honestly have no idea what I should be doing next. Any and all help would be very appreciated as I am a suuuper novice. Cheers.
import os, sys, glob
path = r"C:\Users\XXXXX" #insert folder for data here
dirs=os.listdir(path)
fullfilenames=[]
variablelist=[]
allvariables=[]
variables=[]
for file in os.listdir(path):
if ".prop" in file:
fullfilenames.append(path+'\\'+file)
for name in fullfilenames: #create list of StoreIDs
index_of_c = name.index('qPA')
file_name= name[index_of_c:] #cuts off path
file_name=file_name.rsplit(".",1)[0] #removes extension
SiteID= file_name[4:] #splits filename into Site ID
print (SiteID) #prints SiteID
for file in fullfilenames:
f = open(file,'r') #opens the file and enters reading mode
content=f.readlines() #reads each line of file and seperates based on whitespace
for line in content:
variables.append(line.split('=')[0]) #splits up each line of each file, specified before the "="
for variable in variables:
if variable not in allvariables: #checks if variable is included in the variable list
allvariables.append(variable) #if variabe isn't include in the list, it adds it to list
def createkeys():
print(allvariables)
print(type(allvariables))
print(len(allvariables))
import os, sys, glob, re
path = r"C:\Users\mcantwell\Desktop\Projects\kohls_prop" #insert folder for data here
outfile = r"C:\Users\mcantwell\Desktop\Projects\kohls_prop.csv"
dirs=os.listdir(path)
fullfilenames=[]
variablelist=[]
allvariables=set()
variables=[]
for file in os.listdir(path):
if ".prop" in file:
fullfilenames.append(path+'\\'+file)
for file in fullfilenames:
f = open(file,'r') #opens the file and enters reading mode
content=f.readlines() #reads each line of file and seperates based on whitespace
for line in content:
line_split = line.split('=') #splits up each line of each file, specified before the "="
if len(line_split) == 2:
variable = line_split[0]
allvariables.add(variable)
out = open(outfile, 'w')
def writerow(row):
out.write(', '.join(row))
out.write('\n')
writerow(['SiteID'] + list(allvariables))
for file in fullfilenames:
m = re.search('qPAC(\d+)', file)
SiteID = m.group(1)
f = open(file,'r') #opens the file and enters reading mode
content=f.readlines() #reads each line of file and seperates based on whitespace
data={}
for line in content:
line_split = line.strip().split('=') #splits up each line of each file, specified before the "="
if len(line_split) == 2:
variable = line_split[0]
value = line_split[1]
data[variable] = value
values = [SiteID] + [data.get(variable, '') for variable in allvariables]
writerow(values)
print(allvariables)
print(type(allvariables))
print(len(allvariables))
I'm trying to get the script to read a text file of Congress members in which each line is formatted like this:
Darrell Issa (R-Calif)
I want it to print a line to a different file that's formatted like this (notice the added comma):
Darrell Issa,(R-Calif)
For some reason the script below works but it only does it for the first line. How do I get it to execute the loop for each line?
basicfile = open('membersofcongress.txt', 'r')
for line in basicfile:
partyst = line.find('(')
partyend = line.find(')')
party = line[partyst:partyend+1]
name = line[+0:partyst-1]
outfile = open('memberswcomma.txt','w')
outfile.write(name)
outfile.write(",")
outfile.write(party)
outfile.close()
basicfile.close()
print "All Done"
Thank you in advance for your help.
According to documentation,
'w' for only writing (an existing file with the same name will be
erased)
When you open your output file with w, loop keeps creating a new txt file for each line. Using a would be better.
basicfile = open('membersofcongress.txt', 'r')
for line in basicfile:
partyst = line.find('(')
partyend = line.find(')')
party = line[partyst:partyend+1]
name = line[+0:partyst-1]
outfile = open('memberswcomma.txt','a')
outp = name + "," + party + "\n"
outfile.write(outp)
outfile.close()
basicfile.close()
EDIT:
Much better solution would be,
You open your output file at the begining of the loop instead of inside of it.
basicfile = open('membersofcongress.txt', 'r')
outfile = open('memberswcomma.txt','w')
for line in basicfile:
partyst = line.find('(')
partyend = line.find(')')
party = line[partyst:partyend+1]
name = line[+0:partyst-1]
outp = name + "," + party + "\n"
outfile.write(outp)
outfile.close()
basicfile.close()
ok a few things to fix this, use 'a' mode to open your outfile and open it just before the loop, close the outfile after the loop and not inside it.
something like this should work (tested it)
basicfile = open('membersofcongress.txt', 'r')
outfile = open('memberswcomma.txt','a')
for line in basicfile:
partyst = line.find('(')
partyend = line.find(')')
party = line[partyst:partyend+1]
name = line[0:partyst-1]
outfile.write(name)
outfile.write(",")
outfile.write(party)
outfile.close()
basicfile.close()
print "All Done"
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.