+ 1

Reading Through: 35.2 Challenge Python For Data Science

Problem: You need to make a program to read the given number of characters of a file. Take a number N as input and output the first N characters of the books.txt file. My attempt is below. I’m lost about how to get the first element from the list generated. I end up with a list of strings instead of a string of characters.

10th Apr 2021, 10:47 PM
Fan Mason
16 Réponses
+ 5
file = open("/usercode/files/books.txt") n = int(input()) g=file.read()[:n] file.close() print(g) I think i had the join in there unnecessarily
28th Jul 2021, 8:58 PM
Fan Mason
+ 3
If you just want the first n characters of the file (not n characters per line of the file), then you can just use; file.read()[:n] This will include any invisible or whitespace characters such as '\n'
10th Apr 2021, 11:16 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
CarrieForle FYI, file.read(n) will return n bytes of the file. Which in this particular case is ok (file uses default unicode type UTF-8), but could result in an issue if the unicode type in the file was larger. Also, just to be forthcoming, I'm not sure that the way I wrote it would give a different result if the unicode type was a larger type. At least not without doing some research and testing.
11th Apr 2021, 2:52 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
N = int(input()) print(file.read(N))
3rd Jan 2022, 4:15 PM
Nazarii Zavada
Nazarii Zavada - avatar
+ 1
file = open("/usercode/files/books.txt") n = int(input()) for i in file.readlines(): print(''.join(i[:n])) file.close()
10th Apr 2021, 10:48 PM
Fan Mason
+ 1
You iterated file.readlines(). You will get the list of strings. So don't use for loop, and use read instead. file.read(n)
11th Apr 2021, 2:30 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
Thanks all
11th Apr 2021, 2:31 AM
Fan Mason
0
Fan Mason what was your final code? Whats above doesnt seen to work. Thanks!
28th Jul 2021, 8:55 PM
Zach Z
0
Thanks Fan Mason !!
28th Jul 2021, 9:07 PM
Zach Z
0
file = open("/usercode/files/books.txt") #your code goes here n = int(input()) print(file.read()[:n]) file.close() I think this will work.)
28th Aug 2021, 4:26 PM
Aziz Ergashev
Aziz Ergashev - avatar
0
My Solution: file = open("/usercode/files/books.txt") #your code goes here n = int(input()) g=file.read()[:n] file.close() print(g)
10th Nov 2022, 8:39 AM
Pooja Patel
Pooja Patel - avatar
0
N = int(input("Enter the number of characters to read: ")) with open("books.txt", "r") as file: content = file.read(N) print(content)
25th Apr 2023, 7:32 AM
Ali Akram Marufi
Ali Akram Marufi - avatar
0
file = open("filename.txt",'r') char=int(input()) a=file.read(char) print(a) file.close()
15th Jul 2023, 6:54 AM
Mrcool
Mrcool - avatar
0
My Solution: file = open("/usercode/files/books.txt") #your code goes here N = int(input()) N = file.read(N) print(N) file.close()
5th Sep 2023, 9:32 AM
Mai Phương
Mai Phương - avatar
0
This is my code file = open("/usercode/files/books.txt") #my code N=int(input()) print(file.read(N)) file.close()
2nd Dec 2023, 9:26 AM
vahid kamrani
vahid kamrani - avatar
0
Use read(N). It returns a single string with (up to) the first N characters, not a list # input: how many characters to read N = int(input().strip()) with open('books.txt', 'r', encoding='utf-8') as f: chunk = f.read(N) # <- string, not list print(chunk) Why your code likely made a list If you used readlines() (or iterated line-by-line), you got a list of lines. To salvage that approach: N = int(input().strip()) with open('books.txt', 'r', encoding='utf-8') as f: text = ''.join(f.readlines()) # merge list -> single string print(text[:N]) But the first version (f.read(N)) is the simplest and most efficient for this task.
24th Jul 2025, 2:06 PM
Mayank kumar Verma
Mayank kumar Verma - avatar