why this error cant be handled? i already tried all of solution in stackoverflow about this problem using field_size_limit, but not worked. here is my code
import sys
import csv
csv.field_size_limit()
with open('data_latih_jokowi_negatif.csv', 'r', encoding="latin-1") as file:
readCSV = list(csv.reader(file))
#del(readCSV[1664], readCSV[1663])
progress = 0
result = readCSV
and this is the error
Traceback (most recent call last):
File "cleandoublesentences.py", line 7, in <module>
readCSV = list(csv.reader(file))
_csv.Error: field larger than field limit (131072)
You will need to call field_size_limit() with the new limit.
csv.field_size_limit(10000000) # 10 megabytes, give or take
Related
I'm getting a file not found error in JupyterLab. I can see the file and I have tried several different ways to open it via JupyterLab, but I keep getting an error as if the file does not exist.
Below is my code:
import os
import csv
csvpath = os.path.join('..','Resources','budget_data.csv')
with open(csvpath, newline="") as csvfile:
`enter code here`csvreader = csv.reader(csvfile, delimiter=",")
Here is the error:
FileNotFoundError Traceback (most recent call last)
<ipython-input-17-3c32e2cc9d43> in <module>
----> 1 with open(csvpath, newline="") as csvfile:
2 csvreader = csv.reader(csvfile, delimiter=",")
FileNotFoundError: [Errno 2] No such file or
directory: '..,Resources,budget_data.csv'
Here is the path: GTATL201902DATA3/03-Python/Homework/Instructions/PyBank/Resources/budget_data.csv
csvpath = os.path.join('..','Resources','budget_data.csv')
Are you absolutely sure this is the actual code you're using? Because going by the error message, it looks like you did this instead:
csvpath = os.path.join('..,Resources,budget_data.csv')
i.e. the .. and the commas are literally part of the string, instead of joining three separate strings.
I try to import my CA-10-60 file with this code:
import csv
with open('CA-10-60.csv', newline='') as f:
reader = csv.DictReader(f, delimiter='')
for row in reader:
print(row['Contract'], row['Serial'])
But I get this Error:
Traceback (most recent call last):
File "C:/Users/id984876/PycharmProjects/Search Engine SMC/flask/play.py", line 3, in <module>
reader = csv.DictReader(f, delimiter='')
AttributeError: module 'csv' has no attribute 'DictReader'
One issue you will definitely have is your delimiter needs to be a one character string as evidenced by what happens when I try and replicate your error -
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\caleb\AppData\Local\Programs\Python\Python36\lib\csv.py", line 87, in __init__
self.reader = reader(f, dialect, *args, **kwds)
TypeError: "delimiter" must be a 1-character string
Try fixing that and see if anything changes. As far as I can tell, the csv module is present in both 2.7.14 and 3.6.3 so unless you are running an older version I can't imagine any issues with the import.
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 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.
I'm using the latest python release and after searching, I can't seem to find anything on pickles that will work for me.
I am simply going through tutorials attempting to learn about pickling and none of the source code that apparently works on the tutorials will work for me, I suspect this is something to do with the tutorials being outdated.
What I have tried and is the same as what tutorials show is:
import pickle
lists = [1,2,3,4,5]
pickle.dump(lists, open('log.txt', 'a+'))
which gives me the following error:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
pickle.dump(lists, open('log.txt', 'a+'))
TypeError: must be str, not bytes
this
>>> import pickle
>>> unpicklefile = open('log.txt', 'r')
>>> unpickledlist = [1,2,3,4,5]
>>> unpickledlist = pickle.load(unpicklefile)
gives me the following error:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
unpickledlist = pickle.load(unpicklefile)
TypeError: 'str' does not support the buffer interface
Thank you for any replies and help
The 'a+' mode may be causing you problems. And, if you're on Windows, it would be useful to open a file in a binary mode. Also, you should close the file before reopening to read it back in. And make sure you're writing and reading the same file ('log.txt' vs. 'filename'):
import pickle
lists = [1,2,3,4,5]
f = open('tmp_pickle.pic', 'wb')
pickle.dump(lists, f)
f.close()
f = open('tmp_pickle.pic', 'rb')
unpickledlist = pickle.load(f)
print unpickledlist