简体   繁体   中英

How do i read and print a whole .txt file using python?

I am totally new to python, and I am supposed to write a program that can read a whole .txt file and print it. The file is an article in my first language(Norwegian), and long. I have three versions that should do the same thing, but all get error. I have tried in bot PyCharm and eclipse with PyDev installed, and i get the same errors on both...

from sys import argv

import pip._vendor.distlib.compat

script, dev = argv

txt = open(dev)

print("Here's your file %r:" % dev)
print(txt.read())


print("Type the filename again:")h
file_again = pip._vendor.distlib.compat.raw_input("> ")

txt_again = open(file_again)

print(txt_again.read())

But this gets the errors:

Traceback (most recent call last):
File "/Users/vebjornbergaplass/Documents/Python eclipse/oblig1/src/1A/1A.py", line 5, in <module>
script, dev = argv
ValueError: not enough values to unpack (expected 2, got 1)

Again, i am new to python, and i searched around, but didn't find a solution.

My next attempt was this:

# -*- coding: utf-8 -*-

import sys, traceback

fr = open('dev.txt', 'r')
text = fr.read()
print(text)

But this gets these errors:

Traceback (most recent call last):
    File "/Users/vebjornbergaplass/Documents/Python eclipse/oblig1/src/1A/v2.py", line 6, in <module>
        text = fr.read()

  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
    return codecs.ascii_decode(input, self.errors)[0]

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 26: ordinal not in range(128)

I do not understand why i doesn't work.

My third attempt looks like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
parser = argparse.ArgumentParser()

parser.add_argument("dev.txt", help="dev.txt")
args = parser.parse_args()
if args.filename:
    with open('dev.txt') as f:
        for line in f:
            name, _ = line.strip().split('\t')
            print(name)

And this gets the errors:

usage: v3.py [-h] dev.txt
v3.py: error: the following arguments are required: dev.txt

Any help to why these doesnt work is welcome. Thank you in advance :D

file = open("File.txt", "r")
a = str(file.read())

print(a)

Is this what you were looking for?

For the 2nd approach is the simplest, I'll stick to it.

You stated the contents of dev.txt to be Norwegian, that means, it will include non-ascii characters like Æ,Ø,Å etc. The python interpreter is trying to tell you this:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 26: ordinal not in range(128) It cannot interpret the byte 0xC3 = 195 (decimal) as an ascii character, which is limited to a range of 128 different characters.

I'll assume you're using UTF-8 as encoding but if not, change the parameter in line 2.

# -*- coding: utf-8 -*-

fr = open('dev.txt', 'r', encoding='utf-8')
text = fr.read()
print(text)

If you do not know your encoding, you can find it out via your editor or use python to guess it .

Your terminal could also cause the error when it's not configured to print Unicode Characters or map them correctly. You might want to take a look at this question and its answers.


After operating a file, it is recommended to close it. You can either do that manually via fr.close() or make python do it automatically:

with open('dev.txt', 'r', encoding='utf-8') as fr:
    # automatically closes fr when leaving this code-block

For example:

open ("fileA.txt", "r") as fileA:
    for line in fileA:
        print(line);

This is a possible solution:

f = open("textfile.txt", "r")
lines = f.readlines()
for line in lines:
    print(line)
f.close()

Save it as for example myscript.py and execute it:

python /path/to/myscript.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM