Unexpected output from textfile - cleaning read in lines correctly

NickP

I am trying to use a very basic text file as a settings file. Three lines repeat in this order/format that govern some settings/input for my program. Text file is as follows:

Facebook
1#3#5#2
Header1#Header2#Header3#Header4
...

This is read in using the following Python code:

f = open('settings.txt', 'r')
for row in f:
    platform = f.readline()
    rows_to_keep = int(f.readline().split('#'))
    row_headers = f.readline().split('#')

    clean_output(rows_to_keep, row_headers, platform)

I would expect single string to be read in platform, an array of ints in the second and an array of strings in the third. These are then passed to the function and this is repeated numerous times.

However, the following three things are happening:

  1. Int doesn't convert and I get a TypeError
  2. First line in text file is ignored and I get rows to keep in platform
  3. \n at the end of each line

I suspect these are related and so am only posting one question.

radpotato
  1. You cannot call int on a list, you need do do some kind of list comprehension like

    rows_to_keep = [int(a) for a in f.readline().split('#')]

  2. You're reading a line, then reading another line from the file. You should either do some kind of slicing (see Python how to read N number of lines at a time) or call a function with the three lines after every third iteration.

  3. use .strip() to remove end of lines and other whitespace.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Read range of lines from Textfile

From Dev

Read Lines from textfile in assets to array

From Dev

Is it possible to read multiple lines from textfile and export it to their respective jtextfields?

From Dev

how can i read specific lines from a textfile in java 8?

From Dev

Read textfile with delimiters at all lines

From Dev

Copy range of lines from textfile

From Dev

How to read from large process output correctly

From Dev

How to read from large process output correctly

From Dev

Want to read integer from a textfile

From Dev

Is there any way to read lines from command output?

From Dev

Unexpected output from parallel programming in Python: am I doing it correctly?

From Dev

Qt assign lines from textfile to qlabels

From Dev

Read values from a textfile, delimited by = and $ signs

From Dev

Read/Write from textfile using openFileOutput

From Dev

How to read each element from a TextFile

From Dev

Using matlab to read textfile but then skip lines with a # sign at the beginning of them

From Dev

MATLAB - Read Textfile (lines with different formats) line by line

From Dev

Read lines from a file and output each line with a line number

From Dev

Read specific floating point values from lines of the command output

From Dev

Read lines from a file and output each line with a line number

From Dev

Unexpected output from function

From Dev

How can I create a list with several but not all lines from a textfile?

From Dev

Unexpected Blank lines in python output to Windows console

From Dev

Separating words from a string in input textfile and printing it to output textfile line by line in Java

From Dev

Filter Textfile for similar lines

From Dev

Why does the FileReader stream read 237, 187, 191 from a textfile?

From Dev

Reversing key-values for dictionary read from textfile - python

From Dev

Read comma separated values with stray whitespaces from a textfile in c++

From Dev

How to read the same data from excel sheet to the textfile in Python

Related Related

  1. 1

    Read range of lines from Textfile

  2. 2

    Read Lines from textfile in assets to array

  3. 3

    Is it possible to read multiple lines from textfile and export it to their respective jtextfields?

  4. 4

    how can i read specific lines from a textfile in java 8?

  5. 5

    Read textfile with delimiters at all lines

  6. 6

    Copy range of lines from textfile

  7. 7

    How to read from large process output correctly

  8. 8

    How to read from large process output correctly

  9. 9

    Want to read integer from a textfile

  10. 10

    Is there any way to read lines from command output?

  11. 11

    Unexpected output from parallel programming in Python: am I doing it correctly?

  12. 12

    Qt assign lines from textfile to qlabels

  13. 13

    Read values from a textfile, delimited by = and $ signs

  14. 14

    Read/Write from textfile using openFileOutput

  15. 15

    How to read each element from a TextFile

  16. 16

    Using matlab to read textfile but then skip lines with a # sign at the beginning of them

  17. 17

    MATLAB - Read Textfile (lines with different formats) line by line

  18. 18

    Read lines from a file and output each line with a line number

  19. 19

    Read specific floating point values from lines of the command output

  20. 20

    Read lines from a file and output each line with a line number

  21. 21

    Unexpected output from function

  22. 22

    How can I create a list with several but not all lines from a textfile?

  23. 23

    Unexpected Blank lines in python output to Windows console

  24. 24

    Separating words from a string in input textfile and printing it to output textfile line by line in Java

  25. 25

    Filter Textfile for similar lines

  26. 26

    Why does the FileReader stream read 237, 187, 191 from a textfile?

  27. 27

    Reversing key-values for dictionary read from textfile - python

  28. 28

    Read comma separated values with stray whitespaces from a textfile in c++

  29. 29

    How to read the same data from excel sheet to the textfile in Python

HotTag

Archive