I have a python script that exports a file using the following command in a function. It's works, but I need to import that file after exporting and loop through it.
connector.save_csv(path,'_'+"GT_Weekly"+'_'+keys)
Thereore, I've been hard coding the file name and using it with open(). However, I was wondering how I could specify the file name in the same way as specified when I saved it.
Here's the hard coded approach:
with open(path,'_'+"GT_Weekly"+'_'+keys+'.csv', 'rt') as csvfile:
csvReader = csv.reader(csvfile)
data = []
I want to take the save_csv arguments and add it to open but that doesn't work. How can I do this
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
Both keys and path were specified as
keys ="football"
path = "/home/abraham/Trends"
What component needs to be changed to an integer? It's not evident to me
Furthermore, when I add int,I get the following error
with int(open(path,'_'+"GT_Weekly"+'_'+keys+'.csv', 'rt')) as csvfile:
csvReader = csv.reader(csvfile)
data = []
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: an integer is required
>>>
You seem to think open accepts a path, a file name, and a mode; but it doesn't. The parameters are a file name, a mode, and a buffer size. The buffer size should be an integer, but you are passing 'rt'; hence, you get an error message.
I guess you want open(os.path.join(path, filename), 'rt') instead, or possibly open(path + filename, 'rt'), if the last component of path is a prefix part of the filename you want, not a directory name.
Related
I have some code which i coded in python 2.7, however I need it to work for 3.6 and when i run it i get this error and i am not sure why.
import csv
def ReadFromFile():
with open('File.csv', 'r') as File:
cr = csv.reader(File)
for row in cr:
Name = row[0]
Gender = row[1]
print(Name + Gender)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
ReadFromFile()
File "F:/Test.py", line 6, in ReadFromFile
Name = row[0]
IndexError: list index out of range
I am using the same code saved on a memory stick with the file in 2.7 i get my desired out come of it being read but in 3.6 i am stuck with the error. Thanks for any help
Edit: Added Print
After adding print i got
ELIZABETHFemale
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
ReadFromFile()
File "F:/Test.py", line 6, in ReadFromFile
Name = row[0]
IndexError: list index out of range
So it gave me the first line but nothing more
Python's CSV module has changed how it wants the files you pass to it to be opened. You want to avoid the file object doing any newline transformation because some CSV formats allow embedded newlines within quoted fields. The csv module will do its own newline normalization, so the usual universal newline handling the file object does is redundant.
This is mentioned in the csv.reader documentation, where it is talking about the file argument:
If csvfile is a file object, it should be opened with newline=''.
So for your code, try changing open('File.csv', 'r') to open('File.csv', 'r', newline='').
Have you tried pandas?
I think you may want to use something like
import pandas as pd
def ReadFromFile():
df = pd.read_csv('File.csv')
for row in df:
Name = row[0]
Gender = row[1]
print(Name + Gender)
I am trying to get the code below to read the file raw.txt, split it by lines and save every individual line as a .txt file. I then want to append every text file to splits.zip, and delete them after appending so that the only thing remaining when the process is done is the splits.zip, which can then be moved elsewhere to be unzipped. With the current code, I get the following error:
Traceback (most recent call last): File "/Users/Simon/PycharmProjects/text-tools/file-splitter-txt.py",
line 13, in <module> at stonehenge summoning the all father. z.write(new_file)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/zipfile.py", line 1123, in write st = os.stat(filename) TypeError: coercing to Unicode: need string or buffer,
file found
My code:
import zipfile
import os
z = zipfile.ZipFile("splits.zip", "w")
count = 0
with open('raw.txt','r') as infile:
for line in infile:
print line
count +=1
with open(str(count) + '.txt','w') as new_file:
new_file.write(str(line))
z.write(new_file)
os.remove(new_file)
You could simply use writestr to write a string directly into the zipFile. For example:
zf.writestr(str(count) + '.txt', str(line), compress_type=...)
Use the file name like below. write method expects the filename and remove expects path. But you have given the file (file_name)
z.write(str(count) + '.txt')
os.remove(str(count) + '.txt')
So, i've been writing this program that takes a HTMl file, replaces some text and puts the return back into a different file in a different directory.
This error happened.
Traceback (most recent call last):
File "/Users/Glenn/jack/HTML_Task/src/HTML Rewriter.py", line 19, in <module>
with open (os.path.join("/Users/Glenn/jack/HTML_Task/src", out_file)):
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/posixpath.py", line 89, in join
genericpath._check_arg_types('join', a, *p)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/genericpath.py", line 143, in _check_arg_types
(funcname, s.__class__.__name__)) from None
TypeError: join() argument must be str or bytes, not 'TextIOWrapper'
Below is my code. Has anyone got any solutions I could implement, or should I kill it with fire.
import re
import os
os.mkdir ("dest")
file = open("2016-06-06_UK_BackToSchool.html").read()
text_filtered = re.sub(r'http://', '/', file)
print (text_filtered)
with open ("2016-06-06_UK_BackToSchool.html", "wt") as out_file:
print ("testtesttest")
with open (os.path.join("/Users/Glenn/jack/HTML_Task/src", out_file)):
out_file.write(text_filtered)
os.rename("/Users/Glenn/jack/HTML_Task/src/2016-06-06_UK_BackToSchool.html", "/Users/Glenn/jack/HTML_Task/src/dest/2016-06-06_UK_BackToSchool.html")
with open (os.path.join("/Users/Glenn/jack/HTML_Task/src", out_file)):
Here out_file if TextIOWrapper, not string.
os.path.join takes string as arguments.
Do not use keywords name as variable. file is keyword.
Do not use space in between function call os.mkdir ("dest")
try to change this:
with open ("2016-06-06_UK_BackToSchool.html", "wt") as out_file
on this:
with open ("2016-06-06_UK_BackToSchool.html", "w") as out_file:
or this:
with open ("2016-06-06_UK_BackToSchool.html", "wb") as out_file:
Bascially I am reading in a file called Sounding, which has its name as '12142014_2345.csv' and I want to save it as
'12142014_2345_Averaged.csv'
Below is the code I have leading up to it.
basename = os.path.basename(Sounding)
basename,ext = os.path.split(basename)
with open(os.path.join(basename+'_Averaged'+ext)) as f:
w = csv.DictWriter(f, rows_1[0].keys())
w.writeheader()
And this is my error.
Traceback (most recent call last):
File "C:\Users\Bud\Desktop\OWLES RECENT\Moving Average.py", line 151, in <module>
with open(os.path.join(basename+'_Averaged'+ext)) as f:
IOError: [Errno 2] No such file or directory: u'_AveragedUIllinois_20131207Singular.csv'
I'm not exactly sure what I am doing wrong with it.
You want to use os.path.splitext (to split the extension) instead of split (that splits last path element).
And don't forget to open the file in write mode (and check your indentation):
basename,ext = os.path.splitext(basename)
with open(os.path.join(basename+'_Averaged'+ext), 'w') as f:
w = csv.DictWriter(f, rows_1[0].keys())
w.writeheader()
Looking at the file name in the trace, you can see '_Averaged' coming before the full file name. os.path.split looks for the directory divider (usually slash). I think you want os.path.splitext, which splits the string into path and extension. Since you already have just the basename, the path will be the filename without the extension.
Whenever I try to overwrite a file in Python 2.7 this is what happens, code:
a = open('hello.txt')
a.write('my name is mark')
And my error is:
Traceback (most recent call last):
File "C:\Users\Mark Malkin\Desktop\New folder\opener.py", line 2, in <module>
a.write('my name is mark')
IOError: File not open for writing
From docs on open:
If mode is omitted, it defaults to 'r'.
To write instead use,
a = open('hello.txt', 'w')
Or better yet,
with open('hello.txt', 'w') as f:
f.write('my name is mark')