Normally when I open a CSV file in Python, I need to use:
with open(filename, newline='', mode='w') as f:
And if I don't have that newline='' in there, it creates an empty line between each line in my CSV. However, I am using Tkinter to save the file, so I have:
new_filename = asksaveasfile(mode='w', defaultextension='.csv')
Since "new_filename" is already open, I can't do the "open" command to indicate the newline='' in there. If I try opening it again, I get an error. So how do I get rid of the extra spaces in this case?
Thanks for your help and patience.
you have some other problem regarding the new line parameter - I don't have to use it at all here. But for your tkinter problem, you can use asksaveasfilename instead. That returns the selected file name, then you can open it in any way you want.
Related
So have a "file.csv" and I want to extract some data from it using Python.
The problem for me is that the lines are formatted not in standard csv format, but instead a line looks like this:
;a_value=value;b_value=value;
So my idea was to use .replace() to edit everything in a line so that in the end it looks as wished:
'a_value':'value', 'b_value':'value'
In order to iterate and modify every line I followed the idea from this question from Modify csv files?
But I get:
AttributeError: 'str' object has no attribute 'readline'
My code:
with open(name_file, "r") as csv_file:
data=name_file.readlines()
csv_file=close()
csv_file=open(name_file, "w")
for row in csv_file:
#Replace commands
f.close()
It's something pretty simple in this case; When you open a file using with open, you need to operate on the file type variable you defined rather than using the path of the file.
In this case, you open the file but then try to read the string based path of the file, rather than the file type.
So you'd need to rewrite your code as such:
with open(name_file, "r") as csv_file:
data=csv_file.readlines()
Hope this helped!
P.S, the close method is only needed when you don't use the with keyword, when you exit the indented block, the file is closed.
I am able to make the text file into a list and sort it alphabetically. I am having trouble saving/inserting this new list into a NEW text file called GirlsNamesSorted.txt. I currently have:
newGirlFile = open('/Users/MacUser/Documents/Python/GirlNamesSorted.txt')
for i in newGirlFile:
j = i.rstrip("\r\n")
girlList.append(j)
girlList.sort(key=str.lower)
newGirlFile.write("\n".join(girlList))
print newGirlFile.read()
newGirlFile.close()
I believe the error is coming from:
newGirlFile.write("\n".join(girlList))
But I am not entirely sure and need help finding the error and fixing it.
To sort your file, just do:
from contextlib import closing
with closing(open('/Users/MacUser/Documents/Python/GirlNamesSorted.txt', 'w+')) as newGirlFile:
girlList=sorted([i.strip() for i in newGirlFile], key=str.lower)
newGirlFile.write("\n".join(girlList))
Note: you should also use with syntax when writing and reading file so the file gets closed properly. In Jython, the context manager requires an additional closing()
By default, open() opens a file as read-only; you need to specify the mode, in this case w+ for writing and reading, as the second parameter.
newGirlFile = open('/Users/MacUser/Documents/Python/GirlNamesSorted.txt', 'w+')
It is not a good practice to read input from a file and write output to the same file.
Please crate new file to write the output as mentioned below,
newGirlFile = open('/Users/MacUser/Documents/Python/GirlNamesSorted.txt','r')
out_file = open('/Users/MacUser/Documents/Python/GirlNamesSortedOutput.txt', 'w')
girlList = []
for i in newGirlFile:
j = i.rstrip("\r\n")
girlList.append(j)
girlList.sort(key=str.lower)
out_file.write("\n".join(girlList))
newGirlFile.close()
out_file.close()
Hope this will works.
Please let me know in terms of any queries.
oldGirlFile = open('/Users/MacUser/Documents/Python/GirlNamesSorted.txt', 'r')
girlList = [line.rstrip("\r\n") for line in newGirlFile.readlines()]
oldGirlFile.close()
girlList = girlList.sort(key=str.lower)
newGirlFile = open('/Users/MacUser/Documents/Python/GirlNamesSortedNew.txt', 'w')
newGirlFile.write("\n".join(girlList))
newGirlFile.close()
Then you can open the file in your text editor and check its content.
This is similar or identical to csv writer not closing file but I'm not 100% sure why my behaviour is different.
def LoadCSV:
with open('test.csv', 'r') as csvfile:
targetReader = csv.reader(csvfile, delimiter=',')
for row in targetReader:
...
then finally in the function
csvfile.close()
This opens the test.csv file in the same direction as the script. Desired behaviour is for when the script has done what it's doing to the rows in the function, it renames the sheet to test.[timestamp] to archive it and watches the directory for a new sheet to arrive.
Later down the code;
os.rename('test.csv', "test." + time.strftime("%x") )
Gives an error that the file can't be renamed because a process is still using it. How do I close this file once I'm done? csvfile.close() doesn't raise an exception, and if I step through my code in interactive mode I can see that csvfile is a "closed file object." What even is that? Surely an open file is an object but a closed one isn't, how do I make my code forget this even exists so I can then do IO on the file?
NOT FOR POINTS.
Code is not valid anyway, since your function name is wrong. If that was not intentional, better edit it or to produce a pseudo-replica of your code, rather than have us guess what the issue is.
To iterate, the issues with your code:
def LoadCSV is not valid. def LoadCSV() is. Proof in following screenshot. Notice how the lack of () is showing syntax error markers.
Fixing (1) above, your next problem is using csvfile.close(). If the code is properly written, once the code is out of the scope of with, the file is closed automatically. Even if the renaming part of the code is inside the function, it shouldn't pose any problems.
Final word of warning -- using the format string %x will produce date-strings like 08/25/14, depending on locale. Obviously, this is erroneous, as a / is invalid in filenames in Windows (try renaming a file manually with this). Better to be very explicit and just use %m%d%y instead.
Finally, see the running code on my end. If your code is not structured like this, then other errors we cannot guess might arise.
Result as follows after running:
Code for reference:
import csv
import os
import time
def LoadCSV():
with open("test.csv", "r") as csvfile:
targetReader = csv.reader(csvfile, delimiter=",")
for row in targetReader:
print row
new_name = "test.%s.csv" % time.strftime("%m%d%y")
print new_name
os.rename("test.csv", new_name)
LoadCSV()
Note that on my end, there is nothing that watches my file. Antivirus is on, and no multithreading obviously is enabled. Check if one of your other scripts concurrently watches this file for changes. It's better if instead of watching the file, the file is sent as an argument post-renaming to this other function instead, as this might be the reason why it's "being used". On the one hand, and this is untested on my side, possibly better to copy the file with a new name rather than rename it.
Hope this helps.
When you are using a with block you do not need to close the file, it should be released outside the scope. If you want python to "forget" the entire filehandle you could delete it with del csvfile. But since you are using with you should not delete the variable inside the scope.
Try without the with scope instead:
csvfile = open('test.csv','r')
targetReader = csv.reader(csvfile, delimiter=',')
for row in targetReader:
....
csvfile.close()
del targetReader
os.rename('test.csv','test.'+time.strftime('%x'))
It might be the csv reader that still access the file when you are using a with block.
I am a beginner, writing a python script in which I need it to create a file that I can write information to. However, I am having problems getting it to create a new, not previously existing file.
for example, I have:
file = open(coordinates.kml, 'w')
which it proceeds to tell me:
nameerror: name 'coordinates' is not defined.
Of course it isn't defined, I'm trying to make that file.
Everything I read on creating a new file says to take this route, but it simply will not allow me. What am I doing wrong?
I even tried to flat out define it...
file = coordinates.kml
file_open = open(file, 'w')
... and essentially got the same result.
You need to pass coordinates.kml as a string, so place them in quotes (single or double is fine).
file = open("coordinates.kml", "w")
In addition to the above answer,
If you want to create a file in the same path, then no problem or else you need to specify the path as well in the quotes.
But surely opening a file with read permission will throw an error as you are trying to access an nonexistent file.
To be future proof and independent of the platforms you can read and write files in binaries. For example if this is Python on Windows, there could be some alternations done to the end of line. Hence reading and writing in Binary mode should help, using switches "rb" and "wb"
file = open("coordinates.kml", "wb")
And also remember to close the file session, else can throw errors while re running the script.
how can I clear a complete csv file with python. Most forum entries that cover the issue of deleting row/columns basically say, write the stuff you want to keep into a new file. I need to completely clear a file - how can I do that?
Basically you want to truncate the file, this can be any file. In this case it's a csv file so:
filename = "filewithcontents.csv"
# opening the file with w+ mode truncates the file
f = open(filename, "w+")
f.close()
Your question is rather strange, but I'll interpret it literally. Clearing a file is not the same as deleting it.
You want to open a file object to the CSV file, and then truncate the file, bringing it to zero length.
f = open("filename.csv", "w")
f.truncate()
f.close()
If you want to delete it instead, that's just a os filesystem call:
import os
os.remove("filename.csv")
The Python csv module is only for reading and writing whole CSV files but not for manipulating them. If you need to filter data from file then you have to read it, create a new csv file and write the filtered rows back to new file.