Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
abelwingnut
Dec 23, 2002


i am new to python so this is probably pretty easy.

let's say i have an if/else loop. if condition x, i want to set variable a = 'stringA', b = intB, and c = 'stringC'. if not condition x, i want to set variable a = 'stringAmod', b = intBmod, and c = 'stringCmod'. that's easy enough. what i'm unclear on is how best to combine the entire result set a, b, and c from the first condition to the entire result set a, b, and c from the else condition, and then write that entire set to a csv.

now, there's a simple way--in each condition, write each row to a csv after defining the variables. something like:

if x is true:
a = stringA
b = intB
c = stringC

write.writerow(a, b, c)

else:

a = stringAmod
b = intBmod
c = stringCmod

write.writerow(a, b, c)

--

however, i feel like a more efficient way would be to do something like:

if x is true:
a = stringA
b = intB
c = stringC

#i have no idea what the right thing here is, hence my question
jsonlikeobject = add(a, b, c)

else:
a = stringAmod
b = intBmod
c = stringCmod

jsonlikeobject = add(a, b, c)

writerow(cycle through jsonlikeobject, inserting each 'array' or whatever you'd call it.

does this make sense? if so, what am i looking for?

thanks.

abelwingnut fucked around with this message at 23:24 on Aug 18, 2020

Adbot
ADBOT LOVES YOU

abelwingnut
Dec 23, 2002


you prefer it over the 'csv' module? i've been recently learning how to deal with csv files in python and most things i found there deal with that module. i'm simply writing from existing csv and writing to new ones.

abelwingnut
Dec 23, 2002


Marx Was A Lib posted:

e: don't want to doublepost, however I just stumbled upon xlwings, this may have some promise.

relatedly, has anyone used this: https://developers.google.com/sheets/api/quickstart/python

and is it decent?

abelwingnut
Dec 23, 2002


i'm totally new to list comprehensions, so please forgive me if this is dumb.

i am trying to create a list comprehension and i am having a hell of a time with it. basically, i have:

list1 = [ ['a', 'b', 'c'] , ['d', 'e', 'f'] , [ 'g', 'h', 'i'] ]

so this is a list of lists. each sublist is a row of data imported from a csv.

--

list2 = [ 'j', 'k', 'l' ]

each of the elements here is a string representing a category. i want to add each category to each sublist. so i am trying to get to...

[ 'a', 'b', 'c', 'j'] , [ 'a', 'b', 'c', k' ] , [ 'a', 'b', 'c', 'l' ] , [ 'd', 'e', 'f', 'j' ], ...

--

list3 = [ 'm', 'n', 'o', 'p', 'q' ]

each of the elements here is a subcategory. i want to add element here to each of the new lists from directly above. so...

[ 'a', 'b', 'c', 'j', 'm' ] , [ 'a', 'b', 'c', 'j', 'n' ] , [ 'a', 'b', 'c', 'j', 'o' ] , [ 'a', 'b', 'c', 'j', 'p' ] , [ 'a', 'b', 'c', 'j', 'q' ] , [ 'a', 'b', 'c', 'k', 'm' ] , ...

which, in nested for loop land, would look like:

for x in list1:

for y in list2:

for z in list3:

<do stuff>

simple enough.

however, i'm having trouble getting from list1 to list2 via a list comprehension. basically i can't seem to append that category to the sublists. any ideas? what should it look like?

i have, effectively:

listLocation = [ x.append(y) for x in list1 for y in list2]

abelwingnut
Dec 23, 2002


sorry. kind of forgot the exist.

in any case, this isn't homework. just trying to figure out how to combine all these loops into one. i somehow thought it'd be cleaner, but it seems like it's getting messy. there are also conditions on the first and second loops. it's ugly.

abelwingnut
Dec 23, 2002


thanks for all the help--it's working. my logic for the loops was correct, but i was trying to add strings to lists in an incompatible way.

thanks, again!

abelwingnut
Dec 23, 2002


as a data guy who needs to run lots of one-off scripts and snippets here and there, i love notebooks. they’re a critical piece of my workflow within the anaconda platform.

abelwingnut
Dec 23, 2002


so i'm trying to get python running on my win10 machine. what's the best way? this windows subsystem for linux seems pretty nifty but it also feels like it could be a disaster. as of now, i'm only trying to do web stuff, but could see myself doing more machine learning things.

e: or do i even want to try? i guess i could pretty easily deploy a linux vm on azure and just run everything there. might be the best idea given this machine is also my personal.

abelwingnut fucked around with this message at 17:40 on Feb 18, 2022

abelwingnut
Dec 23, 2002


thanks for all of the replies!

i also discovered anaconda, which is intriguing. any thoughts on it versus the aforementioned options? seems great for data work, not so sure on dev work.

abelwingnut
Dec 23, 2002


i've always done the object first, then the descriptors.

so like, idCustomer, idTransaction, nameFirst, nameLast, etc.

helps me understand things a bit quicker in a glance

abelwingnut
Dec 23, 2002


python novice, so this should be easy.

i have 25 simple python scripts that each run a distinct query on a database and export them to individual csv.

i then have another 'orchestration' python script that aims to run all those, then upload the files to s3.

i've got the upload working just fine, but i'm not sure how to run all of those python scripts from within the orchestration script. basically, i'd want the os module to run through the directory containing all of the scripts, put them in a list or something, then run each individually and sequentially. once done, it then moves on to the upload process.

so...how do i go about running all of those scripts like this? i can easily get the filepaths for each. is there a simple way to run them from within this orchestration script?

Adbot
ADBOT LOVES YOU

abelwingnut
Dec 23, 2002


thank you for the help! i'll see what comes of all this and follow up i need some additional pointers.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply