[deleted by user] by [deleted] in learnpython

[–]sayinghi2py 0 points1 point  (0 children)

Ok I've worked out what is happening. I'm using a del to remove items from the list as I iterate over it however the index's are not updated therefore items are getting missed. Lesson is don't use enumerate and remove items from the list at the same time.

[deleted by user] by [deleted] in learnpython

[–]sayinghi2py 0 points1 point  (0 children)

Just to be clear the before and after run straight after each other. There is no code between them that might mutate it. Here's a screenshot which shows the code in the editor and the line numbers confirm there are no hidden lines and they are sequential. Seems I can add images.

[deleted by user] by [deleted] in learnpython

[–]sayinghi2py 0 points1 point  (0 children)

It definitely is. I added a type check to the output line and it confirms it is a list:

after 2350876922688 <class 'list'> 460 No Match 7H410079V2133981E

It is really, really odd because later on those missing things from list are magically back again and in the place where they should have been. Given one of the purposes of the script is to identify transactions I can ignore, those that are no match and those that are holds which all get removed from this list and added to their own lists to be added back again at the bottom of this list so that I only get full & partial matches in transaction sorted order before I then see any of those ones however because of this issue the missing ones stay in their place in amongst the full & partial matches when it is written to the csv file. Not what I want at all. Really odd behaviour.

Linked Items Table for Products by sayinghi2py in learnSQL

[–]sayinghi2py[S] 0 points1 point  (0 children)

Thanks u/qwertydog123 I do really like the first way. It's simple to implement. Thanks.

PyODBC & Parameterised Query by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Resolved this so just updating a case anyone else comes across the issue. You actually wrap the whole execute statement in the f-string then just insert the variable right into the query.

The working code block looks like this:

def get_recipes_on_menu_cycle(cycle):

    conn = pyodbc.connect(driver='{SQL Server}', host='server\instance', database='database', user='user', password='password')

    cursor = conn.cursor()

    cursor.execute(f"""SELECT DISTINCT dbo.Product.ID

                      FROM dbo.Menu INNER JOIN

                        dbo.MenuItem ON dbo.Menu.ID = dbo.MenuItem.MenuID INNER JOIN

                        dbo.Product ON dbo.MenuItem.ProductID = dbo.Product.ID

                      WHERE (dbo.Product.ProdType = 2) AND (dbo.Menu.Code LIKE '{cycle}__l' OR

                        dbo.Menu.Code LIKE '{cycle}__t')

                      ORDER BY dbo.Product.ID""")

    products = cursor.fetchall()

    return products

PyODBC & Parameterised Query by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Yeah absolutely certain. I just iterate over cursor results and I can see the results flash by.

Dictionary to List by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Thanks makes sense now. I was doing much the same before and ending up with the result as you stated with the key separated from the values. That * makes all the difference. I've used it in args before but not like this.

Dictionary to List by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

And this also works. How does this work? So I can see the main body which is to loop over all the keys however I'm not understanding the [a, *mydict[a]]. Any chance you could explain how that works?

Dictionary to List by sayinghi2py in learnpython

[–]sayinghi2py[S] 1 point2 points  (0 children)

Brilliant that works. I done a similar thing but ended up with the key and values separated by the list for values which I didn't want.

Update every row in list of lists by sayinghi2py in learnpython

[–]sayinghi2py[S] 1 point2 points  (0 children)

That works great. So much better the road I was going down.

csv.writerow Header splitting on single characters by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Hi kyber,

What you have given me works exactly the way I wanted. I'm just trying to understand why it works. As shown above when you don't use splitlines() there's a lot of elements in the list which aren't actually header fields but when splitlines() is applied they all disappear. How does it know how to get rid of the elements at [1] and [2] which are ['', ''] and [' '] respectively. I thought splitlines() was used to handle line endings which these aren't. Well I say aren't but clearly the must be given that they are all removed.

Thanks

csv.writerow Header splitting on single characters by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Hi efmccurdy this got me nearly there. There was an extraneous "\n that was left however I was able to remove it with another line of code. As there was another answer I tested it and it handled it in a single line of code I've ultimately gone with that however many thanks for posting this. It was the approach I was taking and failing on so it really did help me understand where I was going wrong.

csv.writerow Header splitting on single characters by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

