Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read from file
#1
I am trying to solve this task:

Read the dates and the energy-values from the file "kwh.dat". Every line you readin is a string with string representations of the date, 2 blanks as seperators, and
a string representation of the energy. Store this information in two different lists:
yearmonthday and kwh. For this you have to split a line directly, when you
read it into two string variables. Use for this the method split. When you save
the energy values into the list kwh convert the values into integers.

and this is the code I wrote:
file=open('kwh.dat','r')
yearmonthday=[]; kwh1=[]
for i in file:
  tempsplit=i.split("  ")
  yearmonthday.append(tempsplit[0])
  kwh1.append(int(tempsplit[1]))
but I am getting this error:

Error:
line 44, in <module> kwh1.append(int(tempsplit[1])) IndexError: list index out of range
I am not sure why and how to fix it.
Reply
#2
You'll fix it first by finding the faulty line in the data file
file=open('kwh.dat','r')
yearmonthday=[]; kwh1=[]
for lineno, i in enumerate(file, 1):
  tempsplit=i.split("  ")
  yearmonthday.append(tempsplit[0])
  try:
    kwh1.append(int(tempsplit[1]))
  except IndexError:
    print('Faulty line', lineno, 'in data file:', repr(i))
    raise
Note: it is best to indent python code with 4 space characters. This convention is almost universally adopted, so use it, it will ease your communication with the rest of the python world. Your editor can be configured to insert 4 spaces when you hit the tab key.
Reply
#3
I copy/pasted your code and is giving me the same exact error on line 7.
Reply
#4
Yes, that's because of the raise on line 10. Before the error it should have printed out the problematic line in the file. Check that line in the file.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
here is the whole error:

Error:
runfile('C:/Users/Desktop/python ode/ex.9.py', wdir='C:/Users/Desktop/python ode') Faulty line 1 in data file: '1 st line \n' Traceback (most recent call last): File "<ipython-input-168-859e3635a8fb>", line 1, in <module> runfile('C:/Users/Desktop/python ode/ex.9.py', wdir='C:/Users/Desktop/python ode') File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Desktop/python ode/ex.9.py", line 52, in <module> kwh1.append(int(tempsplit[1])) IndexError: list index out of range
and it looks like that I should focus on 'faulty line 1' message, but at this point
I really don't know how.
I am pretty new to files reading/loading on python and I don't really know how to handle them.
For instance, should I access the first line in the file? If so, how?
Reply
#6
Well, your split is on two spaces. The line uses one space between items. Change your split to work on one space.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
I corrected my original code.
Here it is:
file=open('kwh.dat','r')
yearmonthday=[]; kwh1=[]
for i in file:
    tempsplit=i.split(" ")
    yearmonthday.append(tempsplit[0])
    kwh1.append(str(tempsplit[1]))
and the outcome is totally empty when I run it. Even if I erase 'str' on the last line, I still get an empty outcome with no error.
However, if I write 'int' again, I get this error:

Error:
File "<ipython-input-183-859e3635a8fb>", line 1, in <module> runfile('C:/Users/Desktop/python ode/ex.9.py', wdir='C:/Users/Desktop/python ode') File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile execfile(filename, namespace) File "C:\Users\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "C:/Users/Desktop/python ode/ex.9.py", line 44, in <module> kwh1.append(int(tempsplit[1])) ValueError: invalid literal for int() with base 10: 'st'
I really don't know what I should be doing and the kind of result I should be getting.

print(kwh1)
and
print(yearmonthday)

give only the following outcome:

['st', 'line']
['1', '2nd']
Reply
#8
first of all your code and the traceback don't match:
in line 6 you convert to str
    kwh1.append(str(tempsplit[1]))
and the error shows conversion to int:
Error:
kwh1.append(int(tempsplit[1]))
always post the threceback alongside the actual code that produce it.


Assuming you convert to int - I would guess you have header line in the file
Post example of your input data file
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
my file is a .dat file in a notepad.
Here I am pasting the datas contained in it.

