AttributeError: 'tuple' object has no attribute, merging csvHow to merge two dictionaries in a single expression?How to output MySQL query results in CSV format?How to know if an object has an attribute in PythonHow to merge a specific commit in GitHow to join (merge) data frames (inner, outer, left, right)How do you merge two Git repositories?How to merge two arrays in JavaScript and de-duplicate itemsDetermine the type of an object?Merge / convert multiple PDF files into one PDF“Large data” work flows using pandas
Draw a line nicely around notes
What are the benefits to casting without the need for somatic components?
Is there a way to handmake alphabet pasta?
Why do the faithful have to say "And with your spirit " in Catholic Mass?
Is there any way for an Adventurers League, 5th level Wizard, to gain heavy armor proficiency?
What to look for in climbing shoes?
Can I send medicine to an American visitor in Canada?
Index Uniqueness Overhead
Why should I cook the flour first when making bechamel sauce?
Is `curl something | sudo bash -` a reasonably safe installation method?
Why does the Trade Federation become so alarmed upon learning the ambassadors are Jedi Knights?
Extension of trace on von Neumann subalgebra
Why is "dark" an adverb in this sentence?
Can a pizza stone be fixed after soap has been used to clean it?
What is this old "lemon-squeezer" shaped pan
Are L-functions uniquely determined by their values at negative integers?
Why did Steve Rogers choose Sam in Endgame?
What's the meaning of こそ in this sentence?
Why did Spider-Man take a detour to Dorset?
Can you perfectly wrap a cube with this blocky shape?
Using two linked programs, output ordinal numbers up to n
Source of story about the Vilna Gaon and immigration policy
Will it hurt my career to work as a graphic designer in a startup for beauty and skin care?
What exactly is a Hadouken?
AttributeError: 'tuple' object has no attribute, merging csv
How to merge two dictionaries in a single expression?How to output MySQL query results in CSV format?How to know if an object has an attribute in PythonHow to merge a specific commit in GitHow to join (merge) data frames (inner, outer, left, right)How do you merge two Git repositories?How to merge two arrays in JavaScript and de-duplicate itemsDetermine the type of an object?Merge / convert multiple PDF files into one PDF“Large data” work flows using pandas
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have multiple.csv
files into a folder and they have the same structure.
First two columns contain two values k1
and k2
the third column contain a numerical value.
I want to iterate on these files and merge them into a new csv
, where I will have the first two columns for K1
and K2
and then n
columns, one for each of the n
files.
This is my solution:
import glob
import csv
import os
def get_data(filename):
'''function to read the data form the input csv file to use in the analysis'''
with open(filename, 'r') as f:
reader = csv.reader(f)
result = tuple(row[:2]): row[2] for row in reader
return result
path='mypath'
for infile in glob.glob(os.path.join(path, '*.csv')):
print ("Current File Being Processed is: " + infile)
#use split to seperate the path and name of the file
(PATH, FILENAME) = os.path.split(infile)
all_data=[]
#adds the data from the csv file to a blank list so it can be operated on
all_data.extend(get_data(infile))
result =
for dic in all_data:
for key in (dic.viewkeys() | result.keys()):
if key in dic: result.setdefault(key, []).append(dic[key])
else: result.setdefault(key, []).append(0)
with open('mypath', 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in result:
writer.writerow(data)
but it returns
Traceback (most recent call last):
File "C:UsersDesktopp.py", line 26, in <module>
for key in (dic.viewkeys() | result.keys()):
AttributeError: 'tuple' object has no attribute 'viewkeys'
python csv merge
add a comment |
I have multiple.csv
files into a folder and they have the same structure.
First two columns contain two values k1
and k2
the third column contain a numerical value.
I want to iterate on these files and merge them into a new csv
, where I will have the first two columns for K1
and K2
and then n
columns, one for each of the n
files.
This is my solution:
import glob
import csv
import os
def get_data(filename):
'''function to read the data form the input csv file to use in the analysis'''
with open(filename, 'r') as f:
reader = csv.reader(f)
result = tuple(row[:2]): row[2] for row in reader
return result
path='mypath'
for infile in glob.glob(os.path.join(path, '*.csv')):
print ("Current File Being Processed is: " + infile)
#use split to seperate the path and name of the file
(PATH, FILENAME) = os.path.split(infile)
all_data=[]
#adds the data from the csv file to a blank list so it can be operated on
all_data.extend(get_data(infile))
result =
for dic in all_data:
for key in (dic.viewkeys() | result.keys()):
if key in dic: result.setdefault(key, []).append(dic[key])
else: result.setdefault(key, []).append(0)
with open('mypath', 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in result:
writer.writerow(data)
but it returns
Traceback (most recent call last):
File "C:UsersDesktopp.py", line 26, in <module>
for key in (dic.viewkeys() | result.keys()):
AttributeError: 'tuple' object has no attribute 'viewkeys'
python csv merge
1
are you using jupyter?
– Florian H
Mar 26 at 7:26
No, I'm working on Python IDLE
– ds1993
Mar 26 at 7:36
add a comment |
I have multiple.csv
files into a folder and they have the same structure.
First two columns contain two values k1
and k2
the third column contain a numerical value.
I want to iterate on these files and merge them into a new csv
, where I will have the first two columns for K1
and K2
and then n
columns, one for each of the n
files.
This is my solution:
import glob
import csv
import os
def get_data(filename):
'''function to read the data form the input csv file to use in the analysis'''
with open(filename, 'r') as f:
reader = csv.reader(f)
result = tuple(row[:2]): row[2] for row in reader
return result
path='mypath'
for infile in glob.glob(os.path.join(path, '*.csv')):
print ("Current File Being Processed is: " + infile)
#use split to seperate the path and name of the file
(PATH, FILENAME) = os.path.split(infile)
all_data=[]
#adds the data from the csv file to a blank list so it can be operated on
all_data.extend(get_data(infile))
result =
for dic in all_data:
for key in (dic.viewkeys() | result.keys()):
if key in dic: result.setdefault(key, []).append(dic[key])
else: result.setdefault(key, []).append(0)
with open('mypath', 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in result:
writer.writerow(data)
but it returns
Traceback (most recent call last):
File "C:UsersDesktopp.py", line 26, in <module>
for key in (dic.viewkeys() | result.keys()):
AttributeError: 'tuple' object has no attribute 'viewkeys'
python csv merge
I have multiple.csv
files into a folder and they have the same structure.
First two columns contain two values k1
and k2
the third column contain a numerical value.
I want to iterate on these files and merge them into a new csv
, where I will have the first two columns for K1
and K2
and then n
columns, one for each of the n
files.
This is my solution:
import glob
import csv
import os
def get_data(filename):
'''function to read the data form the input csv file to use in the analysis'''
with open(filename, 'r') as f:
reader = csv.reader(f)
result = tuple(row[:2]): row[2] for row in reader
return result
path='mypath'
for infile in glob.glob(os.path.join(path, '*.csv')):
print ("Current File Being Processed is: " + infile)
#use split to seperate the path and name of the file
(PATH, FILENAME) = os.path.split(infile)
all_data=[]
#adds the data from the csv file to a blank list so it can be operated on
all_data.extend(get_data(infile))
result =
for dic in all_data:
for key in (dic.viewkeys() | result.keys()):
if key in dic: result.setdefault(key, []).append(dic[key])
else: result.setdefault(key, []).append(0)
with open('mypath', 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in result:
writer.writerow(data)
but it returns
Traceback (most recent call last):
File "C:UsersDesktopp.py", line 26, in <module>
for key in (dic.viewkeys() | result.keys()):
AttributeError: 'tuple' object has no attribute 'viewkeys'
python csv merge
python csv merge
asked Mar 26 at 7:23
ds1993ds1993
11 bronze badge
11 bronze badge
1
are you using jupyter?
– Florian H
Mar 26 at 7:26
No, I'm working on Python IDLE
– ds1993
Mar 26 at 7:36
add a comment |
1
are you using jupyter?
– Florian H
Mar 26 at 7:26
No, I'm working on Python IDLE
– ds1993
Mar 26 at 7:36
1
1
are you using jupyter?
– Florian H
Mar 26 at 7:26
are you using jupyter?
– Florian H
Mar 26 at 7:26
No, I'm working on Python IDLE
– ds1993
Mar 26 at 7:36
No, I'm working on Python IDLE
– ds1993
Mar 26 at 7:36
add a comment |
1 Answer
1
active
oldest
votes
result = tuple(row[:2]): row[2] for row in reader
You are storing key as tuple(key) and hence iterating over it throwing error for you.
he is creating a dict with tuples as keys and putting that dict into a list, therfore he has something like [(.....):[.....]]. Therfore his dic variable in the loop should actually point to a dictionary.
– Florian H
Mar 26 at 7:38
Thanks. What could be a possible alternative solution?
– ds1993
Mar 26 at 7:40
@FlorianH no, they are doingall_data.extend(get_data(infile))
which iterates over the tuples in the dict returned byget_data
, adding them to the list, so it will be a list of tuples.
– juanpa.arrivillaga
Mar 26 at 7:42
@ds1993 perhaps you meantall_data.append(get_data(infile))
– juanpa.arrivillaga
Mar 26 at 7:42
you are right ! I somehow read append ;-).
– Florian H
Mar 26 at 7:43
|
show 5 more comments
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%2f55351731%2fattributeerror-tuple-object-has-no-attribute-merging-csv%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
result = tuple(row[:2]): row[2] for row in reader
You are storing key as tuple(key) and hence iterating over it throwing error for you.
he is creating a dict with tuples as keys and putting that dict into a list, therfore he has something like [(.....):[.....]]. Therfore his dic variable in the loop should actually point to a dictionary.
– Florian H
Mar 26 at 7:38
Thanks. What could be a possible alternative solution?
– ds1993
Mar 26 at 7:40
@FlorianH no, they are doingall_data.extend(get_data(infile))
which iterates over the tuples in the dict returned byget_data
, adding them to the list, so it will be a list of tuples.
– juanpa.arrivillaga
Mar 26 at 7:42
@ds1993 perhaps you meantall_data.append(get_data(infile))
– juanpa.arrivillaga
Mar 26 at 7:42
you are right ! I somehow read append ;-).
– Florian H
Mar 26 at 7:43
|
show 5 more comments
result = tuple(row[:2]): row[2] for row in reader
You are storing key as tuple(key) and hence iterating over it throwing error for you.
he is creating a dict with tuples as keys and putting that dict into a list, therfore he has something like [(.....):[.....]]. Therfore his dic variable in the loop should actually point to a dictionary.
– Florian H
Mar 26 at 7:38
Thanks. What could be a possible alternative solution?
– ds1993
Mar 26 at 7:40
@FlorianH no, they are doingall_data.extend(get_data(infile))
which iterates over the tuples in the dict returned byget_data
, adding them to the list, so it will be a list of tuples.
– juanpa.arrivillaga
Mar 26 at 7:42
@ds1993 perhaps you meantall_data.append(get_data(infile))
– juanpa.arrivillaga
Mar 26 at 7:42
you are right ! I somehow read append ;-).
– Florian H
Mar 26 at 7:43
|
show 5 more comments
result = tuple(row[:2]): row[2] for row in reader
You are storing key as tuple(key) and hence iterating over it throwing error for you.
result = tuple(row[:2]): row[2] for row in reader
You are storing key as tuple(key) and hence iterating over it throwing error for you.
answered Mar 26 at 7:30
PravinPravin
1771 gold badge4 silver badges19 bronze badges
1771 gold badge4 silver badges19 bronze badges
he is creating a dict with tuples as keys and putting that dict into a list, therfore he has something like [(.....):[.....]]. Therfore his dic variable in the loop should actually point to a dictionary.
– Florian H
Mar 26 at 7:38
Thanks. What could be a possible alternative solution?
– ds1993
Mar 26 at 7:40
@FlorianH no, they are doingall_data.extend(get_data(infile))
which iterates over the tuples in the dict returned byget_data
, adding them to the list, so it will be a list of tuples.
– juanpa.arrivillaga
Mar 26 at 7:42
@ds1993 perhaps you meantall_data.append(get_data(infile))
– juanpa.arrivillaga
Mar 26 at 7:42
you are right ! I somehow read append ;-).
– Florian H
Mar 26 at 7:43
|
show 5 more comments
he is creating a dict with tuples as keys and putting that dict into a list, therfore he has something like [(.....):[.....]]. Therfore his dic variable in the loop should actually point to a dictionary.
– Florian H
Mar 26 at 7:38
Thanks. What could be a possible alternative solution?
– ds1993
Mar 26 at 7:40
@FlorianH no, they are doingall_data.extend(get_data(infile))
which iterates over the tuples in the dict returned byget_data
, adding them to the list, so it will be a list of tuples.
– juanpa.arrivillaga
Mar 26 at 7:42
@ds1993 perhaps you meantall_data.append(get_data(infile))
– juanpa.arrivillaga
Mar 26 at 7:42
you are right ! I somehow read append ;-).
– Florian H
Mar 26 at 7:43
he is creating a dict with tuples as keys and putting that dict into a list, therfore he has something like [(.....):[.....]]. Therfore his dic variable in the loop should actually point to a dictionary.
– Florian H
Mar 26 at 7:38
he is creating a dict with tuples as keys and putting that dict into a list, therfore he has something like [(.....):[.....]]. Therfore his dic variable in the loop should actually point to a dictionary.
– Florian H
Mar 26 at 7:38
Thanks. What could be a possible alternative solution?
– ds1993
Mar 26 at 7:40
Thanks. What could be a possible alternative solution?
– ds1993
Mar 26 at 7:40
@FlorianH no, they are doing
all_data.extend(get_data(infile))
which iterates over the tuples in the dict returned by get_data
, adding them to the list, so it will be a list of tuples.– juanpa.arrivillaga
Mar 26 at 7:42
@FlorianH no, they are doing
all_data.extend(get_data(infile))
which iterates over the tuples in the dict returned by get_data
, adding them to the list, so it will be a list of tuples.– juanpa.arrivillaga
Mar 26 at 7:42
@ds1993 perhaps you meant
all_data.append(get_data(infile))
– juanpa.arrivillaga
Mar 26 at 7:42
@ds1993 perhaps you meant
all_data.append(get_data(infile))
– juanpa.arrivillaga
Mar 26 at 7:42
you are right ! I somehow read append ;-).
– Florian H
Mar 26 at 7:43
you are right ! I somehow read append ;-).
– Florian H
Mar 26 at 7:43
|
show 5 more comments
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55351731%2fattributeerror-tuple-object-has-no-attribute-merging-csv%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
are you using jupyter?
– Florian H
Mar 26 at 7:26
No, I'm working on Python IDLE
– ds1993
Mar 26 at 7:36