Concatenate csv column to string and print specific valueIs there a built-in function to print all the current properties and values of an object?Convert array to CSV/TSV-formated string in PythonPython Print String To Text FileHow can I print literal curly-brace characters in python string and also use .format on it?Concatenate item in list to stringsSelecting and operating on columns in a .csvSelect rows from a DataFrame based on values in a column in pandasPython - Using Input From CSV File/Splitting a StringBuild string by selecting column in csvHow to convert the column to numeric in python for sorting
Why, even after his imprisonment, people keep calling Hannibal Lecter "Doctor"?
May I know how to stop these death waves?
I transpose the source code, you transpose the input!
Why does C++ have 'Undefined Behaviour' and other languages like C# or Java don't?
irrational fear of hell and damnation and spiralling, please help me?
Why did UK NHS pay for homeopathic treatments?
Algorithm that generates orthogonal vectors: C++ implementation
What should I consider when deciding whether to delay an exam?
What happens to a net with the Returning Weapon artificer infusion after it hits?
Why is STARTTLS still used?
Hangman Game (YAHG)
How to justify getting additional team member when the current team is doing well?
Why does (inf + 0j)*1 evaluate to inf + nanj?
Difference between "rip up" and "rip down"
Beyond Futuristic Technology for an Alien Warship?
Character Transformation
How 象【しょう】 ( ≈かたち、 すがた、ようす) and 象【ぞう】 (どうぶつ) got to be written with the same kanji?
Youtube not blocked by iptables
Do wheelchair-accessible aircraft exist?
New road bike: alloy dual pivot brakes work poorly
What did Jesse Pinkman mix into Walt's coffee?
Does "as soon as" imply simultaneity?
Is a Middle Name a Given Name?
Do we have any particular tonal center in mind when we are NOT listening music?
Concatenate csv column to string and print specific value
Is there a built-in function to print all the current properties and values of an object?Convert array to CSV/TSV-formated string in PythonPython Print String To Text FileHow can I print literal curly-brace characters in python string and also use .format on it?Concatenate item in list to stringsSelecting and operating on columns in a .csvSelect rows from a DataFrame based on values in a column in pandasPython - Using Input From CSV File/Splitting a StringBuild string by selecting column in csvHow to convert the column to numeric in python for sorting
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a csv file contains three columns: date, time and temperature.
Now I have the following code and need to concatenate date, time and temperature in a temporary string and append the temporary string into an array that holds values where temperature > 80, and print the result.
import csv
data_array = []
output = []
with open("temp.csv") as csvfile:
csv_reader = csv.reader(csvfile, delimiter=",")
next(csv_reader, None)
for row in csv_reader:
data_array.append("date": row[0], "time": row[1], "temp": float(row[2]))
#create an array of dictionary to put related data together; convert the temperature to float.
for item in data_array:
if item['temp'] > 80:
output.append(item)
print(output)
I can only print a dictionary of those items but how can I concatenate items and print a result? Shouldn't I use dictionary?
python
add a comment
|
I have a csv file contains three columns: date, time and temperature.
Now I have the following code and need to concatenate date, time and temperature in a temporary string and append the temporary string into an array that holds values where temperature > 80, and print the result.
import csv
data_array = []
output = []
with open("temp.csv") as csvfile:
csv_reader = csv.reader(csvfile, delimiter=",")
next(csv_reader, None)
for row in csv_reader:
data_array.append("date": row[0], "time": row[1], "temp": float(row[2]))
#create an array of dictionary to put related data together; convert the temperature to float.
for item in data_array:
if item['temp'] > 80:
output.append(item)
print(output)
I can only print a dictionary of those items but how can I concatenate items and print a result? Shouldn't I use dictionary?
python
I'm confused what your expected output would look like. Could you post a sample?
– Reedinationer
Mar 28 at 18:13
Sure. like [‘1/2/2018 10:00 AM’, 65.5], [1/2/2018 11:00 AM’, 67.3], …
– Biu
Mar 28 at 18:15
add a comment
|
I have a csv file contains three columns: date, time and temperature.
Now I have the following code and need to concatenate date, time and temperature in a temporary string and append the temporary string into an array that holds values where temperature > 80, and print the result.
import csv
data_array = []
output = []
with open("temp.csv") as csvfile:
csv_reader = csv.reader(csvfile, delimiter=",")
next(csv_reader, None)
for row in csv_reader:
data_array.append("date": row[0], "time": row[1], "temp": float(row[2]))
#create an array of dictionary to put related data together; convert the temperature to float.
for item in data_array:
if item['temp'] > 80:
output.append(item)
print(output)
I can only print a dictionary of those items but how can I concatenate items and print a result? Shouldn't I use dictionary?
python
I have a csv file contains three columns: date, time and temperature.
Now I have the following code and need to concatenate date, time and temperature in a temporary string and append the temporary string into an array that holds values where temperature > 80, and print the result.
import csv
data_array = []
output = []
with open("temp.csv") as csvfile:
csv_reader = csv.reader(csvfile, delimiter=",")
next(csv_reader, None)
for row in csv_reader:
data_array.append("date": row[0], "time": row[1], "temp": float(row[2]))
#create an array of dictionary to put related data together; convert the temperature to float.
for item in data_array:
if item['temp'] > 80:
output.append(item)
print(output)
I can only print a dictionary of those items but how can I concatenate items and print a result? Shouldn't I use dictionary?
python
python
edited Mar 28 at 19:15
mttpgn
2282 silver badges11 bronze badges
2282 silver badges11 bronze badges
asked Mar 28 at 18:11
BiuBiu
165 bronze badges
165 bronze badges
I'm confused what your expected output would look like. Could you post a sample?
– Reedinationer
Mar 28 at 18:13
Sure. like [‘1/2/2018 10:00 AM’, 65.5], [1/2/2018 11:00 AM’, 67.3], …
– Biu
Mar 28 at 18:15
add a comment
|
I'm confused what your expected output would look like. Could you post a sample?
– Reedinationer
Mar 28 at 18:13
Sure. like [‘1/2/2018 10:00 AM’, 65.5], [1/2/2018 11:00 AM’, 67.3], …
– Biu
Mar 28 at 18:15
I'm confused what your expected output would look like. Could you post a sample?
– Reedinationer
Mar 28 at 18:13
I'm confused what your expected output would look like. Could you post a sample?
– Reedinationer
Mar 28 at 18:13
Sure. like [‘1/2/2018 10:00 AM’, 65.5], [1/2/2018 11:00 AM’, 67.3], …
– Biu
Mar 28 at 18:15
Sure. like [‘1/2/2018 10:00 AM’, 65.5], [1/2/2018 11:00 AM’, 67.3], …
– Biu
Mar 28 at 18:15
add a comment
|
2 Answers
2
active
oldest
votes
You don't need a temporary list of dicts. Simply build a list of concatenated date and time and the temperature and append the list to the output list when the temperature is greater than 80 directly withint the loop that iterates through the CSV reader:
with open("temp.csv") as csvfile:
csv_reader = csv.reader(csvfile, delimiter=",")
next(csv_reader, None)
for date, time, temperature in csv_reader:
temperature = float(temperature)
if temperature > 80:
output.append([' '.join((date, time)), temperature])
1
Thank you very much. My second day of python so don't now lots of simple stuff yet.
– Biu
Mar 28 at 18:35
add a comment
|
Just change
output.append(item)
To
output.append([' '.format(item['date'], item['time']), item['temp']])
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/4.0/"u003ecc by-sa 4.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%2f55404288%2fconcatenate-csv-column-to-string-and-print-specific-value%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't need a temporary list of dicts. Simply build a list of concatenated date and time and the temperature and append the list to the output list when the temperature is greater than 80 directly withint the loop that iterates through the CSV reader:
with open("temp.csv") as csvfile:
csv_reader = csv.reader(csvfile, delimiter=",")
next(csv_reader, None)
for date, time, temperature in csv_reader:
temperature = float(temperature)
if temperature > 80:
output.append([' '.join((date, time)), temperature])
1
Thank you very much. My second day of python so don't now lots of simple stuff yet.
– Biu
Mar 28 at 18:35
add a comment
|
You don't need a temporary list of dicts. Simply build a list of concatenated date and time and the temperature and append the list to the output list when the temperature is greater than 80 directly withint the loop that iterates through the CSV reader:
with open("temp.csv") as csvfile:
csv_reader = csv.reader(csvfile, delimiter=",")
next(csv_reader, None)
for date, time, temperature in csv_reader:
temperature = float(temperature)
if temperature > 80:
output.append([' '.join((date, time)), temperature])
1
Thank you very much. My second day of python so don't now lots of simple stuff yet.
– Biu
Mar 28 at 18:35
add a comment
|
You don't need a temporary list of dicts. Simply build a list of concatenated date and time and the temperature and append the list to the output list when the temperature is greater than 80 directly withint the loop that iterates through the CSV reader:
with open("temp.csv") as csvfile:
csv_reader = csv.reader(csvfile, delimiter=",")
next(csv_reader, None)
for date, time, temperature in csv_reader:
temperature = float(temperature)
if temperature > 80:
output.append([' '.join((date, time)), temperature])
You don't need a temporary list of dicts. Simply build a list of concatenated date and time and the temperature and append the list to the output list when the temperature is greater than 80 directly withint the loop that iterates through the CSV reader:
with open("temp.csv") as csvfile:
csv_reader = csv.reader(csvfile, delimiter=",")
next(csv_reader, None)
for date, time, temperature in csv_reader:
temperature = float(temperature)
if temperature > 80:
output.append([' '.join((date, time)), temperature])
edited Mar 28 at 18:36
answered Mar 28 at 18:18
blhsingblhsing
50.9k5 gold badges19 silver badges49 bronze badges
50.9k5 gold badges19 silver badges49 bronze badges
1
Thank you very much. My second day of python so don't now lots of simple stuff yet.
– Biu
Mar 28 at 18:35
add a comment
|
1
Thank you very much. My second day of python so don't now lots of simple stuff yet.
– Biu
Mar 28 at 18:35
1
1
Thank you very much. My second day of python so don't now lots of simple stuff yet.
– Biu
Mar 28 at 18:35
Thank you very much. My second day of python so don't now lots of simple stuff yet.
– Biu
Mar 28 at 18:35
add a comment
|
Just change
output.append(item)
To
output.append([' '.format(item['date'], item['time']), item['temp']])
add a comment
|
Just change
output.append(item)
To
output.append([' '.format(item['date'], item['time']), item['temp']])
add a comment
|
Just change
output.append(item)
To
output.append([' '.format(item['date'], item['time']), item['temp']])
Just change
output.append(item)
To
output.append([' '.format(item['date'], item['time']), item['temp']])
answered Mar 28 at 18:18
ReedinationerReedinationer
4,0831 gold badge4 silver badges27 bronze badges
4,0831 gold badge4 silver badges27 bronze badges
add a comment
|
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%2f55404288%2fconcatenate-csv-column-to-string-and-print-specific-value%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
I'm confused what your expected output would look like. Could you post a sample?
– Reedinationer
Mar 28 at 18:13
Sure. like [‘1/2/2018 10:00 AM’, 65.5], [1/2/2018 11:00 AM’, 67.3], …
– Biu
Mar 28 at 18:15