1

I'm trying to print two separate indexes from two separate tuples on the same line. but when I print using f-strings it puts the second index I print on a new line.

Can't seem to figure it out no matter what I try it always prints on a new line the surname.

surnames = tuple(open("Surnames.txt", "r"))
first_names_male = tuple(open("Male_names.txt", "r"))
first_names_female = tuple(open("Female_names.txt", "r"))

print(f"{first_names_male[0]} {surnames[0]}")

Should print out "David Smith" all on one line

Instead, it shows David on the first line then on the next line has a space and then Smith

lammyalex
  • 1,179
  • 2
  • 7
  • 8
  • 2
    That is a really strange use of `open()`. Normally you would read the file contents into a list first. – Ralf Jul 04 '19 at 21:08
  • The "error" probably happens because each line from the file ends in a newline char. If you don't want that, try reading the first line and use `.strip()` on that line – Ralf Jul 04 '19 at 21:09
  • 3
    try `print(f"{first_names_male[0].strip()} {surnames[0].strip()}")` – Masoud Jul 04 '19 at 21:09
  • better to explicitly store the file handle so it can be closed (or use `with`) – Chris_Rands Jul 04 '19 at 21:10
  • Perfect, yes that works! Is there an easier way to read the file then? rather than using the tuple as i have? – lammyalex Jul 04 '19 at 21:12
  • 1
    please read cpython official doc: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files – Masoud Jul 04 '19 at 21:15

2 Answers2

1

You could use code like this to remove the new lines from the end of each line in the file and to explicitly close the files:

with open("Surnames.txt", "r") as f:
    surnames = f.read().splitlines()
with open("Male_names.txt", "r") as f:
    first_names_male = f.read().splitlines()
with open("Female_names.txt", "r") as f:
    first_names_female = f.read().splitlines()

print(f"{first_names_male[0]} {surnames[0]}")

Or something like this if you prefer:

with open("Surnames.txt", "r") as f:
    surnames = map(str.rstrip, f)
    # or
    surnames = [r.rstrip('\n') for r in f]

For more options, see https://stackoverflow.com/a/12330535/3830997

Matthias Fripp
  • 17,670
  • 5
  • 28
  • 45
  • I think there is also `.readlines()` instead of `.read()` that could be used – Ralf Jul 04 '19 at 21:28
  • Yeah, but `f.readlines()` keeps the newlines at the end of each line, so it's pretty much equivalent to `list(f)`. Either way, you have to strip off the newlines before you can work with the rows. – Matthias Fripp Jul 05 '19 at 00:05
  • Interesting distinction; I did not notice that. Good answer. – Ralf Jul 05 '19 at 10:57
0

This would be a better handling of the files, because it also closes the files and removes newlines from the end of each line.

def func(filename):
    line_list = []

    with open(filename) as f:
        for line in f:
            line = line.strip()
            if len(line) > 0:
                line_list.append(line)

    return line_list


if __name__ == '__main__':
    surnames_list = func('Surnames.txt')
    male_names_list = func('Male_names.txt')
    female_names_list = func('Female_names.txt')

    print(f"{male_names_list[0]} {female_names_list[0]}")

Does this give you the desired result?

Ralf
  • 16,086
  • 4
  • 44
  • 68