Python Forum

Full Version: files creation question, any help plz ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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?
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.
(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
Didn't get it.
A row in the input file contains 1 "what a nice day"?
(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
(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
(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.
(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
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.
(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?
Pages: 1 2