2015-11-01 327384
2015-10-01 326351
2015-09-01 325478
2015-08-01 324789
2015-07-01 324089
2015-06-01 323185
2015-05-01 322040
2015-04-01 320915
2015-03-01 319494
2015-02-01 317993
2015-01-01 316088
2014-12-01 314541
2014-11-01 313124
2014-10-01 311948
2014-09-01 310910
2014-08-01 310143
2014-07-01 309513
2014-06-01 308693
2014-05-01 307766
2014-04-01 306554
2014-03-01 304666
2014-02-01 302596
2014-01-01 300462
2013-12-01 299149
2013-11-01 297291
2013-10-01 295931
2013-09-01 294883
2013-08-01 294183
2013-07-01 293486
2013-06-01 292481
2013-05-01 291465
2013-04-01 290043
2013-03-01 288000
2013-02-01 285733
2013-01-01 283669
2012-12-01 281995
2012-11-01 280539
2012-10-01 279181
2012-09-01 278106
2012-08-01 277420
2012-07-01 276709
2012-06-01 275812
2012-05-01 274946
2012-04-01 273791
2012-03-01 272480
2012-02-01 270632
2012-01-01 268852
2011-12-01 267529
2011-11-01 265962
2011-10-01 264591
2011-09-01 263585
2011-08-01 262909
2011-07-01 262284
2011-06-01 261564
2011-05-01 260779
2011-04-01 259797
2011-03-01 257943
2011-02-01 255982
2011-01-01 254047
2010-12-01 251734
2010-11-01 249983
2010-10-01 248595
2010-09-01 247543
2010-08-01 246682
2010-07-01 246123
2010-06-01 245281
2010-05-01 244132
2010-04-01 242742
2010-03-01 240984
2010-02-01 239078
2010-01-01 236527
2009-12-01 234875
2009-11-01 233319
2009-10-01 231711
2009-09-01 230819
2009-08-01 229995
2009-07-01 229068
2009-06-01 228029
2009-05-01 226826
2009-04-01 225388
2009-03-01 223161
2009-02-01 220664
2009-01-01 217466
2008-12-01 215185
2008-11-01 213085
2008-10-01 211498
2008-09-01 210427
2008-08-01 209265
2008-07-01 208425
2008-06-01 207795
2008-05-01 206804
2008-04-01 205246
2008-03-01 202938
2008-02-01 200487
2008-01-01 197974
2007-12-01 195715
2007-11-01 193757
2007-10-01 192050
2007-09-01 190742
2007-08-01 189627
2007-07-01 188716
2007-06-01 187489
2007-05-01 186286
2007-04-01 184874
2007-03-01 182776
2007-02-01 179764
2007-01-01 177118
2006-12-01 175391
2006-11-01 173512
2006-10-01 172266
2006-09-01 171258
2006-08-01 170184
2006-07-01 169293
2006-06-01 168326
2006-05-01 166869
2006-04-01 164671
2006-03-01 161308
2006-02-01 158474
2006-01-01 155077
2005-12-01 152338
2005-11-01 150047
2005-10-01 148630
2005-09-01 147341
2005-08-01 146094
2005-07-01 144875
2005-06-01 143541
2005-05-01 142050
2005-04-01 140199
2005-03-01 137092
2005-02-01 134149
2005-01-01 131693
2004-12-01 129564
2004-11-01 127262
2004-10-01 125690
2004-09-01 124516
2004-08-01 123379
2004-07-01 122607
2004-06-01 121384
2004-05-01 120133
2004-04-01 118587
2004-03-01 116280
2004-02-01 113872
2004-01-01 110880
2003-12-01 109017
2003-11-01 107297
2003-10-01 104832
2003-09-01 103789
2003-08-01 103042
2003-07-01 102193
2003-06-01 101383
2003-05-01 100326
2003-04-01 98161
2003-03-01 95420
2003-02-01 92100
2003-01-01 88736
Reply
#10
well, do you see anything like your outcome?
Output:
['st', 'line'] ['1', '2nd']
clearly you read something different. Something like
Output:
1 st 2nd line
with the above data your code WILL work
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can we store value in file if we open file in read mode? prasanthbab1234 3 2,575 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  [split] how to read a specific row in CSV file ? laxmipython 2 8,882 May-22-2020, 12:19 PM
Last Post: Larz60+
  Read data from a CSV file in S3 bucket and store it in a dictionary in python Rupini 3 6,999 May-15-2020, 04:57 PM
Last Post: snippsat
  Read directly from excel file using python script dvldgs05 0 2,257 Oct-19-2018, 02:51 AM
Last Post: dvldgs05
  Read a data from text file (Notepad) Leonzxd 24 13,913 May-23-2018, 12:17 AM
Last Post: wavic
  Homework - Read from/Write to file (renamed from Help help help) Amitkafle 1 3,049 Jan-11-2018, 07:24 AM
Last Post: wavic
  read a binary file to find its type atux_null 7 17,307 Nov-24-2017, 10:18 AM
Last Post: DeaD_EyE
  Cannot read from text file aljonesy 5 3,611 Oct-05-2017, 05:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020