I have a script in python 3.6 what parses a .csv file on windows 7. The initial file is a kind of biological analyzer output. I need to make it more human-readable, so I add the comments based on the data in the initial file. I use dictionaries to keep such comments. The script also has a GUI made with Tkinter library.
Everything worked well before I added some extra keys to the dict_2. Now the script cannot be run via double click anymore. However, it still works if I run the script by Thonny.
Are there any ways to overcome this dictionary size limitation?
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
import sys
import subprocess
import os
import csv
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
def callback():
if askyesno('Verify', 'Close?'):
master.destroy()
else:
showinfo('No', 'Continue')
def make_report():
csv_filename = askopenfilename(initialdir = "/",title = "Chose file",filetypes = (("Report","*.csv"),("all files","*.*")))
txt_filename = asksaveasfilename(initialdir = "/",title = "resulting file*.txt",filetypes = (("txt","*.txt"),("all files","*.*")))
if (str(csv_filename)!= "" ) and str(txt_filename)!= "":
with open (str(csv_filename), 'r', newline='', encoding='utf-16') as tsvin:
tsvin = csv.reader (tsvin, delimiter=';')
with open (str(txt_filename) , 'w') as fileOut:
exlc_list = []
dict_1 = {'x1':'x1a', ...., 'x67':'x67a'}
dict_2 = {'y1':'x1a', ...., 'y216':'y216a'}
.....
Related
I am trying to write a python 3.6 script what will parse a .csv file on windows 7. I also need to take a path to my file from a variable (from the keyboard via sys.argv).It was easy when I tried it on linux:
Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:51:32)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
import sys
import csv
#Run script like
#project.py <source_file_path> <resulting_file_path>
source = sys.argv[1]
res_file = sys.argv[2]
fileIn = open(source, 'r')
fileOut = open (res_file, 'w')
with open(str(source),encoding='utf-16') as tsvin:
tsvin = csv.reader(tsvin, delimiter=';')
fileOut = open (str(res_file), 'w')
for row in tsvin:
fileOut.write(""+"\t"+row [0]+"\t"+row[0]+"\n")
When I switched to Windows7 it became more complicated. Finally, I ended up with this.
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32
#!/usr/bin/python3
import sys
import csv
#raw_file_path = str(sys.argv[1])
#report_path = str(sys.argv[2])
with open (r'C:\Users\folder\source.csv', 'r', newline='', encoding='utf-16') as tsvin:
tsvin = csv.reader (tsvin, delimiter=';')
with open (r'C:\Users\folder\res.txt' , 'w') as fileOut:
for row in tsvin:
fileOut.write(""+"\t"+row [0]+"\t"+row[0]+"\n")
If I try to use my variables instead of the real path I have an error (1):
with open ('r + raw_file_path , 'r', newline='', encoding='utf-16') as tsvin:
OSError: [Errno 22] Invalid argument: "r'C:\\Users\\folder\\source.csv'"
Or the other error (2) if I try to use my variable in a different way:
with open (r + raw_file_path , 'r', newline='', encoding='utf-16') as tsvin:
NameError: name 'r' is not defined
I guess, the problem is due to quotes, but I have no idea how to fix it.
That's not what r is for. It's for literals only. You don't need it if you are using a variable.
with open(raw_file_path , 'r', newline='', encoding='utf-16') as tsvin:
Of course, if your variable is defined elsewhere with a literal string, you would use r at that point.
I am having trouble creating and writing to a text file in Python. I am running Python 3.5.1 and have the following code to try and create and write to a file:
from os import *
custom_path = "MyDirectory/"
if not path.exists(custom_path)
mkdir(custom_path)
text_path = custom_path + "MyTextFile.txt"
text_file = open(text_path, "w")
text_file.write("my text")
But I get a TypeError saying an integer is required (got type str) at the line text_file = open(text_path, "w").
I don't know what I'm doing wrong as my code is just about identical to that of several tutorial sites showing how to create and write to files.
Also, does the above code create the text file if it doesn't exist, and if not how do I create it?
Please don't import everything from os module:
from os import path, mkdir
custom_path = "MyDirectory/"
if not path.exists(custom_path):
mkdir(custom_path)
text_path = custom_path + "MyTextFile.txt"
text_file = open(text_path, 'w')
text_file.write("my text")
Because there also a "open" method in os module which will overwrite the native file "open" method.
I am working in a simple task of appending and adding an extra column to multiple CSV files.
The following code works perfectly in Python prompt shell:
import csv
import glob
import os
data_path = "C:/Users/mmorenozam/Documents/Python Scripts/peptidetestivory/"
outfile_path = "C:/Users/mmorenozam/Documents/Python Scripts/peptidetestivory/alldata.csv"
filewriter = csv.writer(open(outfile_path,'wb'))
file_counter = 0
for input_file in glob.glob(os.path.join(data_path,'*.csv')):
with open(input_file,'rU') as csv_file:
filereader = csv.reader(csv_file)
name,ext = os.path.splitext(input_file)
ident = name[-29:-17]
for i, row in enumerate(filereader):
row.append(ident)
filewriter.writerow(row)
file_counter += 1
However, when I run this code using Spyder, in order to have the desired .csv file, I have to add
exit()
or type in the IPython console "%reset".
Is there a better way to finish this part of the script? because the following parts of my code work with the .csv file generated in this part, and using the options above is annoying
I would like to have my tkinter program prompt the user to select the path the want to save the file which will be produced by the program.
My code looks like this. At this stage the program only saves to one file (the one I defined to test the program)
What code would I use to have 'test_write.csv' changed to any file the user chooses?
##Writing to .cvs file
with open('test_write.csv', 'w') as fp:
a = csv.writer(fp)
# write row of header names
a.writerow(n)
Thank you
Here's an example using tkFileDialog:
import Tkinter
import tkFileDialog
import csv
formats = [('Comma Separated values', '*.csv'), ]
root = Tkinter.Tk()
file_name = tkFileDialog.asksaveasfilename(parent=root, filetypes=formats, title="Save as...")
if file_name:
with open(file_name, 'w') as fp:
a = csv.writer(fp)
# write row of header names
a.writerow(n)
Use the tkFileDialog module.
Example:
import tkFileDialog
with open(tkFileDialog.asksaveasfilename(), "w") as fp:
...
Solution for python3.xxx
import tkinter
from tkinter.filedialog import asksaveasfilename
with open(asksaveasfilename(), 'w') as fp:
I have the following code where I'm trying to allow the user to open a text file and once the user has selected it, I would like the code to read it (this isn't a finished block of code, just to show what I'm after).
However, I'm having difficulties either using tkFileDialog.askopenfilename and adding 'mode='rb'' or using the code like below and using read where it produces an error.
Does anyone know how I can arrange to do this as I don't wish to have to type Tkinter.'module' for each item such as Menu and Listbox. Beginner to Tkinter and a bit confused! Thanks for the help!
import sys
from Tkinter import *
import tkFileDialog
from tkFileDialog import askopenfilename # Open dialog box
fen1 = Tk() # Create window
fen1.title("Optimisation") #
menu1 = Menu(fen1)
def open():
filename = askopenfilename(filetypes=[("Text files","*.txt")])
txt = filename.read()
print txt
filename.close()
fen1.mainloop()
Obviously the error I'm getting here is:
AttributeError: 'unicode' object has no attribute 'read'
I don't understand how to use the askopen and also be able to read the file I'm opening.
askopenfilename only returns a file name, what you wanted was askopenfile which accepts a mode parameter and opens the file for you.
The filename in your sample code is just that -- a string indicating the name of the file you wish to open. You need to pass that to the open() method to return a file handle for the name. You can then read from the file handle.
Here's some quick and dirty code to run in the Python interpreter directly. (You can run this in a script, too, but I really like REPL interfaces for quickly trying things out. You may like it as well.)
$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Tkinter
>>> from tkFileDialog import askopenfilename
>>> root = Tkinter.Tk() ; root.withdraw()
''
>>> filename = askopenfilename(parent=root)
>>> filename
'/tmp/null.c'
>>> f=open(filename)
>>> f.read()
'#include<stdio.h>\n\nint main()\n{\n for(;NULL;)\n printf("STACK");\n\n return 0;\n}\n\n'
>>> f.close()
>>>
Note especially that there's nothing Tkinter-specific in reading the file -- the dialog box just gives you a filename.
Your error is the name of your function. I simply changed def open() for def open1() and it works.
def open1():
filename = askopenfilename(parent=fen1)
print(filename)
f = open(filename)
txt = f.read()
print txt
f.close()
i think you can read your file like this
import sys
from Tkinter import *
import tkFileDialog
from tkFileDialog import askopenfilename # Open dialog box
fen1 = Tk() # Create window
fen1.title("Optimisation") #
menu1 = Menu(fen1)
def open1():
filename = askopenfilename(filetypes=[("Text files","*.txt")])
text1 = open(filename, r)
read_file = text1.read()
print(read_file)
text1.close()
fen1.mainloop()