Posts: 15
Threads: 6
Joined: Mar 2017
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?
Posts: 470
Threads: 29
Joined: Sep 2016
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.
Posts: 15
Threads: 6
Joined: Mar 2017
(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
Posts: 2,953
Threads: 48
Joined: Sep 2016
Didn't get it.
A row in the input file contains 1 "what a nice day" ?
Posts: 470
Threads: 29
Joined: Sep 2016
Apr-12-2017, 12:49 PM
(This post was last modified: Apr-12-2017, 12:50 PM by Kebap.)
(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
Posts: 8,154
Threads: 160
Joined: Sep 2016
(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
Posts: 2,953
Threads: 48
Joined: Sep 2016
Apr-12-2017, 01:05 PM
(This post was last modified: Apr-12-2017, 01:05 PM by wavic.)
(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.
Posts: 15
Threads: 6
Joined: Mar 2017
Apr-12-2017, 01:21 PM
(This post was last modified: Apr-12-2017, 01:27 PM by Qubayel.)
(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 
I do know the baisics but I couldnt imagine how to do it
Posts: 2,953
Threads: 48
Joined: Sep 2016
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.
Posts: 15
Threads: 6
Joined: Mar 2017
Apr-12-2017, 02:10 PM
(This post was last modified: Apr-12-2017, 02:19 PM by Qubayel.)
(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?
|