Python Forum
read from txt. file in certain manner
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
read from txt. file in certain manner
#1
Greetings,

I just found this website that summarizes how to read from a .txt file:
https://www.guru99.com/reading-and-writi...ython.html

It only gives the options of reading the entire content or reading line by line.

Now I have to plot (x,y) data that is stored in a .txt file in two columns (one column for the x-, one columns for the y-values). The columns are merely separated by a few spaces. I want to store the x values in one array/list and the y values in another.

I am a complete Python beginner and I intend to work with Matplotlib later on for the actual plotting. However, to plot something I probably have to read in the data first, so here we are.

Regards!
Reply
#2
Either that site is quite old, or the author is not a GURU.
I would like to suggest HDF5 as a better solution: http://www.h5py.org/ and docs: http://docs.h5py.org/en/stable/
Reply
#3
Example:
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import csv

x = []
y = []
with open('x_y.txt') as csv_file:
    plots = csv.reader(csv_file, delimiter=',')
    for row in plots:
        x.append(int(row[0]))
        y.append(int(row[1]))

plt.plot(x,y, label='Loaded from file!')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Graph x y')
plt.legend(loc='upper left', shadow=True, fontsize='large')
plt.show()
x_y.txt:
Output:
1,5 2,3 3,4 4,7 5,4
[Image: NRrzlw.jpg]

In future also look into Pandas and Jupyter Notebook.
This can make it easier to load and work with data/plot.
[Image: DoZjfX.jpg]
Reply
#4
Wow! Super awesome example!
Reply
#5
(Aug-06-2018, 07:59 PM)snippsat Wrote: Example:
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import csv

x = []
y = []
with open('x_y.txt') as csv_file:
    plots = csv.reader(csv_file, delimiter=',')
    for row in plots:
        x.append(int(row[0]))
        y.append(int(row[1]))

plt.plot(x,y, label='Loaded from file!')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Graph x y')
plt.legend(loc='upper left', shadow=True, fontsize='large')
plt.show()
x_y.txt:
Output:
1,5 2,3 3,4 4,7 5,4
[Image: NRrzlw.jpg]


Hello Snippsat,

thanks for the great example!
Can you elaborate this part of the code?
with open('x_y.txt') as csv_file:
    plots = csv.reader(csv_file, delimiter=',')
    for row in plots:
        x.append(int(row[0]))
        y.append(int(row[1]))
The first line seems to tell the computer to read the file as a csv_file. And I guess it is line by line? The lines are saved in the list(?) plots. The "delimiter=',' " means you are overwriting my spaces between the columns with a ','? Why exaclty?

"for row in plots" just seems to go through the rows and to save the x,y values in the lists.
I suppose I could also save additional columns like this, right?


edit: I am getting an error: "ValueError: invalid literal for int() with base 10: '0 144'"
"0 144" is the first line in the file. Most of the numbers are doubles, mb it is a problem that these first ones can be interpreted as ints?

Regards
Reply
#6
in snippsat file x_y.txt he has comma as separator. In your case you will use space, e.g. delimiter=" "

This whole line plots = csv.reader(csv_file, delimiter=' ') creates a csv.reader object which will be used to iterate/read lines from the 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
#7
(Aug-07-2018, 10:36 AM)buran Wrote: in snippsat file x_y.txt he has comma as separator. In your case you will use space, e.g. delimiter=" "

This whole line plots = csv.reader(csv_file, delimiter=' ') creates a csv.reader object which will be used to iterate/read lines from the file

Ok, I changed it. Now I get the error:

File "C:path.../.py", line 11, in <module>
y.append(int(row[1]))

ValueError: invalid literal for int() with base 10: ' '

the first rows in my file are these:
0 144
0.01 144
0.02 143.88
0.03 143.78

It's a .dat file that can be opened with the text editor. Ofc I changed the filename in the with open - line.
Reply
#8
You need to use float, because you have float numbers, not int like in snippsat example
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
Ah, I tried double but I just realized there is just float here in Python.

Now, I get:
y.append(float(row[1]))

ValueError: could not convert string to float:

Sth wrong with the spaces?
Reply
#10
(Aug-07-2018, 12:54 PM)SchroedingersLion Wrote: Sth wrong with the spaces?
Check that you don't have extra spaces.
use print(repr(row[0])) and print(repr(row[1])) to see what you actually work with.
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
  Pandas read csv file in 'date/time' chunks MorganSamage 4 1,709 Feb-13-2023, 11:24 AM
Last Post: MorganSamage
  Can't read text file with pandas zinho 6 12,137 May-24-2020, 06:13 AM
Last Post: azajali43
  Read file Into array with just $0d as Newline lastyle 5 3,383 Feb-03-2020, 11:58 PM
Last Post: lastyle
  Read csv file from Yahoo Finance ian 3 4,663 Sep-22-2019, 06:47 AM
Last Post: ndc85430
  read complex file with both pandas and not Diedro 1 2,897 Jan-29-2019, 05:26 PM
Last Post: Larz60+
  Need help in framing data read from wav file Vishweshkumar 1 3,676 Feb-10-2017, 01:45 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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