The following code returns a list, e.g. <class 'list'> in python. Everything I do to access that list fails
indexing list fails,
enumerating list fails
example if I just print(s)
['0.5211', '3.1324']
but if I access the indices
Traceback (most recent call last):
File "parse-epoch.py", line 11, in <module>
print("losses.add({}, {})".format(s[0], s[1]))
IndexError: list index out of range
Why can't I access the elements of the list?
import re
with open('epoch.txt', 'r') as f:
content = f.readlines()
content = [x.strip() for x in content]
for line in content:
s = re.findall("\d+\.\d+", line)
#print(s)
print("losses.add({}, {})".format(s[0], s[1]))
You should check what print(s) outputs again. Your issue is likely with a line where s does not contain a list with 2 values. If those values do not exist, then you cannot use them.
Related
I am trying to append the list to itself but it says "takes exactly one argument"
I have tried using other variables to store the splited line.
testfile = open(r'''C:\Users\shawa\Desktop\sampleg102.txt''' ,'r')
print(testfile)
word=list
for line in testfile:
line = line.lstrip()
word = word.append([1])
print(word)
and the error is
E:\vscode.source> python -u "e:\vscode.source\python\countingWords.py"
<_io.TextIOWrapper name='C:\\Users\\shawa\\Desktop\\sampleg102.txt'
mode='r' encoding='cp1252'>
Traceback (most recent call last):
File "e:\vscode.source\python\countingWords.py", line 7, in <module>
word = word.append([1])
TypeError: append() takes exactly one argument (0 given)
To explain the first mistake, take a look at this:
>>> list
<class 'list'>
>>> word = list
>>> word
<class 'list'>
>>> word = list()
>>> word
<list object at 0x...>
At the moment, you have only assigned the name list to the name word. You need to call the list constructor to actually create the list. Now, the method list.append() modifies the list in-place. It returns None and you don’t need to re-assign it.
word = list()
# word = '' works too
for line in testfile:
line = line.lstrip()
word.append([1])
print(word)
Your code will print a list of lists of 1s at the moment. If you meant to append the lines of the file, use
word.append(line)
Note that it’s preferable to use the literal [] empty list syntax rather than calling list() as it makes it very clear what is being created.
You are looking for word=list() not word=list. list is just a keyword, list() instantiates
You need to do this if you are trying to append line to list. In your code you are appending only [1] to the list word.
testfile = open(r'''C:\Users\shawa\Desktop\sampleg102.txt''' ,'r')
print(testfile)
word=list()
for line in testfile:
line = line.lstrip()
word.append(1) #or word.append(line[1]) according to use case
print(word)
Here is what I want
read large JSON file(4.8MB)
replace specific words to new words
make new file then write line to new file
Here is my code.
def replaceString(input,replace_list): #read one line, and in that line, replace string in replace_list[0] with string in replace_list[1]
new_string = input
for i in range(len(replace_list)):
new_string = new_string.replace(replace_list[i][0], replace_list[i][1])
return new_string
input_f = open("ko_ko.json",'r') #very long file
output_f = open("new_ko_ko.json",'w')
replace_list = [["`","'"],["'"],[" !","!"],[" ?","?"]] #[ ["string to replace", "string to be replaced"] , ... ]
input_line = input_f.readlines()[0]
new_lines = replaceString(input_line,replace_list)
output_f.write(new_lines)
I debugged program keep showing following error
Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/jaegu/PycharmProjects/newJSON/makeJSON.py
Traceback (most recent call last):
File "/Users/jaegu/PycharmProjects/newJSON/makeJSON.py", line 13, in <module>
new_lines = replaceString(input_line,replace_list)
File "/Users/jaegu/PycharmProjects/newJSON/makeJSON.py", line 4, in replaceString
new_string = new_string.replace(replace_list[i][0], replace_list[i][1])
IndexError: list index out of range
One of your replace_list elements is a list with just one element: ["'"]. There is no second element in that list so you get an exception. Presumably you wanted that to be ["", "'"].
Some other remarks:
Use tuples for your pairs; the pairs don't need to be mutable, using tuples lets you catch bugs earlier.
Don't use range() when you can loop directly over your pairs:
for old, new in replace_list:
new_string = new_string.replace(old, new)
I am having as issue while trying to split a line of text I get from .txt file. It is quite a big file, but I will paste only 2 lines, with original text
1307;Własność: udział 1/1<>GMINA TARNOWIEC<><> 211<>30-200 ZipCode;KS1J/00080000/2;861;Własność: udział 1/1<>GMINA TARNOWIEC<><> 211<>30-200 ZipCode;KS1J/00080990/2;
1306;Własność: udział 1/1<>Jan Nowak<>im. rodz.: Tomasz_ Maria<>Somewhere 2<>30-200 ZipCode;KW22222;861;Własność: udział 1/1<>GMINA TARNOWIEC<><>Tarnowiec 211<>30-200 ZipCode;KS1W/00080000/1;
Data I get from this file will be used to create reports, and _ and <> will be used for further formatting. I want to have the line split on ;
Problem is, I am getting error on 2 methods of splitting.
first, the basic .split(';')
dane = open('dane_protokoly.txt', 'r')
for line in dane:
a,b,c,d,e,f,g = line.split(';')
print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
print(g)
I am getting an error after printing the first loop
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\Nowy folder\costam.py", line 36, in <module>
a,b,c,d,e,f,g = line.split(';')
ValueError: not enough values to unpack (expected 7, got 1)
Same with creating lists from this file (list looks like: ['1307', 'Własność: udział 1/1<>GMINA TARNOWIEC<><> 211<>30-200 ZipCode', 'KS1J/00080000/2', '861', 'Własność: udział 1/1<>GMINA TARNOWIEC<><> 211<>30-200 ZipCode', 'KS1J/00080990/2', '']
dane = plik('dane_protokoly.txt')
for line in dane:
a = line[0]
b = line[1]
c = line[2]
d = line[3]
e = line[4]
f = line[5]
g = line[6]
print(str(a))
print(str(b))
print(str(c))
print(str(d))
print(str(e))
print(str(f))
error I get also after properly printing the first line:
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\Nowy folder\costam.py", line 22, in <module>
b = line[1]
IndexError: list index out of range
Any idea why am I getting such errors?
Sometimes line.split(';') not giving 7 values to unpack for (a,b,c,...), So better to iterate like this ,
lst = line.split(';')
for item in lst:
print item
And there is a newline in between that's making the problems for you,
And the syntax that followed is a bad practice
You change your code like this,
for line in open("'dane_protokoly.txt'").read().split('\n'):
lst = line.split(';')
for item in lst:
print item
It's doesn't care about the newlines in between,
As Rahul K P mentioned, the problems are the "empty" lines in between your lines with the data. You should skip them when trying to split your data.
Maybe use this as a starting point:
with open(r"dane_protokoly.txt", "r") as data_file:
for line in data_file:
#skip rows which only contain a newline special char
if len(line)>1:
data_row=line.strip().split(";")
print(data_row)
Your second strategy didn't work because line[0] is essentially the whole line as it includes no spaces and the default is splitting at spaces.
Therefore there is no line[1] or line[2]... and therefore you get a list index out of range error.
I hope this helps. And I hope it solves your problem.
I tried to use the below program
import os
HOME= os.getcwd()
STORE_INFO_FILE = os.path.join(HOME,'storeInfo')
def searchStr(STORE_INFO_FILE, storeId):
with open (STORE_INFO_FILE, 'r') as storeInfoFile:
for storeLine in storeInfoFile:
## print storeLine.split(r'\s+')[0]
if storeLine.split()[0] == storeId:
print storeLine
searchStr(STORE_INFO_FILE, 'Star001')
An example line in the file:
Star001 Sunnyvale 9.00 USD Los_angeles/America sunnvaleStarb#startb.com
But it gives the below error
./searchStore.py Traceback (most recent call last): File
"./searchStore.py", line 21, in
searchStr(STORE_INFO_FILE, 'Star001') File "./searchStore.py", line 17, in searchStr
if storeLine.split()[0] == storeId: IndexError: list index out of range
I have tried printing using split function on the command line and I was able to print it.
It looks like you have an empty or blank line in your file:
>>> 'abc def hij\n'.split()
['abc', 'def', 'hij']
>>> ' \n'.split() # a blank line containing white space
[]
>>> '\n'.split() # an empty line
[]
The last 2 cases show that an empty list can be returned by split(). Trying to index that list raises an exception:
>>> '\n'.split()[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
You can fix the problem by checking for empty and blank lines. Try this code:
def searchStr(store_info_file, store_id):
with open (store_info_file) as f:
for line in f:
if line.strip() and (line.split()[0] == store_id):
print line
Adding line.strip() allows you to ignore empty lines and lines containing only whitespace.
Code has an issue if split method returns an empty list.
You can change code that calls split method and add error handling code.
Following can be done
storeLineWords = storeLine.split()
if len(storeLineWords) > 0 and storeLineWords[0] == storeId:
The following is my code, its giving me Memory Error:
with open('E:\\Book\\1900.txt', 'r', encoding='utf-8') as readFile:
for line in readFile:
sepFile = readFile.read().lower()
words_1900 = re.findall('\w+', sepFile)
output:
Traceback (most recent call last):
File "C:\Python34\50CommonWords.py", line 13, in <module>
sepFile = readFile.read().lower()
MemoryError
I would say instead of reading the entire file into memory, you should read the file line by line , and then use collections.Counter() to incrementally keep track of the words and their count in the entire file. And then at the end use the Counter.most_common() method to get the 50 most common elements. Example -
import collections
import re
cnt = Counter()
with open('E:\\Book\\1900.txt', 'r', encoding='utf-8') as readFile:
for line in readFile:
cnt.update(re.findall('\w+', line.lower()))
print("50 most common are")
print([x for x,countx in cnt.most_common(50)]) # Doing this list comprehension to only take the elements, not the count.
This method may also end up with MemoryError if there are lots of distinct words in the file.
Also, Counter.most_common() returns a list of tuples, where in each tuple the first element of the tuple is the actual word , and the second element is the count of that word.