CSV file written but output is not correct
I used python 3.5 to write a CSV file using the information from a previous CSV file. Here is an input line from the first file:
John Smith johnsmith@gmail.com Yellow Johnsmith, LLC
The format is first name, last name, email, favorite color, and company. The delimiter for this file is comma. The next CSV file will have all the same information but the delimiter will be changed from commas to tabs. Heres the script I wrote:
import csv
with open('combinedtable.csv','r') as csv_file, open('alteredcombinedtable.csv','w') as csv_file2:
csv_reader = csv.reader(csv_file, delimiter=',')
csv_writer = csv.writer(csv_file2, delimiter='t')
for row in csv_file:
csv_writer.writerow(row)
The script created the CSV file but the output was not correct. Here is what it looks like:
"f i r s t n a m e " " l a s t n a m e " " e m a i l a d d r e s s " " f a v o r i t e c o l o r " " c o m p a n y """
It only has the headers and they're separated like this when pasted from the csv file. Would anyone be able to explain why this output occurred?
python python-3.x csv
add a comment |
I used python 3.5 to write a CSV file using the information from a previous CSV file. Here is an input line from the first file:
John Smith johnsmith@gmail.com Yellow Johnsmith, LLC
The format is first name, last name, email, favorite color, and company. The delimiter for this file is comma. The next CSV file will have all the same information but the delimiter will be changed from commas to tabs. Heres the script I wrote:
import csv
with open('combinedtable.csv','r') as csv_file, open('alteredcombinedtable.csv','w') as csv_file2:
csv_reader = csv.reader(csv_file, delimiter=',')
csv_writer = csv.writer(csv_file2, delimiter='t')
for row in csv_file:
csv_writer.writerow(row)
The script created the CSV file but the output was not correct. Here is what it looks like:
"f i r s t n a m e " " l a s t n a m e " " e m a i l a d d r e s s " " f a v o r i t e c o l o r " " c o m p a n y """
It only has the headers and they're separated like this when pasted from the csv file. Would anyone be able to explain why this output occurred?
python python-3.x csv
1
The for statement is wrong. You should be iterating over csv_reader.
– Q-life
Nov 24 '18 at 4:09
I think @Q-life's is correct. Also note that you should include anewline=''
keyword argument when youopen()
csv files for reading or writing as shown in the examples in the module's documentation.
– martineau
Nov 24 '18 at 7:24
Thanks, both of your recommendations worked out perfectly and I was able to correctly write my code. Really appreciate it!
– A.Gar
Nov 25 '18 at 1:17
add a comment |
I used python 3.5 to write a CSV file using the information from a previous CSV file. Here is an input line from the first file:
John Smith johnsmith@gmail.com Yellow Johnsmith, LLC
The format is first name, last name, email, favorite color, and company. The delimiter for this file is comma. The next CSV file will have all the same information but the delimiter will be changed from commas to tabs. Heres the script I wrote:
import csv
with open('combinedtable.csv','r') as csv_file, open('alteredcombinedtable.csv','w') as csv_file2:
csv_reader = csv.reader(csv_file, delimiter=',')
csv_writer = csv.writer(csv_file2, delimiter='t')
for row in csv_file:
csv_writer.writerow(row)
The script created the CSV file but the output was not correct. Here is what it looks like:
"f i r s t n a m e " " l a s t n a m e " " e m a i l a d d r e s s " " f a v o r i t e c o l o r " " c o m p a n y """
It only has the headers and they're separated like this when pasted from the csv file. Would anyone be able to explain why this output occurred?
python python-3.x csv
I used python 3.5 to write a CSV file using the information from a previous CSV file. Here is an input line from the first file:
John Smith johnsmith@gmail.com Yellow Johnsmith, LLC
The format is first name, last name, email, favorite color, and company. The delimiter for this file is comma. The next CSV file will have all the same information but the delimiter will be changed from commas to tabs. Heres the script I wrote:
import csv
with open('combinedtable.csv','r') as csv_file, open('alteredcombinedtable.csv','w') as csv_file2:
csv_reader = csv.reader(csv_file, delimiter=',')
csv_writer = csv.writer(csv_file2, delimiter='t')
for row in csv_file:
csv_writer.writerow(row)
The script created the CSV file but the output was not correct. Here is what it looks like:
"f i r s t n a m e " " l a s t n a m e " " e m a i l a d d r e s s " " f a v o r i t e c o l o r " " c o m p a n y """
It only has the headers and they're separated like this when pasted from the csv file. Would anyone be able to explain why this output occurred?
python python-3.x csv
python python-3.x csv
edited Nov 24 '18 at 7:18
martineau
68k1090183
68k1090183
asked Nov 24 '18 at 4:02
A.GarA.Gar
41
41
1
The for statement is wrong. You should be iterating over csv_reader.
– Q-life
Nov 24 '18 at 4:09
I think @Q-life's is correct. Also note that you should include anewline=''
keyword argument when youopen()
csv files for reading or writing as shown in the examples in the module's documentation.
– martineau
Nov 24 '18 at 7:24
Thanks, both of your recommendations worked out perfectly and I was able to correctly write my code. Really appreciate it!
– A.Gar
Nov 25 '18 at 1:17
add a comment |
1
The for statement is wrong. You should be iterating over csv_reader.
– Q-life
Nov 24 '18 at 4:09
I think @Q-life's is correct. Also note that you should include anewline=''
keyword argument when youopen()
csv files for reading or writing as shown in the examples in the module's documentation.
– martineau
Nov 24 '18 at 7:24
Thanks, both of your recommendations worked out perfectly and I was able to correctly write my code. Really appreciate it!
– A.Gar
Nov 25 '18 at 1:17
1
1
The for statement is wrong. You should be iterating over csv_reader.
– Q-life
Nov 24 '18 at 4:09
The for statement is wrong. You should be iterating over csv_reader.
– Q-life
Nov 24 '18 at 4:09
I think @Q-life's is correct. Also note that you should include a
newline=''
keyword argument when you open()
csv files for reading or writing as shown in the examples in the module's documentation.– martineau
Nov 24 '18 at 7:24
I think @Q-life's is correct. Also note that you should include a
newline=''
keyword argument when you open()
csv files for reading or writing as shown in the examples in the module's documentation.– martineau
Nov 24 '18 at 7:24
Thanks, both of your recommendations worked out perfectly and I was able to correctly write my code. Really appreciate it!
– A.Gar
Nov 25 '18 at 1:17
Thanks, both of your recommendations worked out perfectly and I was able to correctly write my code. Really appreciate it!
– A.Gar
Nov 25 '18 at 1:17
add a comment |
0
active
oldest
votes
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%2f53455048%2fcsv-file-written-but-output-is-not-correct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53455048%2fcsv-file-written-but-output-is-not-correct%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
1
The for statement is wrong. You should be iterating over csv_reader.
– Q-life
Nov 24 '18 at 4:09
I think @Q-life's is correct. Also note that you should include a
newline=''
keyword argument when youopen()
csv files for reading or writing as shown in the examples in the module's documentation.– martineau
Nov 24 '18 at 7:24
Thanks, both of your recommendations worked out perfectly and I was able to correctly write my code. Really appreciate it!
– A.Gar
Nov 25 '18 at 1:17