PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label append. Show all posts
Showing posts with label append. Show all posts

Thursday, November 10, 2022

[FIXED] How can i avoid duplication in a proc append even if i run it twice?

 November 10, 2022     append, macros, proc, sas     No comments   

Issue

I can't just delete duplication from my base table because i have a DATETIME column so it will not be considered like duplication.

Exemple : base (after first append) :

+------+----------------------------+------+-----+
| name | date                       |  id  |year |  
+------+----------------------------+------+-----+
|    X |Thursday06july2020 16:21:06 |   303| 2019| 
|    Y |Thursday06july2020 16:21:06 |   91 | 2020|  
+------+----------------------------+------+-----+

after doing some modification on my data table i will have this in :

+------+----------------------------+------+-----+
| name | date                       |  id  |year |  
+------+----------------------------+------+-----+
|    W |Friday07August2020 13:27:15 |  92  | 2019|
|    X |Friday07August2020 13:27:15 |  303 | 2019| 
|    Y |Friday07August2020 13:27:15 |  91  | 2020|  
|    Z |Friday07August2020 13:27:15 |  45  | 2020|
+------+----------------------------+------+-----+

then i want to reexcute the proc append. Is there any method to compare all the columns expect the date column ? I wish that it's clear enough.


Solution

Create a primary key on all of your columns except date in your base table. For example:

data basetable;
    length pk $50.;
    set basetable;
 
    pk = cats(name, id, year);
run;

You can use this to update values and columns within your data.

To continue using proc append, you can create an integrity constraint on pk that prevents duplicates on your base table.

proc datasets lib=mylib nolist;
    modify basetable;
        ic create unique (pk);
quit;

This integrity constraint will be destroyed if you recreate the table.



Answered By - Stu Sztukowski
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, November 4, 2022

[FIXED] How do I append a lambda to a list in python?

 November 04, 2022     append, lambda, python     No comments   

Issue

I am trying to make a program that creates a list of lambda functions of the format y=mx+b, where 'm' and 'b' are predetermined values

My overall goal is to implement a function that

  • Takes a picture
  • Finds the lines on it
  • Extends them across the whole picture in a solid colour

Basically, something like a Hough transforms if you know what that is.

Once I have the lines for a specific image, I can create a lambda function to represent the slope of the line and where it begins. I'm having an issue not being able to append a lambda function to the list.

I have tried this :

if __name__ == "__main__":
  nums = []
  for i in range(10):
    j = lambda x: x + i
    nums.append(j)
  for i in nums:
    print(i(1))

Here is the error I'm getting :

Traceback (most recent call last):
  File "C:/Users/me/.PyCharmCE2018.3/config/scratches/scratch_3.py", line 7, in <module>
    print(i(1))
  File "C:/Users/me/.PyCharmCE2018.3/config/scratches/scratch_3.py", line 4, in <lambda>
    j = (lambda x: x + i)
TypeError: unsupported operand type(s) for +: 'int' and 'function'

Solution

The problem is that the lambdas you create are referring to the current value of i in the active stack frame. When you later reuse i for the second for loop, it is bound to the lambdas in your list. When invoked as i(1), the lambdas are trying to evaluate 1 + i where i is the lambda, so of course you get an error.

Probably what you want is to freeze the value of i at the point at which the lambda is created. You can do this by replacing:

j = lambda x: x + i

with:

j = (lambda y: lambda x: x + y)(i)

This effectively captures the current value of i by binding it to a lambda variable, then immediately applying that lambda, after which the binding remains fixed.



Answered By - Tom Karzes
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, November 2, 2022

[FIXED] How to append multiple files in R

 November 02, 2022     append, csv, file, r     No comments   

Issue

I'm trying to read a list of files and append them into a new file with all the records. I do not intend to change anything in the original files. I've tried couple of methods.

Method 1: This methods creates a new file but at each iteration the previous file gets added again. Because I'm binding the data frame recursively.

files <- list.files(pattern = "\\.csv$")

  #temparary data frame to load the contents on the current file
  temp_df <- data.frame(ModelName = character(), Object = character(),stringsAsFactors = F)

  #reading each file within the range and append them to create one file
  for (i in 1:length(files)){
    #read the file
    currentFile = read.csv(files[i])

    #Append the current file
    temp_df = rbind(temp_df, currentFile)    
  }

  #writing the appended file  
  write.csv(temp_df,"Models_appended.csv",row.names = F,quote = F)

Method 2: I got this method from Rbloggers . This methods won't write to a new file but keeps on modifying the original file.

multmerge = function(){
  filenames= list.files(pattern = "\\.csv$")
  datalist = lapply(filenames, function(x){read.csv(file=x,header=T)})
  Reduce(function(x,y) {merge(x,y)}, temp_df)

}

Can someone advice me on how to achieve my goal?


Solution

it could look like this:

files <- list.files(pattern = "\\.csv$")

DF <-  read.csv(files[1])

#reading each file within the range and append them to create one file
for (f in files[-1]){
  df <- read.csv(f)      # read the file
  DF <- rbind(DF, df)    # append the current file
}
#writing the appended file  
write.csv(DF, "Models_appended.csv", row.names=FALSE, quote=FALSE)

or short:

files <- list.files(pattern = "\\.csv$")

DF <-  read.csv(files[1])
for (f in files[-1]) DF <- rbind(DF, read.csv(f))   
write.csv(DF, "Models_appended.csv", row.names=FALSE, quote=FALSE)


Answered By - jogo
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How do I append text to a file?

 November 02, 2022     append, file, file-io, shell, text     No comments   

Issue

What is the easiest way to append text to a file in Linux?

I had a look at this question, but the accepted answer uses an additional program (sed) I'm sure there should be an easier way with echo or similar.


Solution

cat >> filename
This is text, perhaps pasted in from some other source.
Or else entered at the keyboard, doesn't matter. 
^D

Essentially, you can dump any text you want into the file. CTRL-D sends an end-of-file signal, which terminates input and returns you to the shell.



Answered By - Jon Kiparsky
Answer Checked By - David Goodson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, September 15, 2022

[FIXED] Why is the Output of these Two Lists Different?

 September 15, 2022     append, list, printing, python     No comments   

Issue

import random

words = ['nugget', 'jasmine', 'trolley', 'weight', 'soap']
mywords = []

random.shuffle(words)
mywords.append(random.sample(words, 2))

print("Words: ")
for i in words:
    print(str(i))

print("My Words: ")
for i in mywords:
    print(str(i))

I am working on an example that moves items from one list to another. The example seems to work as intended except for the output of the second list 'mywords', which prints in brackets and not as an unordered list as the list 'words' prints. Any suggestions? My guess is it has something to do with the append function I used.


Solution

The function random.sample() returns a list. The function list.append() takes the item you pass as an arugment, in this case a list, and adds it to the end of your list. If you replace append() with extend() it will add each item returned from random.sample() to the list individually.



Answered By - Nathan Roberts
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, August 13, 2022

[FIXED] Why the "NONE" is getting appended in list?

 August 13, 2022     append, function, list, python, python-3.x     No comments   

Issue

I have made 2 functions: one to generate random values from 0 to 20, and another to check if the value generated by function random matches the value of list. If the value matches then random should start again to produce new value

My code is:

import random
mylist=[6,2,4]
y=int()
def randoom():
    str=random.randint(0,20)
    return str
        
def checklist():
    y=randoom()
    print(y,"Generated value y")    
    if y in mylist:
        print(y,"already exist in list")
        checklist()
    if y not in mylist:
        return y

for b in range(1,10):
    x=checklist()
    print(x," X got the value")
    mylist.append(x)
    print(mylist)

My output is: [6, 2, 4, 5, 12, 7, 16, 13, None, 17, 19, None]

Why is None getting appended?

I tried everything in last 3 days to figure out why None is getting appended in list even when I made sure the function runs again to produce a new value of y if it matches the list.


Solution

Your checklist function can return None if y is in mylist:

if y in mylist:
    print(y,"already exist in list")
    checklist()  # Executes checklist again, but discards the return value

There is no path after this that can return a value, so the function returns None. You can fix this by returning checklist():

if y in mylist:
    print(y,"already exist in list")
    return checklist()  # Executes checklist again, but returns the value


Answered By - TheMikeste1
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, July 23, 2022

[FIXED] How to append JSON fields in sqlalchemy statements

 July 23, 2022     append, json, python, sqlalchemy     No comments   

Issue

How to update JSON field in sqlalchemy appending another json?

    stmt = pg_insert(Users).values(
        userid=user.id,
        pricesjson=[{
            "product1": user.product1,
            "product2": user.product2,
            "product3": user.product3
        }],
        tsins=datetime.now()
    )
    stmtUpsert = stmt.on_conflict_do_update(index_elements=[Users.userid],
                                            set_={'pricesjson': cast({"product1": user.product1,
                                                                      "product2": user.product2,
                                                                      "product3": user.product3
                                                                     } +
                                                                     cast(stmt.excluded.pricesjson, JSONB),
                                                                     JSON)
                                                , 'tsvar': datetime.now()})

In that way i don't receive errors but overwrite json field without append. Thank you ;)


Solution

Solved: After altered the field on table from json to jsonb, that's the working code:

stmtUpsert = stmt.on_conflict_do_update(index_elements=[Users.userid],
                                                  set_={'pricesjson': cast([{"product1": user.product1,
                                                                       "product2": user.product2,
                                                                       "product3": user.product3
                                                                      ], JSONB) + Users.pricesjson
                                                , 'tsvar': datetime.now()})

That's the relative sample query:

insert into users (userid, pricesjson) values('1',  '{"product1": "test1", product2": "test2"}') 
on conflict (userid) 
do update set pricesjson =cast('[{"productX": "testX"}]' as jsonb) || securitiesprices.pricesjson


Answered By - Damiano Dotto
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Friday, July 22, 2022

[FIXED] How to dynamically append tailwindcss classes to each list item in JavaScript

 July 22, 2022     append, arrays, for-loop, javascript, tailwind-css     No comments   

Issue

I am attempting to add Tailwindcss styling classes to list items appended to an HTML document from an array:

const array1 = ['one', 'two', 'three', 'four', 'five']

for (let i = 0; i < array1.length; i++) {
  let li = document.createElement('li')
  document.querySelector('#newList').append(array1[i], li)
}

The querySelector is bound to an UL element in a tailwindcss template:

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.tailwindcss.com"></script>
  </head>
  <body>
    <h1 class="text-3xl font-bold underline">
      My Todo list
    </h1>
    <div>
      <ul id="newList"></ul>
    </div>    
    <script src="app.js"></script>
  </body>
</html>

I want each LI element to have the following tailwindcss classes:

<li href="#" class="block p-6 max-w-sm bg-white rounded-lg border border-gray-200 shadow-md hover:bg-gray-100 dark:bg-gray-800 dark:border-gray-700 dark:hover:bg-gray-700"></li>

How can I configure either the JS or HTML to dynamically append these classes to each LI element from the array?


Solution

If you want to append to a node as child, i think use appendChild() like this:

const array1 = ['one', 'two', 'three', 'four', 'five']

for (let i = 0; i < array1.length; i++) {
  let li = document.createElement('li')
  li.innerHTML = array1[i]
  li.classList.add('block', 'p-6', 'max-w-sm', 'bg-white', 'rounded-lg', 'border', 'border-gray-200', 'shadow-md', 'hover:bg-gray-100', 'dark:bg-gray-800', 'dark:border-gray-700', 'dark:hover:bg-gray-700')
  document.querySelector('#newList').appendChild(li)
}


Answered By - VMT
Answer Checked By - Cary Denson (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Thursday, July 21, 2022

[FIXED] How to append a new line to a csv file in python

 July 21, 2022     append, python, selenium     No comments   

Issue

with open('Price.csv', 'w', newline = '', encoding= 'utf-8') as csvFile:
    csvWriter = csv.writer(csvFile, delimiter=' ')
    csvWriter.writerow("Price")

    for item in items:


        whole_price = item.find_elements(By.XPATH, './/span[@class="a-price-whole"]')
        fraction_price = item.find_elements(By.XPATH, './/span[@class="a-price-fraction"]')

        if whole_price != [] and fraction_price != []:

            price = '.'.join([whole_price[0].text, fraction_price[0].text])
            product_price.append(price)


        else:
            price = 0
   
    csvWriter.writerow(product_price)

driver.quit()

Trying to figure out how to append price to product_price with a new line character at the end.

This has been my outcome and I'm confused why. Do I need to individual print the rows and add a new line. I thought writerow added a new line break already?

P r i c e
41.18 48.56 18.73 48.56 48.56 37.46 37.46 53.22 60.99 32.99 18.73 7.79 32.34 39.99 20.49 7.79 34.90 37.25 56.49 48.56 156.00 42.95 85.00 34.98 60.00 17.98 60.61 95.50 6.59 7.49 87.40 74.00 17.73 52.56 34.99 39.99 170.00 18.73 2.

Solution

writerow without s at the end is for writing all values on list in one row.

And you have all values on one list - so it treats it as single row.

You should rather write row directly when you find price - but price has to be as list.

            price = '.'.join([whole_price[0].text, fraction_price[0].text])
            
            csvWriter.writerow( [price] )

OR you should append price as list with single value

            price = '.'.join([whole_price[0].text, fraction_price[0].text])

            product_price.append( [price] )

and later use writerows with s at the end which write every nested list as separated row

    csvWriter.writerows(product_price)  # with `s` at the end

BTW: When you write header then you should also use list

csvWriter.writerow( ["Price"] )

beause at this moment it treads "Price" as list of chars

csvWriter.writerow( ["P", "r", "i", "c", "e"] )

and it writes space between chars.


EDIT:

# PEP8: `lower_case_names` for variables `csv_file`, `csv_writer`

with open('Price.csv', 'w', newline='', encoding='utf-8') as csv_file:  
    csv_writer = csv.writer(csv_file, delimiter=' ')
    csv_writer.writerow( ["Price"] )

    for item in items:

        whole_price    = item.find_elements(By.XPATH, './/span[@class="a-price-whole"]')
        fraction_price = item.find_elements(By.XPATH, './/span[@class="a-price-fraction"]')

        if whole_price and fraction_price:

            price = '.'.join([whole_price[0].text, fraction_price[0].text])
            csv_writer.writerow( [price] )

PEP 8 -- Style Guide for Python Code



Answered By - furas
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] Why jquery not working on this page but it works on other Normal pages

 July 21, 2022     append, contains, html     No comments   

Issue

I have this jquery that can detect the sentence and add the span on it

https://cognitive-assessment.iwi.unisg.dev/submission/?mode=preview&token=c5e77101c559cd672654248f52d5692a

this is the page where i want to run jquery

you have to login to view the page

Username:ABC paswword:ABC@@99

jQuery(document).ready(function( $ ){
  
$("h5:contains('Medium Overall Feasibility of Use Case')").html(function(_, html) {
   return html.replace(/(Medium Overall Feasibility of Use Case)/g, '<yellow class="yellow">$1</yellow>');
});

});

This is the jQuery that need to run...


Solution

The jQuery snippet you shared works on my side for the page you shared. As a proof of concept, take a look at the following screenshot:

enter image description here

What are the other pages you are referring to ? What exactly doesn't work when you run this code at your side ?

Note: It's always a bad idea to publicly share the credentials of your university account, for security reasons.



Answered By - Nick Papadiochos
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How can i write text on a file in c?

 July 21, 2022     append, c, file, fopen, stdio     No comments   

Issue

I am programming a simple text editor in c and I defined a structure named node and created a linked list named textbuffer. I am trying to create an insert function for the text editor. Here is the code so far:

#include <stdio.h>
#include <string.h>

struct node
{
    char statement[40];
    int next;
};
struct node textbuffer[25];

int free_head;
int inuse_head;

void insert(int line, char* stat)
{
    FILE *file;
    file=fopen("texteditor.txt","w");
    
    if(file!=NULL)
    {
        int i;
        int k;
        
        strcpy(textbuffer[line].statement,stat);
        textbuffer[line].next=line+1;
        fprintf(file,textbuffer[line].statement);
        
        for(i=0;i<=25;i++)
        {
            if(textbuffer[i].statement==NULL)
            {
                free_head=i;
                break;
            }
        }
        
        for(k=0;k<=25;k++)
        {
            if(textbuffer[k].statement!=NULL)
            {
                inuse_head=k;
                break;
            }
        }
    }
    else
    {
        printf("File couldn't found.");
    }
    fclose(file);
}


int main()
{
    insert(1,"Hello World");
    return 0;   
}


The problem is when texteditor.txt file is empty and when i run the code it writes "Hello World" in the file, that's fine but when i run it for the second the, i am expecting it to write "HelloWorldHelloWorld" but it doesn't do anything.it's still staying as "Hello World". How can i solve this?


Solution

You can simply open the file in append mode:

file=fopen("texteditor.txt","a");

More info: https://en.cppreference.com/w/c/io/fopen



Answered By - HARSH MITTAL
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to append xml file attribute with array value using Python?

 July 21, 2022     append, arrays, python, xml     No comments   

Issue

I have an xml file.

<?xml version='1.0' encoding='utf-8'?>
<systemdata>
     <process>
          <number code="hsfg" class="hgdgf" tool="gagfa">
               <value type="string" />
               <value type="None" />
          </number>
          <!-- ID -->
          <id code="hsfg" class="gfdg" tool="fadg">
               <value type="string" />
               <value type="None" />
          </id>
     </process>
</systemdata>

I would like to append this array to my XML file above.

memorys = []
for mem in wmiquery.Win32_PhysicalMemory():
    sysmem = {}
    sysmem['location'] = mem.DeviceLocator
    sysmem['banklabel'] = mem.BankLabel
    sysmem['cap'] = mem.Capacity
    memorys.append(sysmem)
for m in memorys:
    print(m)

The value of m is like this:

{'location': 'DIMM1', 'banklabel': 'ChannelA', 'cap': '8589934592'}
{'location': 'DIMM2', 'banklabel': 'ChannelA', 'cap': '8589934592'}

I would like to append these array to my XML. So my expectation based on the array above, I will append 2 new element. If the array has 4 then create new 4 element. Here is my expectation output:

<?xml version='1.0' encoding='utf-8'?>
<systemdata>
     <process>
          <number code="hsfg" class="hgdgf" tool="gagfa">
               <value type="string" />
               <value type="None" />
          </number>
          <!-- ID -->
          <id code="hsfg" class="gfdg" tool="fadg">
               <value type="string" />
               <value type="None" />
          </id>
     </process>
     <!-- memory -->
     <unitmemory>
          <!-- data -->
          <module location="DIMM1">
               <banklabel tool="banklabel">
                    <value type="string">ChannelA</value>
               </banklabel>
               <cap tool="cap">
                    <value type="string">8589934592</value>
               </cap>
          </module>             
          <module location="DIMM2">
               <banklabel tool="banklabel">
                    <value type="string">ChannelA</value>
               </banklabel>
               <cap tool="cap">
                    <value type="string">8589934592</value>
               </cap>
          </module>
     </unitmemory>
</systemdata>

Anyone can give me any idea?


Solution

Jack's approach seem easier, here is another way including the comments you need:

  1. read your file using a parser to keep comments
  2. insert comments using ET.Comment()
  3. loop through list of dictionaries and add sub-elements to xml
  4. use toprettyxml() to get convert xml to formatted string, but this adds unnecessary new-lines
  5. remove the extra newlines using list comprehension and strip()
  6. add encoding info to xml declaration
  7. write to original file
import xml.etree.ElementTree as ET
import xml.dom.minidom

memorys = [
    {'location': 'DIMM1', 'banklabel': 'ChannelA', 'cap': '100'},
    {'location': 'DIMM2', 'banklabel': 'ChannelB', 'cap': '200'}
]

m_encoding = 'utf-8'
parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))

tree = ET.parse('sampleXml.xml', parser=parser)
root = tree.getroot()

root.insert(1, ET.Comment('memory'))

unit_mem = ET.SubElement(root, 'unitmemory')
unit_mem.insert(0, ET.Comment('data'))

for mem in memorys:
    m_module = ET.SubElement(unit_mem, 'module ')
    m_module.set('location', mem['location'])

    b_label = ET.SubElement(m_module, 'banklabel  ')
    m_cap = ET.SubElement(m_module, 'cap ')
    b_value = ET.SubElement(b_label, 'value ')
    c_value = ET.SubElement(m_cap, 'value ')

    m_module.set('location', mem['location'])
    b_label.set('tool', 'banklabel')
    m_cap.set('tool', 'cap')
    b_value.set('type', 'string')
    c_value.set('type', 'string')

    b_value.text = mem['banklabel']
    c_value.text = mem['cap']

dom = xml.dom.minidom.parseString(ET.tostring(root))
xml_string = dom.toprettyxml()
xml_string = '\n'.join([line for line in xml_string.splitlines() if line.strip()])
part1, part2 = xml_string.split('?>')

with open("sampleXml.xml", 'w') as xfile:
    xfile.write(part1 + 'encoding=\"{}\"?>\n'.format(m_encoding) + part2)
    xfile.close()

My input

{'location': 'DIMM1', 'banklabel': 'ChannelA', 'cap': '100'}
{'location': 'DIMM2', 'banklabel': 'ChannelB', 'cap': '200'}

My output

<?xml version="1.0" encoding="utf-8"?>

<systemdata>
    <process>
        <number code="hsfg" class="hgdgf" tool="gagfa">
            <value type="string"/>
            <value type="None"/>
        </number>
        <!-- ID -->
        <id code="hsfg" class="gfdg" tool="fadg">
            <value type="string"/>
            <value type="None"/>
        </id>
    </process>
    <!--memory-->
    <unitmemory>
        <!--data-->
        <module location="DIMM1">
            <banklabel tool="banklabel">
                <value type="string">ChannelA</value>
            </banklabel>
            <cap tool="cap">
                <value type="string">100</value>
            </cap>
        </module>
        <module location="DIMM2">
            <banklabel tool="banklabel">
                <value type="string">ChannelB</value>
            </banklabel>
            <cap tool="cap">
                <value type="string">200</value>
            </cap>
        </module>
    </unitmemory>
</systemdata>


Answered By - Abhi_J
Answer Checked By - Marie Seifert (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to append a list to another list in a python for loop when each element of the existing list is being changed inside the for loop?

 July 21, 2022     append, extend, list, numpy, python     No comments   

Issue

I am trying to write a code which will create modified lists in a for loop every time and append it to a new list. For this, I tried creating an empty numpy array with two elements which are to be modified inside the for loop iterating over dictionary elements.

r2 = []
r1 = np.empty(2, dtype = object)
r1 = r1.tolist()
r_1 = {'G':32,'H':3}
for i in r_1:
    r1[0] = i
    r1[1] = i + 'I'
    print(r1)    
    r2.append(r1)
print(r2)

which gives me r2 as

[['H', 'HI'], ['H', 'HI']]

The r1 values in each iteration are as expected. However, I am expecting r2 to be

[['G', 'GI'], ['H', 'HI']]

I don't know why append() is not working properly. I also tried the same by doing extend() but the same thing happens on doing extend([r1]) whereas on doing extend(r1) it gives me

['G', 'GI','H', 'HI']

Am I doing it wrong or the code is interpreting something else?


Solution

When you append r1 twice to r2 it essentially makes r2 a list of [r1, r1] not the contents of r1 when it was appended, so when r1 is changed before the second append, the first element in r2 which is a reference to r1 is also changed.

One solution is to not use r1 at all and just append the contents directly:

r2 = []
r_1 = {'G':32,'H':3}
for i in r_1:
    r2.append([i, i+"I"])
print(r2)

A second solution is to append a copy of r1 to avoid the two elements having the same reference:

r2 = []
r_1 = {'G':32,'H':3}
r1 = [None, None]
for i in r_1:
    r1[0] = i
    r1[1] = i + "I"
    r2.append(r1.copy())
print(r2)


Answered By - Telan
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to append character to specific part of item in list - Python

 July 21, 2022     append, character, list, python     No comments   

Issue

I have the following list Novar:

["'population_3000|road_class_3_3000'", "'population_3000|road_class_3_3000|trafBuf25'", "'population_3000|road_class_3_3000|trafBuf25|population_1000'"]

How can I append a character (e.g. $) to a specific part of the items in the list, for instance to road_class_3_3000, so that the upgraded list becomes:

["'population_3000|road_class_3_3000$'", "'population_3000|road_class_3_3000$|trafBuf25'", "'population_3000|road_class_3_3000$|trafBuf25|population_1000'"]

Most similar questions on Stack Overflow seem to focus on manipulating the item itself, rather than a part of the item, e.g. here and here.

Therefore, applying the following code:

    if (item == "road_class_3_3000"):
        item.append("$")

Would be of no use since road_class_3_3000 is part of the items "'population_3000|road_class_3_3000'", "'population_3000|road_class_3_3000|trafBuf25'" and "'population_3000|road_class_3_3000|trafBuf25|population_1000'"


Solution

You might harness re module (part of standard library) for this task following way

import re
novar = ["'population_3000|road_class_3_3000'", "'population_3000|road_class_3_3000|trafBuf25'", "'population_3000|road_class_3_3000|trafBuf25|population_1000'"]
novar2 = [re.sub(r'(?<=road_class_3_3000)', '$', i) for i in novar]
print(novar2)

output

["'population_3000|road_class_3_3000$'", "'population_3000|road_class_3_3000$|trafBuf25'", "'population_3000|road_class_3_3000$|trafBuf25|population_1000'"]

Feature I used is called positive lookbehind, it is kind of zero-length assertion. I look for place after road_class_3_3000 of zer-length, which I then replace using $ character.



Answered By - Daweo
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to write a.dbf file

 July 21, 2022     append, dbf, merge, r     No comments   

Issue

I'm encountering issue using the below script. All are working fine except for the final line which results to the error below.

enter image description here

# read dbf
library(foreign)
setwd("C:/Users/JGGliban/Desktop/Work/ADMIN/Other Stream/PH")

# Combine multiple dbf files
# library('tidyverse')
# List all files ending with dbf in directory
dbf_files <- list.files(pattern = c("*.DBF","*.dbf"), full.names = TRUE)
# Read each dbf file into a list
dbf_list <- lapply(dbf_files, read.dbf, as.is = FALSE)
# Concatenate the data in each dbf file into one combined data frame
data <- do.call(rbind, dbf_list)
# Write dbf file - max-nchar is the maimum number of characters allowed in a character field. After the max, it will be truncated.
x <- write.dbf(data, file, factor2char = TRUE, max_nchar = 254)

Solution

Code modified to:

x <- write.dbf(data, "file.dbf", factor2char = TRUE, max_nchar = 254)


Answered By - Janine
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to replicate DataFrame.append of pandas.Series by using pandas.concat?

 July 21, 2022     append, concatenation, dataframe, pandas, python     No comments   

Issue

I have a code in which I create a pivot table, I apply a function to each column, and I append the result as a row to the dataframe. The code behaves like this:

>>> import pandas as pd
>>> df = pd.DataFrame({
... 'id':['A','B','C','A','B','C'],
... 'par':['x','x','x','y','y','y'],
... 'val':[1,2,3,4,5,6]})
>>> pv = df.pivot_table(index="id", columns='par', values="val", aggfunc="sum")
>>> avg = pv.mean().rename('MyFunc')
>>> pv.append(avg)
par       x    y
id
A       1.0  4.0
B       2.0  5.0
C       3.0  6.0
MyFunc  2.0  5.0

Which is the output I expect. However, documentation of pandas.DataFrame.append says:

Deprecated since version 1.4.0: Use concat() instead. For further details see Deprecated DataFrame.append and Series.append

When I try to replicate this with pandas.concat I get a different output

>>> pd.concat([pv, avg])
     x    y    0
A  1.0  4.0  NaN
B  2.0  5.0  NaN
C  3.0  6.0  NaN
x  NaN  NaN  2.0
y  NaN  NaN  5.0
>>> pd.concat([pv, avg], axis=1)
     x    y  MyFunc
A  1.0  4.0     NaN
B  2.0  5.0     NaN
C  3.0  6.0     NaN
x  NaN  NaN     2.0
y  NaN  NaN     5.0

Is it possible to achieve the same result of append by using concat?


Solution

If append Series is possible use DataFrame.loc:

pv.loc['MyFunc'] = pv.mean()
print (pv)
par       x    y
id              
A       1.0  4.0
B       2.0  5.0
C       3.0  6.0
MyFunc  2.0  5.0

Or concat with convert Series to one row DataFrame by Series.to_frame and transpose:

print (avg.to_frame().T)
par       x    y
MyFunc  2.0  5.0

df = pd.concat([pv, avg.to_frame().T])
print (df)
par       x    y
A       1.0  4.0
B       2.0  5.0
C       3.0  6.0
MyFunc  2.0  5.0


Answered By - jezrael
Answer Checked By - Senaida (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to update value of key in nested dictionary?

 July 21, 2022     append, dictionary, python, python-3.x     No comments   

Issue

items = [{'id': 1, 'language': 'English', 'name': 'Sarah', 'description': 'Blah blah'}, {'id': 2, 'language': 'English', 'name': 'Jessica', 'description': 'More blah'}]

d = {}
for item in items:

    language = item['language']
    id = item['id']
    name = item['name']
    description = item['description']

    d[language][id] = {'name': name, 'description': description}

print(d)

I'm expecting to see in output:

{'English': {1:{'name': 'Sarah', 'description': 'Blah blah'}, 2:{'name': 'Jessica', 'description': 'More blah'}}}

But unfortunately I'm getting KeyError: Traceback: KeyError

So, the question is how to update/append value in nested dictionary? What I'm doing wrong?


Solution

d does not contain d["English"] which you try to create with d[language][id] = {'name': name, 'description': description} - hence the error.


You cannot create intermediate dictionaries "on the fly" if they do not exist - either check if they already exist and if not create them - or use dict.setdefault(key,default) to create the entry if it does not yet exist:

items = [{'id': 1, 'language': 'English', 'name': 'Sarah', 'description': 'Blah blah'},
         {'id': 2, 'language': 'English', 'name': 'Jessica', 'description': 'More blah'}]

d = {}
for item in items:
   
    language = item['language']
    idd = item['id']
    name = item['name']
    description = item['description']

    d.setdefault(language,{})[idd] = {'name': name, 'description': description}

print(d)

Output:

{'English': {1: {'name': 'Sarah', 'description': 'Blah blah'}, 
             2: {'name': 'Jessica', 'description': 'More blah'}}}

You can use collections.defaultdict as well if you come into performance problems using setdefault - which is slightly less fast.


Related: Use cases for the 'setdefault' dict method



Answered By - Patrick Artner
Answer Checked By - Timothy Miller (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] What is the easiest way to append data to Pandas DataFrame?

 July 21, 2022     append, beautifulsoup, dataframe, pandas, python     No comments   

Issue

I am trying to append scraped data to a dataframe:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup
import requests
import csv
url="https://en.wikipedia.org/wiki/List_of_German_football_champions"
page=requests.get(url).content
soup=BeautifulSoup(page,"html.parser")

seasons=[]
first_places=[]
runner_ups=[]
third_places=[]
top_scorrers=[]

tbody=soup.find_all("tbody")[7]
trs=tbody.find_all("tr")
for tr in trs:
    season = tr.find_all("a")[0].text
    first_place = tr.find_all("a")[1].text
    runner_up = tr.find_all("a")[2].text
    third_place = tr.find_all("a")[3].text
    top_scorer = tr.find_all("a")[4].text
    seasons.append(season)
    first_places.append(first_place)
    runner_ups.append(runner_up)
    third_places.append(third_place)
    top_scorrers.append(top_scorer)

tuples=list(zip(seasons,first_places,runner_ups,third_places,top_scorrers))
df=pd.DataFrame(tuples,columns=["Season","FirstPlace","RunnerUp","ThirdPlace","TopScorrer"])
df

enter image description here

Is there an easier way to append data directly to an empty dataframe without creating lists and then zipping them?


Solution

While still using pandas "simplest" way to create your DataFrame is going with pandas.read_html():

import pandas as pd

df = pd.read_html('https://en.wikipedia.org/wiki/List_of_German_football_champions')[7]

To simply rename the columns and get rid of the [7]:

df.columns = ['Season', 'Champions', 'Runners-up', 'Third place',
   'Top scorer(s)', 'Goals']

Output:

Season Champions Runners-up Third place Top scorer(s) Goals
0 1963–64 1. FC Köln (2) Meidericher SV Eintracht Frankfurt Uwe Seeler 30
1 1964–65 Werder Bremen (1) 1. FC Köln Borussia Dortmund Rudi Brunnenmeier 24
2 1965–66 TSV 1860 Munich (1) Borussia Dortmund Bayern Munich Friedhelm Konietzka 26
3 1966–67 Eintracht Braunschweig (1) TSV 1860 Munich Borussia Dortmund Lothar Emmerich, Gerd Müller 28
4 1967–68 1. FC Nürnberg (9) Werder Bremen Borussia Mönchengladbach Hannes Löhr 27

...


An alternativ to avoid all these lists, get cleaner in process and using BeautifulSoup directly is to create more structured data - A single list of dicts:

data = []

for tr in soup.select('table:nth-of-type(8) tr:not(:has(th))'):
    data.append({
        'season':tr.find_all("a")[0].text,
        'first_place': tr.find_all("a")[1].text,
        'runner_up': tr.find_all("a")[2].text,
        'third_place': tr.find_all("a")[3].text,
        'top_scorer': tr.find_all("a")[4].text,
    })

pd.DataFrame(data)
Example
import pandas as pd
from bs4 import BeautifulSoup
import requests

url="https://en.wikipedia.org/wiki/List_of_German_football_champions"
page=requests.get(url).content
soup=BeautifulSoup(page,"html.parser")

data = []

for tr in soup.select('table:nth-of-type(8) tr:not(:has(th))'):
    data.append({
        'season':tr.find_all("a")[0].text,
        'first_place': tr.find_all("a")[1].text,
        'runner_up': tr.find_all("a")[2].text,
        'third_place': tr.find_all("a")[3].text,
        'top_scorer': tr.find_all("a")[4].text,
    })

pd.DataFrame(data)


Answered By - HedgeHog
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How do I append to a list when the name of the list lies within a string?

 July 21, 2022     append, python     No comments   

Issue

I've got multiple lists:

    list_1 = []
    list_2 = []
    list_3 = []

And I've got the following string, which depending on the circumstances will correspond to one of the lists above:

    x = 'list_2'

How do I go about appending to the list by using the string? Is there any way I can do something along the lines of:

    'value of x'.append("Whatever")

Solution

Use a dictionary of lists. Do not use eval if you can avoid it (it is dangerous), and yours is a classic case where you can avoid it.

dct = {'list_1': [],
       'list_2': [],
       'list_3': [],}

x = 'list_2'
print(dct[x])
# []

SEE ALSO:

  • How do I create variable variables?
  • How can you dynamically create variables?
  • Using a string variable as a variable name


Answered By - Timur Shtatland
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] How to add whitespace when appending values extracted from txt file in python

 July 21, 2022     append, file-read, python, split, whitespace     No comments   

Issue

In a text file I have two columns of value (x and y) The file was read and it was split using space and appended in x and y. but while appending x values, it is continuous without any space. How to add space or to make it appear in new line for every appended value.

import numpy as np 

x = []
y = []

for line in open('IV.txt', 'r'):
    lines = [i for i in line.split(" ")]
    x.append(lines[0])
    y.append(lines[1])

f1 = open('xdata.txt','a')
f1.writelines(x)
f1.close()

f2 = open('ydata.txt','a')
f2.writelines(y)
f2.close()

the input data is

0 3
0.2 3.4
0.4 3.6
0.6 3.8
0.8 4.2
1 5.3
1.2 5.5
1.4 5.8
1.6 6.0
1.8 6.4
2 6.8
2.2 7.1
2.4 7.8
2.6 8.2
2.8 8.4
3 8.8

So the output of x appears as 00.20.40.60.811.21.41.61.822.22.42.62.83


Solution

f.writelines(" ".join(x))
f.writelines("\n".join(x))

You can put space or newline like this.

ADD

you can get x, y array from input data like this.

x = list()
y = list()
with open("data.txt", "r") as f:
    for line in f.readlines():
        split_line = line.split()
        x.append(split_line[0])
        x.append(split_line[1])


Answered By - Lazyer
Answer Checked By - Clifford M. (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing