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;








-1















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'









share|improve this question

















  • 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















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'









share|improve this question

















  • 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








-1








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'









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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












1 Answer
1






active

oldest

votes


















1














 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.






share|improve this answer























  • 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 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











  • you are right ! I somehow read append ;-).

    – Florian H
    Mar 26 at 7:43










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
);



);













draft saved

draft discarded


















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









1














 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.






share|improve this answer























  • 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 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











  • you are right ! I somehow read append ;-).

    – Florian H
    Mar 26 at 7:43















1














 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.






share|improve this answer























  • 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 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











  • you are right ! I somehow read append ;-).

    – Florian H
    Mar 26 at 7:43













1












1








1







 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.






share|improve this answer













 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.







share|improve this answer












share|improve this answer



share|improve this answer










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 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











  • 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











  • 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











  • @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
















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








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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript