Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

पायथन में मीडिया फाइलों के माध्यम से कैसे स्क्रैप करें

परिचय

वास्तविक दुनिया की कॉर्पोरेट बिजनेस सेटिंग में, अधिकांश डेटा टेक्स्ट या एक्सेल फाइलों में संग्रहीत नहीं हो सकता है। SQL-आधारित रिलेशनल डेटाबेस जैसे Oracle, SQL Server, PostgreSQL, और MySQL व्यापक उपयोग में हैं, और कई वैकल्पिक डेटाबेस काफी लोकप्रिय हो गए हैं।

डेटाबेस का चुनाव आमतौर पर किसी एप्लिकेशन की परफॉरमेंस, डेटा इंटीग्रिटी, और स्केलेबिलिटी आवश्यकताओं पर निर्भर करता है।

कैसे करें

इस उदाहरण में हम देखेंगे कि sqlite3 डेटाबेस कैसे बनाया जाए। sqlite3 पायथन इंस्टॉलेशन के साथ डिफ़ॉल्ट रूप से इंस्टॉल होता है और इसके लिए किसी और इंस्टॉलेशन की आवश्यकता नहीं होती। हम पैंडास भी इंपोर्ट करेंगे।

SQL से डेटा को DataFrame में लोड करना काफी सीधा है, और पैंडास में इस प्रक्रिया को सरल बनाने के लिए कुछ फंक्शन्स हैं।


import sqlite3
import pandas as pd

print(f"Output \n {sqlite3.version}")

आउटपुट: 2.6.0


# कनेक्शन ऑब्जेक्ट
conn = sqlite3.connect("example.db")

# कस्टमर्स डेटा
customers = pd.DataFrame({
    "customerID": ["a1", "b1", "c1", "d1"],
    "firstName": ["Person1", "Person2", "Person3", "Person4"],
    "state": ["VIC", "NSW", "QLD", "WA"]
})
print(f"Output \n *** Customers info -\n {customers}")

# ऑर्डर्स डेटा
orders = pd.DataFrame({
    "customerID": ["a1", "a1", "a1", "d1", "c1", "c1"],
    "productName": ["road bike", "mountain bike", "helmet", "gloves", "road bike", "glasses"]
})
print(f"Output \n *** orders info -\n {orders}")

# डेटाबेस में लिखें
customers.to_sql("customers", con=conn, if_exists="replace", index=False)
orders.to_sql("orders", conn, if_exists="replace", index=False)

# डेटा लाने के लिए SQL फ्रेम करें
q = """
select orders.customerID, customers.firstName, count(*) as productQuantity
from orders
left join customers on orders.customerID = customers.customerID
group by customers.firstName;
"""

# SQL चलाएं
pd.read_sql_query(q, con=conn)

पूरा कोड एक साथ


import sqlite3
import pandas as pd

print(f"Output \n {sqlite3.version}")

conn = sqlite3.connect("example.db")

customers = pd.DataFrame({
    "customerID": ["a1", "b1", "c1", "d1"],
    "firstName": ["Person1", "Person2", "Person3", "Person4"],
    "state": ["VIC", "NSW", "QLD", "WA"]
})
print(f"*** Customers info -\n {customers}")

orders = pd.DataFrame({
    "customerID": ["a1", "a1", "a1", "d1", "c1", "c1"],
    "productName": ["road bike", "mountain bike", "helmet", "gloves", "road bike", "glasses"]
})
print(f"*** orders info -\n {orders}")

customers.to_sql("customers", con=conn, if_exists="replace", index=False)
orders.to_sql("orders", conn, if_exists="replace", index=False)

q = """
select orders.customerID, customers.firstName, count(*) as productQuantity
from orders
left join customers on orders.customerID = customers.customerID
group by customers.firstName;
"""

pd.read_sql_query(q, con=conn)

आउटपुट:


2.6.0
*** Customers info -
  customerID firstName state
0 a1 Person1 VIC
1 b1 Person2 NSW
2 c1 Person3 QLD
3 d1 Person4 WA
*** orders info -
  customerID productName
0 a1 road bike
1 a1 mountain bike
2 a1 helmet
3 d1 gloves
4 c1 road bike
5 c1 glasses
customerID firstName productQuantity
0 a1 Person1 3
1 c1 Person3 2
2 d1 Person4 1
  1. पायथन - फोल्डर में सभी एक्सेल फाइलों को कैसे मर्ज करें पायथन - फोल्डर में सभी एक्सेल फाइलों को कैसे मर्ज करें

    परिचय फोल्डर में सभी एक्सेल फाइलों को मर्ज करने के लिए, Glob मॉड्यूल और append() मेथड का उपयोग करें। उदाहरण फाइलें मान लें कि डेस्कटॉप पर निम्नलिखित एक्सेल फाइलें हैं: Sales1.xlsx Sales2.xlsx नोट - आपको openpyxl और xlrd पैकेज इंस्टॉल करने की आवश्यकता हो सकती है। चरण-दर-चरण कार्यान्वयन 1. पथ स

  1. Python में PDF Files Crack कैसे करें? Python में PDF Files Crack कैसे करें?

    Python में libraries का एक समृद्ध संग्रह है जिनका उपयोग multiple purposes के लिए किया जाता है जैसे applications बनाना और develop करना, web development, scientific computation, software testing, machine learning, और बहुत कुछ। Python का उपयोग information security के संदर्भ में system applications को t

  1. Python का उपयोग करके पावरपॉइंट फाइल्स कैसे बनाएं? Python का उपयोग करके पावरपॉइंट फाइल्स कैसे बनाएं?

    परिचय हम सभी को अपने जीवन में किसी न किसी बिंदु पर पावरपॉइंट प्रेजेंटेशन बनाना पड़ा है। अक्सर हमने Microsoft के पावरपॉइंट या Google Slides का उपयोग किया है। लेकिन क्या होगा यदि आपके पास सदस्यता या इंटरनेट तक पहुंच नहीं है? या क्या होगा यदि आप इसे केवल प्रोग्रामर तरीके से करना चाहते हैं? चिंता न करे