Adding column of extracted character to file without using pandas
I have a csv file data that is as shown
09-oct-2010 00:00:00,A,MIN1 MX2 PT34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28
I am trying to extract the digit after "PT". After extracting the numbers, I would like to add the numbers to a new column such as
09-oct-2010 00:00:00,A,MIN1 MX2 PT34,34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9,9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28,28
Below is my code. Please advice on how to extract without using pandas and add back to the csv file. If using pandas, how can i read the numbers from PT
x = str1Var.get()
with open(x,"r") as infile:
writer = csv.writer(infile)
for line in infile.readlines():
dur = line[-4:-2]
print(dur)
The dur contain whole row of numbers extracted
python
add a comment |
I have a csv file data that is as shown
09-oct-2010 00:00:00,A,MIN1 MX2 PT34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28
I am trying to extract the digit after "PT". After extracting the numbers, I would like to add the numbers to a new column such as
09-oct-2010 00:00:00,A,MIN1 MX2 PT34,34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9,9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28,28
Below is my code. Please advice on how to extract without using pandas and add back to the csv file. If using pandas, how can i read the numbers from PT
x = str1Var.get()
with open(x,"r") as infile:
writer = csv.writer(infile)
for line in infile.readlines():
dur = line[-4:-2]
print(dur)
The dur contain whole row of numbers extracted
python
add a comment |
I have a csv file data that is as shown
09-oct-2010 00:00:00,A,MIN1 MX2 PT34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28
I am trying to extract the digit after "PT". After extracting the numbers, I would like to add the numbers to a new column such as
09-oct-2010 00:00:00,A,MIN1 MX2 PT34,34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9,9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28,28
Below is my code. Please advice on how to extract without using pandas and add back to the csv file. If using pandas, how can i read the numbers from PT
x = str1Var.get()
with open(x,"r") as infile:
writer = csv.writer(infile)
for line in infile.readlines():
dur = line[-4:-2]
print(dur)
The dur contain whole row of numbers extracted
python
I have a csv file data that is as shown
09-oct-2010 00:00:00,A,MIN1 MX2 PT34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28
I am trying to extract the digit after "PT". After extracting the numbers, I would like to add the numbers to a new column such as
09-oct-2010 00:00:00,A,MIN1 MX2 PT34,34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9,9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28,28
Below is my code. Please advice on how to extract without using pandas and add back to the csv file. If using pandas, how can i read the numbers from PT
x = str1Var.get()
with open(x,"r") as infile:
writer = csv.writer(infile)
for line in infile.readlines():
dur = line[-4:-2]
print(dur)
The dur contain whole row of numbers extracted
python
python
asked Nov 22 '18 at 5:21


ThanksForHelpingThanksForHelping
647
647
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
This gives me the exact output of what you're expecting without using any specific package.
sourceUrl = open(r"C:UsersseramuDesktoptoolsstackoverflowdummy.txt", "r")
csvOutput = open(r"C:UsersseramuDesktoptoolsstackoverflowdummycsv.csv", "w")
for st in sourceUrl:
csvOutput.write(st.strip('n') + ',')
newlist = st.split(" ")
newstr = ''.join((ch if ch in '0123456789.-e' else ' ') for ch in newlist[-1])
listnum = [int(i) for i in newstr.split()]
csvOutput.write(str(listnum[0]) + 'n')
csvOutput.close()
sourceUrl.close()
Input file:
09-oct-2010 00:00:00,A,MIN1 MX2 PT34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28
Output file:
09-oct-2010 00:00:00,A,MIN1 MX2 PT34,34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9,9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28,28
"dur = line[-4:-2]" in the question will not fetch the proper number always. So I have split the string in to a list and considered only the last element of the list. Once I have the last element I am checking character wise since we are not sure how many digits will be present in that so iterating thru it to find the numbers present in that. While iterating I am checking whether it is a character or a number and storing it in a new list and writing it in the file with a newline character.
Hi! It worked! But do you mind explaining the steps you took for me? sorry for any inconvenience caused
– ThanksForHelping
Nov 22 '18 at 7:23
I have edited and added the explanation in the answer itself :) let me know in case if you need more details.
– Sekar Ramu
Nov 22 '18 at 7:36
Thanks for the update! Would like to consult you if I want to search within the string to get the number after certain character(e.g. after 'Y' or after 'R'). how can I edit my code to suit such example?
– ThanksForHelping
Nov 23 '18 at 6:51
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424333%2fadding-column-of-extracted-character-to-file-without-using-pandas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This gives me the exact output of what you're expecting without using any specific package.
sourceUrl = open(r"C:UsersseramuDesktoptoolsstackoverflowdummy.txt", "r")
csvOutput = open(r"C:UsersseramuDesktoptoolsstackoverflowdummycsv.csv", "w")
for st in sourceUrl:
csvOutput.write(st.strip('n') + ',')
newlist = st.split(" ")
newstr = ''.join((ch if ch in '0123456789.-e' else ' ') for ch in newlist[-1])
listnum = [int(i) for i in newstr.split()]
csvOutput.write(str(listnum[0]) + 'n')
csvOutput.close()
sourceUrl.close()
Input file:
09-oct-2010 00:00:00,A,MIN1 MX2 PT34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28
Output file:
09-oct-2010 00:00:00,A,MIN1 MX2 PT34,34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9,9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28,28
"dur = line[-4:-2]" in the question will not fetch the proper number always. So I have split the string in to a list and considered only the last element of the list. Once I have the last element I am checking character wise since we are not sure how many digits will be present in that so iterating thru it to find the numbers present in that. While iterating I am checking whether it is a character or a number and storing it in a new list and writing it in the file with a newline character.
Hi! It worked! But do you mind explaining the steps you took for me? sorry for any inconvenience caused
– ThanksForHelping
Nov 22 '18 at 7:23
I have edited and added the explanation in the answer itself :) let me know in case if you need more details.
– Sekar Ramu
Nov 22 '18 at 7:36
Thanks for the update! Would like to consult you if I want to search within the string to get the number after certain character(e.g. after 'Y' or after 'R'). how can I edit my code to suit such example?
– ThanksForHelping
Nov 23 '18 at 6:51
add a comment |
This gives me the exact output of what you're expecting without using any specific package.
sourceUrl = open(r"C:UsersseramuDesktoptoolsstackoverflowdummy.txt", "r")
csvOutput = open(r"C:UsersseramuDesktoptoolsstackoverflowdummycsv.csv", "w")
for st in sourceUrl:
csvOutput.write(st.strip('n') + ',')
newlist = st.split(" ")
newstr = ''.join((ch if ch in '0123456789.-e' else ' ') for ch in newlist[-1])
listnum = [int(i) for i in newstr.split()]
csvOutput.write(str(listnum[0]) + 'n')
csvOutput.close()
sourceUrl.close()
Input file:
09-oct-2010 00:00:00,A,MIN1 MX2 PT34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28
Output file:
09-oct-2010 00:00:00,A,MIN1 MX2 PT34,34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9,9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28,28
"dur = line[-4:-2]" in the question will not fetch the proper number always. So I have split the string in to a list and considered only the last element of the list. Once I have the last element I am checking character wise since we are not sure how many digits will be present in that so iterating thru it to find the numbers present in that. While iterating I am checking whether it is a character or a number and storing it in a new list and writing it in the file with a newline character.
Hi! It worked! But do you mind explaining the steps you took for me? sorry for any inconvenience caused
– ThanksForHelping
Nov 22 '18 at 7:23
I have edited and added the explanation in the answer itself :) let me know in case if you need more details.
– Sekar Ramu
Nov 22 '18 at 7:36
Thanks for the update! Would like to consult you if I want to search within the string to get the number after certain character(e.g. after 'Y' or after 'R'). how can I edit my code to suit such example?
– ThanksForHelping
Nov 23 '18 at 6:51
add a comment |
This gives me the exact output of what you're expecting without using any specific package.
sourceUrl = open(r"C:UsersseramuDesktoptoolsstackoverflowdummy.txt", "r")
csvOutput = open(r"C:UsersseramuDesktoptoolsstackoverflowdummycsv.csv", "w")
for st in sourceUrl:
csvOutput.write(st.strip('n') + ',')
newlist = st.split(" ")
newstr = ''.join((ch if ch in '0123456789.-e' else ' ') for ch in newlist[-1])
listnum = [int(i) for i in newstr.split()]
csvOutput.write(str(listnum[0]) + 'n')
csvOutput.close()
sourceUrl.close()
Input file:
09-oct-2010 00:00:00,A,MIN1 MX2 PT34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28
Output file:
09-oct-2010 00:00:00,A,MIN1 MX2 PT34,34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9,9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28,28
"dur = line[-4:-2]" in the question will not fetch the proper number always. So I have split the string in to a list and considered only the last element of the list. Once I have the last element I am checking character wise since we are not sure how many digits will be present in that so iterating thru it to find the numbers present in that. While iterating I am checking whether it is a character or a number and storing it in a new list and writing it in the file with a newline character.
This gives me the exact output of what you're expecting without using any specific package.
sourceUrl = open(r"C:UsersseramuDesktoptoolsstackoverflowdummy.txt", "r")
csvOutput = open(r"C:UsersseramuDesktoptoolsstackoverflowdummycsv.csv", "w")
for st in sourceUrl:
csvOutput.write(st.strip('n') + ',')
newlist = st.split(" ")
newstr = ''.join((ch if ch in '0123456789.-e' else ' ') for ch in newlist[-1])
listnum = [int(i) for i in newstr.split()]
csvOutput.write(str(listnum[0]) + 'n')
csvOutput.close()
sourceUrl.close()
Input file:
09-oct-2010 00:00:00,A,MIN1 MX2 PT34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28
Output file:
09-oct-2010 00:00:00,A,MIN1 MX2 PT34,34
09-oct-2010 00:00:00,B,MIN1 G2 Y4 R6 MX2 FL3 PT9,9
09-oct-2010 00:00:43,A,TG1 MIN1 PT28,28
"dur = line[-4:-2]" in the question will not fetch the proper number always. So I have split the string in to a list and considered only the last element of the list. Once I have the last element I am checking character wise since we are not sure how many digits will be present in that so iterating thru it to find the numbers present in that. While iterating I am checking whether it is a character or a number and storing it in a new list and writing it in the file with a newline character.
edited Nov 22 '18 at 7:33
answered Nov 22 '18 at 7:06
Sekar RamuSekar Ramu
164110
164110
Hi! It worked! But do you mind explaining the steps you took for me? sorry for any inconvenience caused
– ThanksForHelping
Nov 22 '18 at 7:23
I have edited and added the explanation in the answer itself :) let me know in case if you need more details.
– Sekar Ramu
Nov 22 '18 at 7:36
Thanks for the update! Would like to consult you if I want to search within the string to get the number after certain character(e.g. after 'Y' or after 'R'). how can I edit my code to suit such example?
– ThanksForHelping
Nov 23 '18 at 6:51
add a comment |
Hi! It worked! But do you mind explaining the steps you took for me? sorry for any inconvenience caused
– ThanksForHelping
Nov 22 '18 at 7:23
I have edited and added the explanation in the answer itself :) let me know in case if you need more details.
– Sekar Ramu
Nov 22 '18 at 7:36
Thanks for the update! Would like to consult you if I want to search within the string to get the number after certain character(e.g. after 'Y' or after 'R'). how can I edit my code to suit such example?
– ThanksForHelping
Nov 23 '18 at 6:51
Hi! It worked! But do you mind explaining the steps you took for me? sorry for any inconvenience caused
– ThanksForHelping
Nov 22 '18 at 7:23
Hi! It worked! But do you mind explaining the steps you took for me? sorry for any inconvenience caused
– ThanksForHelping
Nov 22 '18 at 7:23
I have edited and added the explanation in the answer itself :) let me know in case if you need more details.
– Sekar Ramu
Nov 22 '18 at 7:36
I have edited and added the explanation in the answer itself :) let me know in case if you need more details.
– Sekar Ramu
Nov 22 '18 at 7:36
Thanks for the update! Would like to consult you if I want to search within the string to get the number after certain character(e.g. after 'Y' or after 'R'). how can I edit my code to suit such example?
– ThanksForHelping
Nov 23 '18 at 6:51
Thanks for the update! Would like to consult you if I want to search within the string to get the number after certain character(e.g. after 'Y' or after 'R'). how can I edit my code to suit such example?
– ThanksForHelping
Nov 23 '18 at 6:51
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424333%2fadding-column-of-extracted-character-to-file-without-using-pandas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown