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
#11
(Aug-07-2018, 01:25 PM)buran Wrote:
(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.

I have two spaces between the columns. But adding a second space in your code does not seem to work.

Those two commands give (only copied last output lines):

'99.95'
''
'99.96'
''
'99.97'
''
'99.98'
''
'99.99'
''
'100'
''

Where the numbers are the last numbers of my first column. Seems like the second space is the problem then. Can I still make it work? The file is too large for manual removal of the second space character.
Reply
#12
that means you have more than one space between the two numbers
use row[0] and row[-1] instead. That is assuming you don't have empty spaces after the second number
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
Delimiter is default to one character doc.
So for 2 character as you have posted in #7 have to change code so it become one character for delimiter.
Have to use edit post to see two character because you have posted data without code tag.
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import csv
 
x = []
y = []
with open('x_float.txt') as csv_file:    
    plots = csv.reader((line.replace('  ', ' ') for line in csv_file), delimiter=' ')
    for row in plots:
        x.append(float(row[0]))
        y.append(float(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()
[Image: 00342B.jpg]
Reply
#14
@buran, Snippsat

Thank you very much guys, both of these ways work! :)
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,133 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,674 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