Python Forum
Read txt files in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read txt files in python
#1
Hi, my teacher just gave me this assignment and i already wrote smt, but it doesn't work... Can anyone help me,please?
---------------------------------------------------------------------------------------------------------------
Forum posts are collected in some files that have the following format.
A file contains one or more posts, the beginning of a post is marked by a line that contains it
sequentially the two substrates "<POST>" and "N" (without quotation marks)
interspersed, preceded, and / or followed by 0.1 or more spaces.
"N" is the post ID (a positive number).
The content of the post is in the next lines up to the line that marks the next post
or the end of the file.
It is assured that the string "<POST>" is not contained in any posts.

In the following, we mean, as usual, any sequence of characters
alphabetically of maximum length. The alphabet characters are the ones for which
True is the isalpha () method.

Write a post function (fposts, set) that takes the input:
- the path of a file (fposts)
- and a set of words (together)
and returns a set (result).

The returned set (result) should contain the post identifiers (IDs)
which contain at least one word of the input element.
Two words are considered equal even if some alphabetic characters appear in one
uppercase and the other in lower case.
---------------------------------------------------------------------------------------------------------------
def post(fposts,set):
fposts=open('file01.txt')
for p in set:
if p.isalpha()==True:
fposts.readline
if p not in fposts:
print("no world in set found in post")
else:
print(fposts(n))
return(p)

---It's the first time for me doing programmation so i don't really know what i am doing and i'm freaking desperate.---
Reply
#2
Your indentation was lost. Please repost using code tags, see BBCODE
to preserve indentation, post code within code tags using shift-ctrl-v
Reply
#3
how do i delete this post?
Reply
#4
Hi, my teacher just gave me this assignment and i already wrote smt, but it doesn't work... Can anyone help me,please?
---------------------------------------------------------------------------------------------------------------
Forum posts are collected in some files that have the following format.
A file contains one or more posts, the beginning of a post is marked by a line that contains it
sequentially the two substrates "<POST>" and "N" (without quotation marks)
interspersed, preceded, and / or followed by 0.1 or more spaces.
"N" is the post ID (a positive number).
The content of the post is in the next lines up to the line that marks the next post
or the end of the file.
It is assured that the string "<POST>" is not contained in any posts.

In the following, we mean, as usual, any sequence of characters
alphabetically of maximum length. The alphabet characters are the ones for which
True is the isalpha () method.

Write a post function (fposts, set) that takes the input:
- the path of a file (fposts)
- and a set of words (together)
and returns a set (result).

The returned set (result) should contain the post identifiers (IDs)
which contain at least one word of the input element.
Two words are considered equal even if some alphabetic characters appear in one
uppercase and the other in lower case.
---------------------------------------------------------------------------------------------------------------
Hi, my teacher just gave me this assignment and i already wrote smt, but it doesn't work... Can anyone help me,please?
---------------------------------------------------------------------------------------------------------------
Forum posts are collected in some files that have the following format.
A file contains one or more posts, the beginning of a post is marked by a line that contains it
sequentially the two substrates "<POST>" and "N" (without quotation marks)
interspersed, preceded, and / or followed by 0.1 or more spaces.
"N" is the post ID (a positive number).
The content of the post is in the next lines up to the line that marks the next post
or the end of the file.
It is assured that the string "<POST>" is not contained in any posts.

In the following, we mean, as usual, any sequence of characters
alphabetically of maximum length. The alphabet characters are the ones for which
True is the isalpha () method.

Write a post function (fposts, set) that takes the input:
- the path of a file (fposts)
- and a set of words (together)
and returns a set (result).

The returned set (result) should contain the post identifiers (IDs)
which contain at least one word of the input element.
Two words are considered equal even if some alphabetic characters appear in one
uppercase and the other in lower case.
---------------------------------------------------------------------------------------------------------------
def post(fposts,set):
fposts=open('file01.txt')
for p in set:
if p.isalpha()==True:
fposts.readline
if p not in fposts:
print("no world in set found in post")
else:
print(fposts(n))
return(p)

---It's the first time for me doing programmation so i don't really know what i am doing and i'm freaking desperate.---

---It's the first time for me doing programmation so i don't really know what i am doing and i'm freaking desperate.---
Reply
#5
You don't delete posts, just post a new one in same thread.
Or if you seee an edit button, you can edit content.

Your new post didn't have code tags either.
Sorry, but they are needed to preserve indentation
(also post code using shift-ctrl-v)
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020