Python Forum
files creation question, any help plz ?
Thread Rating:
  • 2 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
files creation question, any help plz ?
#1
I have a .txt file that contain tweets and its ID for example
1  what a nice day //1 is the ID and "what a nice day" is the tweet text 
2  how are you doing //2 is the ID and "how are you doing" is the tweet text 
.
.
. etc

I want to create a new file for each line and name it with the ID and inside this file I want to put the tweet text take the first line as an example 

"1  what a nice day"

 the file name should be(

1.txt

)and inside that file 

"what a nice day"

I really dont have any idea how to do this can you please help me?
Reply
#2
Hi Qubayel, welcome to the forums! Did you have a try which we can comment on? It seems like a simple task which can be solved with standard python commands. Or are you also just learning python and have never programmed anything before? Then, I'd recommend starting a basic tutorial, for example the official python documentation has a good one.
Reply
#3
(Apr-12-2017, 12:41 PM)Kebap Wrote: Hi Qubayel, welcome to the forums! Did you have a try which we can comment on? It seems like a simple task which can be solved with standard python commands. Or are you also just learning python and have never programmed anything before? Then, I'd recommend starting a basic tutorial, for example the official python documentation has a good one.

Actually im new to python 
i dont have any idea on how to do this 
if can help that will be great 
thanks
Reply
#4
Didn't get it.
A row in the input file contains 1 "what a nice day"?
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Apr-12-2017, 12:44 PM)Qubayel Wrote: i dont have any idea on how to do this 
if can help that will be great

If a programmer has a problem which seems to big to solve, they will split the big problem into many small problems, then solve the small problems one by one. Later combine the small solutions into a big solution.

For example here, do you know how to:
* open a file
* read a file
* write a file
* read a line from a file
* split a text line
* if/else
* loops
* functions

All things, you can learn in the tutorial, like I have helped before.

Or you would rather somebody else program for you? I think you rather want to learn for yourself Wink
Reply
#6
(Apr-12-2017, 12:44 PM)wavic Wrote: Didn't get it.
A row in the input file contains 1 "what a nice day"?
He wants to have separate file for each id with the name <id>.txt and the content of the tweet in it.

(Apr-12-2017, 12:44 PM)Qubayel Wrote: Actually im new to python
i dont have any idea on how to do this
if can help that will be great
thanks

in addition to advise by Kebap - we have nice basic level tutorial on files:
https://python-forum.io/Thread-Basic-Files
Reply
#7
(Apr-12-2017, 12:52 PM)buran Wrote: He wants to have separate file for each id with the name <id>.txt and the content of the tweet in it.
Yes, this is clear enough. I am confusing, how the input file is formatted.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Apr-12-2017, 12:52 PM)buran Wrote:
(Apr-12-2017, 12:44 PM)wavic Wrote: Didn't get it.
A row in the input file contains 1 "what a nice day"?
He wants to have separate file for each id with the name <id>.txt and the content of the tweet in it.

(Apr-12-2017, 12:44 PM)Qubayel Wrote: Actually im new to python
i dont have any idea on how to do this
if can help that will be great
thanks

in addition to advise by Kebap - we have nice basic level tutorial on files:
https://python-forum.io/Thread-Basic-Files

Exactly

(Apr-12-2017, 12:49 PM)Kebap Wrote:
(Apr-12-2017, 12:44 PM)Qubayel Wrote: i dont have any idea on how to do this 
if can help that will be great

If a programmer has a problem which seems to big to solve, they will split the big problem into many small problems, then solve the small problems one by one. Later combine the small solutions into a big solution.

For example here, do you know how to:
* open a file
* read a file
* write a file
* read a line from a file
* split a text line
* if/else
* loops
* functions

All things, you can learn in the tutorial, like I have helped before.

Or you would rather somebody else program for you? I think you rather want to learn for yourself Wink

I do know the baisics but I couldnt imagine how to do it
Reply
#9
Let's break it to simple tasks.
I am supposing that each line is formated like: id tweet text

# open the input file and read it.
with open('input.txt', 'r') as in_file:

    # read each line
    for line in in_file:

        #open an output file for writing
        #get the first element for the output file's name
        elements = line.split()
        with open('{}.txt'.format(elements[0]), 'w') as out_file:
                
            # write the rest of the line into the file
            out_file.write(' '.join(elements[1:])) # 
It's basically this.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(Apr-12-2017, 01:43 PM)wavic Wrote: Let's break it to simple tasks.
I am supposing that each line is formated like: id tweet text

# open the input file and read it.
with open('input.txt', 'r') as in_file:

    # read each line
    for line in in_file:

        #open an output file for writing
        #get the first element for the output file's name
        elements = line.split()
        with open('{}.txt'.format(elements[0]), 'w') as out_file:
                
            # write the rest of the line into the file
            out_file.write(' '.join(elements[1:])) # 
It's basically this.

man I really was Depressed, thanks aloooooot

(Apr-12-2017, 01:43 PM)wavic Wrote: Let's break it to simple tasks.
I am supposing that each line is formated like: id tweet text

# open the input file and read it.
with open('input.txt', 'r') as in_file:

    # read each line
    for line in in_file:

        #open an output file for writing
        #get the first element for the output file's name
        elements = line.split()
        with open('{}.txt'.format(elements[0]), 'w') as out_file:
                
            # write the rest of the line into the file
            out_file.write(' '.join(elements[1:])) # 
It's basically this.

I tried it and it worked perfectly, but is there any way to specify the path that new files  will go in?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Newbie question about switching between files - Python/Pycharm Busby222 3 655 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Making a question answering chatbot based on the files I upload into python. Joejones 1 1,310 May-19-2023, 03:09 PM
Last Post: deanhystad
  Unable to disable creation of __pycache__ and .pyc files kpyeri 1 8,690 Oct-03-2018, 11:22 AM
Last Post: Larz60+
  Question About Files SheeppOSU 1 1,985 Jun-24-2018, 11:41 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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