1

I have a file that has numbers like this in it:

5

10

15

20

I know how to write code that reads the file and inputs the numbers into a LIST but how do I write code that reads the file and inputs the number in a TUPLE if a tuple doesnt support the append function? this is what I got so far:

filename=input("Please enter the filename or path")
file=open(filename, 'r')
filecontents=file.readlines()
tuple1=tuple(filecontents)
print(tuple1)

the output is this:

('5\n', '10\n', '15\n', '20\n')

it should be this:

5,10,15,20
Hemerson Tacon
  • 2,419
  • 1
  • 16
  • 28
mgale
  • 15
  • 1
  • 4
  • `map(int, filter(None, open(my_file,"rb")))` – Joran Beasley Oct 23 '18 at 00:08
  • does your file actually have the extra line breaks in it (so its only got a value on every 2nd line) or is that a consequence of how its been formatted for the question? – Paul Rooney Oct 23 '18 at 02:03
  • Possible duplicate of [Reading a file without newlines](https://stackoverflow.com/questions/12330522/reading-a-file-without-newlines) – Paul Rooney Oct 23 '18 at 02:05

4 Answers4

1

Try this:

s=','.join(map(str.rstrip,file))

Demo:

filename=input("Please enter the filename or path: ")
file=open(filename, 'r')
s=tuple(map(str.rstrip,file))
print(s)

Example output:

Please enter the filename or path: thefile.txt
(5,10,15,20)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
  • 1
    Actionly, although the user had the output like "5,10,15,20" what he asks is how to make that a tuple, which your code doesn't accomplish... – Hemerson Tacon Oct 23 '18 at 00:31
1

Using with open(..) is recommended to make sure the file is closed once you are done with it. Then use an expression to transform the returned list to a tuple.

filename=input("Please enter the filename or path")
with open(filename, 'r') as f:
    lines = f.readlines()

tup = tuple(line.rstrip('\n') for line in lines)
print(tup)
BernardL
  • 5,162
  • 7
  • 28
  • 47
0

If you already know how to make a list of ints, just cast it to a tuple like what you are doing in your attempt to solve the problem.

Here a map object can also se casted to a tuple, but it also works with list:

filename=input("Please enter the filename or path: ")
with open(filename, 'r') as file:
    filecontents=tuple(map(int, file.read().split()))

print(filecontents)

Also, if you use with statement you don't need to worry about closing the file (you was missing that part in your code too)

Hemerson Tacon
  • 2,419
  • 1
  • 16
  • 28
  • Please, elaborate. I realy don't get what is wrong with this approach – Hemerson Tacon Oct 23 '18 at 00:10
  • Okay OP's : `tuple(filecontents)` being: `list(filecontents)` does not change anything, remains same result – U13-Forward Oct 23 '18 at 00:11
  • First: you know that's not a requirement a answer here on SO have code, right? Second: with "I know how to write code that reads the file and inputs the numbers into a LIST" with numbers I had understand that it was integers and he wasn't showing that part of the code. But now I realised that I maybe had misundesrtod that part. – Hemerson Tacon Oct 23 '18 at 00:22
  • 1
    Happy now @U9-Forward? – Hemerson Tacon Oct 23 '18 at 00:29
  • 1
    Good to know and sad by that person didn't commented the reason :/ – Hemerson Tacon Oct 23 '18 at 00:38
  • 1
    Okay fixed your typo :-) – U13-Forward Oct 23 '18 at 04:40
  • https://repl.it/@JoranBeasley/QuickRosyNanotechnology ... demonstrates one problem with your solution (and by extension every other solution that casts to int in this thread) – Joran Beasley Oct 23 '18 at 17:56
  • 1
    Good point @JoranBeasley, changed my solution to use `split()` instead of `splitlines()` – Hemerson Tacon Oct 23 '18 at 18:13
  • also while using a context manager is great, and is considered best practice. when opening files for reading it does not lock the file, and pythons garbage collection is great so its not really necessary ... but its a good habit to be in I guess – Joran Beasley Oct 23 '18 at 18:33
0

If you are sure they are integers, you can do something like:

filename=input("Please enter the filename or path")
with open(filename, 'r') as f:
    lines = f.readlines()

result = tuple(int(line.strip('\n')) for line in lines)
print(resultt)

Also, if you have a list, you can always convert it to a tuple:

t = tuple([1,2,3,4])

So you can build the list appending elements, and finally convert it to a tuple

Smasho
  • 1,170
  • 8
  • 19