Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read from file
#11
just to make sure.
This code
file=open('kwh.dat','r')
yearmonthday=[]; kwh1=[]
for i in file:
    tempsplit=i.split(" ")
    yearmonthday.append(tempsplit[0])


    kwh1.append(str(tempsplit[1])) # or kwh1.append((tempsplit[1]))
print(kwh1)
print(yearmonthday)
produces this output
['st', 'line']
['1', '2nd']

and if I change the 6th line with
kwh1.append(int(tempsplit[1]))
I get the error mentioned above.

So are you saying that with this current code I should be obtaining the right outcome?
What do you think is wrong?

one more thing I think is worth mentioning:
I am trying to import this file by clicking on the arrow
on the top middle of the spyder compiler
and once I write the file name I want to import,
I get this message:

unsupported file extension '.dat'
Would you like to import it anyway?
(by selecting a known file format)


and it still does not work no matter what I select.
Reply
#12
I am saying that whatevewer file you read, it looks like this
Output:
1 st line 2nd line
ring any bells?
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
#13
first of all,
what exactly should I write in my compiler to get the output you just listed?
Secondly,
What should I write to obtain on the console all those numbers that I pasted in my
previous post contained in the file?
Moreover, what can you tell me about my issue with importing the file
I mentioned above?
Is it relevant for me to accomplish my task? If so, what should I do ?
Reply
#14
(May-14-2019, 09:30 AM)mcgrim Wrote: what exactly should I write in my compiler to get the output you just listed?
What I listed is my guess based on the output provided by you:
(May-14-2019, 08:06 AM)mcgrim Wrote: print(kwh1)
and
print(yearmonthday)

give only the following outcome:

['st', 'line']
['1', '2nd']

to print the whole file, run:
with open('kwh.dat','r') as my_file:
    for line in my_file:
        print(line)
this will print file content line by line

(May-14-2019, 09:30 AM)mcgrim Wrote: What should I write to obtain on the console all those numbers that I pasted in my
previous post contained in the file?

as I said your code should work, given that you read the correct file

(May-14-2019, 09:30 AM)mcgrim Wrote: Moreover, what can you tell me about my issue with importing the file
I mentioned above?
Is it relevant for me to accomplish my task? If so, what should I do ?

I don't use Spider, but I don't see why I would need to import anything
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
#15
I used the code you wrote
with open('kwh.dat','r') as my_file:
    for line in my_file:
        print(line)
and now I am getting
Output:
1 st line 2nd line
but what I am really trying to do, is to print two lists
and I want to be able to see the results in my console.
the list kwh1 should print
Output:
327384 326351 325478 324789 324089 323185 322040 320915 319494 . . . . etc
and yearmonthday
should print

Output:
2015-11-01 2015-10-01 2015-09-01 2015-08-01 2015-07-01 2015-06-01 2015-05-01 2015-04-01 . . . etc
Now, how come I am not able to print these values?
Where is the problem with my code?
If you say that my code is correct, how come I am not able
to see these two lists?
Reply
#16
I cannot explain it more clear - the file that you read has the content I guessed and you just confirmed it.
You need to read from the correct file to get the output you want. Obviously you have more than one file with same name in different locations and the file that you read is not the one you think you read.
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
#17
Thank you.
Now is clear.
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,561 Sep-26-2020, 12:10 PM
Last Post: ibreeden
  [split] how to read a specific row in CSV file ? laxmipython 2 8,868 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,973 May-15-2020, 04:57 PM
Last Post: snippsat
  Read directly from excel file using python script dvldgs05 0 2,252 Oct-19-2018, 02:51 AM
Last Post: dvldgs05
  Read a data from text file (Notepad) Leonzxd 24 13,863 May-23-2018, 12:17 AM
Last Post: wavic
  Homework - Read from/Write to file (renamed from Help help help) Amitkafle 1 3,037 Jan-11-2018, 07:24 AM
Last Post: wavic
  read a binary file to find its type atux_null 7 17,285 Nov-24-2017, 10:18 AM
Last Post: DeaD_EyE
  Cannot read from text file aljonesy 5 3,591 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