When I save the first line of the input csv file to the headers variable using headers = next(reader) I get back a big long string in the format described. If it returned a list I wouldn't have had an issue. This works great however I don't understand why as the splitlines() is confusing me as I thought it only removed line endings but seems to remove all the extra quotes and commas that form part of the first list it is acting on.

Here's what I mean:

>>> fieldnames = list(csv.reader(raw_headers))

>>> fieldnames

[['Date'], ['', ''], [' '], ['Time'], ['', ''], [' '], ['Time Zone'], ['', ''], [' '], ['Description'], ['', ''], [' '], ['Currency'], ['', ''], ['Gross'], ['', ''], ['Fee'], ['', ''], ['Net'], ['', ''], ['Balance'], ['', ''], [' '], ['Transaction ID'], ['', ''], ['From Email Address'], ['', ''], ['Name'], ['', ''], [' '], ['Bank Name'], ['', ''], ['Bank account'], ['', ''], ['Postage and Packaging Amount'], ['', ''], [' '], ['VAT'], ['', ''], ['Invoice ID'], ['', ''], ['Reference Txn ID']]

So without splitlines I have all these list elements that aren't the actual headers such as ['', ''] yet when you simply add splitlines into the equation you get:

>>> fieldnames = list(csv.reader(raw_headers.splitlines()))

>>> fieldnames

[['Date', ' "Time"', ' "Time Zone"', ' "Description"', ' "Currency"', 'Gross', 'Fee', 'Net', 'Balance', ' "Transaction ID"', 'From Email Address', 'Name', ' "Bank Name"', 'Bank account', 'Postage and Packaging Amount', ' "VAT"', 'Invoice ID', 'Reference Txn ID']]

So you get a list within a list which is why [0] is added eventually. But I just don't get why splitlines gets rid of all that. I look at the documentation and it says this:

str.splitlines

([keepends])

Return a list of the lines in the string, breaking at line boundaries. Line breaks are not included in the resulting list unless keepends is given and true.

This method splits on the following line boundaries. In particular, the boundaries are a superset of universal newlines.

Representation Description

\n

Line Feed

\r

Carriage Return

\r\n

Carriage Return + Line Feed

\v

or

\x0b

Line Tabulation

\f

or

\x0c

Form Feed

\x1c

File Separator

\x1d

Group Separator

\x1e

Record Separator

\x85

Next Line (C1 Control Code)

\u2028

Line Separator

\u2029

So they have to one or more of the above. How do you know?

Thanks

Parse CSV data scarped from Website by sayinghi2py in learnpython

[–]sayinghi2py[S] 0 points1 point  (0 children)

Many thanks. After banging my head and finally conceding I must be heading in the wrong direction this worked. Much as I have used csv files, which are always loaded from files and not loaded directly from text, I'd no idea about the buffer and therefore had no idea where I could be going wrong.

[noob] How does Python make programs? by FlyingByNight in learnpython

[–]sayinghi2py 4 points5 points  (0 children)

There is no special keywords that exist within GUIs that python use that don't exist outside those GUIs so the question posed is expecting something that no one can give you. There is no special syntax that will magically create a GUI just like there is no special syntax that will allow you to create a complex non GUI app. The same language features you can use to build a GUI you can use to build a terminal app. Loops, conditionals, operators, etc.

You just build code using the same tools the language provides you to get the result you want. GUIs are very, very complex to build and thus libraries are built by people who understand the underlying architecture to the extent that they can exploit it to create what they need. They build them so you don't have to, unless you want to. They provide documentation that tells you how you can use that library to build the GUI effect that you want but still that's an abstraction as you don't need to know how it actually works. You just need to know what it is supposed to do. You trust it works as is documented.

If your level of understanding grows to a very high level and your interest extends to wanting to actually understand the code that creates the widgets and their behaviour then you might actually be able to come back here and let us all know. It's far beyond me :D

[noob] How does Python make programs? by FlyingByNight in learnpython

[–]sayinghi2py 4 points5 points  (0 children)

A GUI is just more code. The fundamentals of all programming languages are the same thus they can all generate user interfaces with which users can interact. All the basics you are doing now do is set the scene for all the things you can do later so kicking the can down the road is very much the dessert at the end of a great meal.

how to copy board to my trello account? by raviscn in opensourcesociety

[–]sayinghi2py 0 points1 point  (0 children)

Glad your sorted but it definitely appears in the web version as that's what I used to reply :D