Create an account


Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 20,145
» Latest member: Benbeckman81
» Forum threads: 22,055
» Forum posts: 22,926

Full Statistics

Online Users
There are currently 873 online users.
» 0 Member(s) | 867 Guest(s)
Applebot, Baidu, Bing, DuckDuckGo, Google, Yandex

 
  [Tut] How to Convert a List of Objects to a CSV File in Python [5 Ways]
Posted by: xSicKxBot - 07-30-2022, 10:14 PM - Forum: Python - No Replies

How to Convert a List of Objects to a CSV File in Python [5 Ways]

5/5 – (1 vote)

? Question: How to convert a list of custom objects to a csv file?

Example: Given is a list of custom objects of, say, type Employee that holds the name, job description, and income like so:

salary = [Employee('Alice', 'Data Scientist', 122000), Employee('Bob', 'Engineer', 77000), Employee('Ann', 'Manager', 119000)]

Your goal is to write the content of the list of objects into a comma-separated-values (CSV) file format.

Your output file should look like this:

# my_file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

Solution: There are four simple ways to convert a list of lists to a CSV file in Python.

  1. CSV: Import the csv module in Python, create a csv writer object, and find a list lst of elements representing each object as a row, that is then written into the CSV using writer.writerow(lst).
  2. Pandas: Import the pandas library, convert each object to a list to obtain a list of lists, create a Pandas DataFrame out of the list of lists, and write the DataFrame to a file using the DataFrame method DataFrame.to_csv('file.csv').
  3. NumPy: Import the NumPy library, convert each object to a list to obtain a list of lists, create a NumPy array, and write the output to a CSV file using the numpy.savetxt('file.csv', array, delimiter=',') method.
  4. Python: Use a pure Python implementation that doesn’t require any library by using the Python file I/O functionality.

⭐ Finxter Favorite: My preference is Method 4 (Vanilla Python) because it’s simplest to use, efficient, and most robust for different input types (numerical or textual) and doesn’t require external dependencies and data wrangling.

Method 1: Python’s CSV Module


You can convert a list of lists to a CSV file in Python easily—by using the csv library. This is the most customizable of all four methods.

class Employee(object): def __init__(self, name, description, salary): self.name = name self.description = description self.salary = salary employees = [Employee('Alice', 'Data Scientist', 122000), Employee('Bob', 'Engineer', 77000), Employee('Ann', 'Manager', 119000)] # Method 1
import csv
with open('my_file.csv', 'w', newline='') as f: writer = csv.writer(f) for x in employees: writer.writerow([x.name, x.description, x.salary])

Output:

# my_file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

In the code, you first open the file using Python’s standard open() command. Now, you can write content to the file object f.

Next, you pass this file object to the constructor of the CSV writer that implements some additional helper method—and effectively wraps the file object providing you with new CSV-specific functionality such as the writerow() method.

You now iterate over the objects and convert each object to a list.

The list representing one row is then passed in the writerow() method of the CSV writer. This takes care of converting the list of objects to a CSV format.

You can customize the CSV writer in its constructor (e.g., by modifying the delimiter from a comma ',' to a whitespace ' ' character). Have a look at the specification to learn about advanced modifications.

Method 2: Pandas DataFrame to_csv()


This method converts a list of objects to a CSV file in two steps:

List of Lists to CSV

You can convert a list of lists to a Pandas DataFrame that provides you with powerful capabilities such as the to_csv() method.

This is a super simple approach that avoids importing yet another library (I use Pandas in many Python projects anyways).

class Employee(object): def __init__(self, name, description, salary): self.name = name self.description = description self.salary = salary employees = [Employee('Alice', 'Data Scientist', 122000), Employee('Bob', 'Engineer', 77000), Employee('Ann', 'Manager', 119000)] # Method 2
import pandas as pd # Step 1: Convert list of objects to list of lists
lst = [[x.name, x.description, x.salary] for x in employees] # Step 2: Convert list of lists to CSV
df = pd.DataFrame(lst)
df.to_csv('my_file.csv', index=False, header=False)

Output:

# my_file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

Code Main Steps:

  1. lst = [[x.name, x.description, x.salary] for x in employees]
  2. df = pd.DataFrame(lst)
  3. df.to_csv('my_file.csv', index=False, header=False)

You convert a list of objects to a CSV file in three main steps.

  1. First, convert the list of objects to a list of lists by using list comprehension to iterate over each object and convert each object to an inner list using your custom expression.
  2. Second, create a Pandas DataFrame, Python’s default representation of tabular data.
  3. Third, the DataFrame is a very powerful data structure that allows you to perform various methods. One of those is the to_csv() method that allows you to write its contents into a CSV file.

You set the index and header arguments of the to_csv() method to False because Pandas, per default, adds integer row and column indices 0, 1, 2, ….

Think of them as the row and column indices in your Excel spreadsheet. You don’t want them to appear in the CSV file so you set the arguments to False.

If you want to customize the CSV output, you’ve got a lot of special arguments to play with. Check out this article for a comprehensive list of all arguments.


? Related article: Pandas Cheat Sheets to Pin to Your Wall

Method 3: NumPy savetext()


NumPy is at the core of Python’s data science and machine learning functionality. Even Pandas uses NumPy arrays to implement critical functionality.

You can convert a list of objects to a CSV file by first converting it to a list of lists which is then converted to a NumPy array, and then using NumPy’s savetext() function by passing the NumPy array as an argument.

This method is best if you can represent the numerical data only—otherwise, it’ll lead to complicated data type conversions which are not recommended.

class Employee(object): def __init__(self, name, description, salary): self.name = name self.description = description self.salary = salary employees = [Employee('Alice', 'Data Scientist', 122000), Employee('Bob', 'Engineer', 77000), Employee('Ann', 'Manager', 119000)] # Method 3
import numpy as np # Convert list of objects to list of lists
lst = [[hash(x.name), hash(x.description), x.salary] for x in employees] # Convert list of lists to NumPy array
a = np.array(lst) # Convert array to CSV
np.savetxt('my_file.csv', a, delimiter=',')

In the code, we use the hash() function to obtain a numerical value for the string attributes name and description of the Employee class.

Output:

# my_file.csv
-8.655249391637094400e+18,-4.821993523891147776e+18,1.220000000000000000e+05 7.826671284149683200e+18,-7.040934892515148800e+18,7.700000000000000000e+04 3.577554885237667328e+18,1.887669837421876992e+18,1.190000000000000000e+05

The output doesn’t look pretty: it stores the values as floats. But no worries, you can reformat the output using the format argument fmt of the savetxt() method (more here). However, I’d recommend you stick to method 2 (Pandas) to avoid unnecessary complexity in your code.

Method 4: Pure Python Without External Dependencies


If you don’t want to import any library and still convert a list of objects into a CSV file, you can use standard Python implementation as well: it’s not complicated but very efficient.

The idea is simple, iterate over the list of object and write a comma-separated representation of each object into the CSV file using a combination of the built-in open() function to create a file object and the file.write() method to write each row.

This method is best if you won’t or cannot use external dependencies.

class Employee(object): def __init__(self, name, description, salary): self.name = name self.description = description self.salary = salary employees = [Employee('Alice', 'Data Scientist', 122000), Employee('Bob', 'Engineer', 77000), Employee('Ann', 'Manager', 119000)] # Method 4
with open('my_file.csv', 'w') as f: for x in employees: f.write(f'{x.name},{x.description},{x.salary}\n')

Output:

# my_file.csv
Alice,Data Scientist,122000,
Bob,Engineer,77000,
Ann,Manager,119000,

In the code, you first open the file object f. Then you iterate over each object and write a custom comma-separated string representation of this object to the file using the file.write() method.

We use Python’s f-string functionality to do that in a concise way. At the end of each row, you place the newline character '\n'.

Method 5 – Bonus: Python One-Liner


The previous method is a one-linerized variant of Method 4. If you’re part of the Finxter community, you know how I love one-liners. ?

# Method 5
open('my_file.csv', 'w').writelines([f'{x.name},{x.description},{x.salary}\n' for x in employees])

Concise, isn’t it? The output is the same as before.

If you’re interested in the art of crafting beautiful one-liners, check out my book on the topic!

Python One-Liners Book: Master the Single Line First!


Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!



https://www.sickgaming.net/blog/2022/07/...on-5-ways/

Print this item

  (Indie Deal) Graffiti Rebel 4 Bundle, THQ Racing Deals, Bandai Giveaways
Posted by: xSicKxBot - 07-30-2022, 10:14 PM - Forum: Deals or Specials - No Replies

Graffiti Rebel 4 Bundle, THQ Racing Deals, Bandai Giveaways

Graffiti Rebel 4 Bundle | 5 Steam Games | 94% OFF
[www.indiegala.com]
Turnip Boy Commits Tax Evasion, Lila's Sky Ark, Nira, Blue Fire, REZ PLZ are the fantastic indie Steam games you must discover with the help of the fourth Graffiti Rebel Bundle.

Solo Deal: Sifu
[www.indiegala.com]
https://www.youtube.com/watch?v=SK89ZRPJXOE
Bandai Giveaways ending soon
[www.indiegala.com]
Racing Deals & more
[www.indiegala.com]

Happy Hour: Secret Desires Bundle
[www.indiegala.com]


https://steamcommunity.com/groups/indieg...5612572189

Print this item

  (Free Game Key) Lawn Mowing Simulator - Free Epic Games Game
Posted by: xSicKxBot - 07-30-2022, 10:14 PM - Forum: Deals or Specials - No Replies

Lawn Mowing Simulator - Free Epic Games Game

Visit the store page and add the games to your account:

Lawn Mowing Simulator[store.epicgames.com]

The games are free to keep until August 04 2022 - 15:00 UTC.

Next week's freebie:
Unrailed!

We are welcoming everyone to join our discord[discord.gg]. We are more active there on finding giveaways, small or large, and there are daily raffles you can participate.

?GrabFreeGames.com ?Twitter ?Steam Curator ?Facebook[fb.me]?Discord[discord.gg]
❤️Support us: ✔️HumbleBundle Partner[www.humblebundle.com] Epic Tag: GrabFreeGames


https://steamcommunity.com/groups/GrabFr...5240564822

Print this item

  PC - Warriors Orochi 3 Ultimate
Posted by: xSicKxBot - 07-30-2022, 10:14 PM - Forum: New Game Releases - No Replies

Warriors Orochi 3 Ultimate



Fans will experience WARRIORS OROCHI 3 in a brand new light as they enter the fray with the implementation of new generation console features, new storylines and scenarios.

Publisher: Koei Tecmo Games

Release Date: Jul 12, 2022




https://www.metacritic.com/game/pc/warri...3-ultimate

Print this item

  [Tut] Top 8 Profitable Python Packages to Learn in 2023
Posted by: xSicKxBot - 07-27-2022, 10:25 AM - Forum: Python - No Replies

Top 8 Profitable Python Packages to Learn in 2023

5/5 – (1 vote)

Are you interested in Python but you don’t know which Python library is most attractive from a career point of view?

Well, you should focus on the library you’re most excited about.

But if you’re generally open because you have multiple passions, it would be reasonable to also consider annual and hourly income.

These are the most profitable Python libraries, frameworks, modules, or packages:


Python Library (Dev) Annual Income (USD) Hourly Income (USD)
Python Developer $82,000 $55
Keras Developer $95,000 $63
Django Developer $117,000 $78
Flask Developer $103,000 $69
NumPy Developer $105,000 $70
Pandas Developer $87,000 $58
TensorFlow Developer $148,000 $99
PyTorch Developer $109,000 $73
Table: Annual and Hourly Income of a developer focusing on different Python libraries/frameworks/packages/modules.

What is the most profitable Python library?

The most profitable Python library is TensorFlow. TensorFlow developers make $148,000 per year on average (US) which roughly translates to $99 per hour assuming an annual workload of 1500 hours.

Let’s dive into each Python library from the table, one by one.

#0 – General Python Developer


A Python developer is a programmer who creates software in the Python programming language. Python developers are often involved in data science, web development, and machine learning applications.

? A Python developer earns $65,000 (entry-level), $82,000 (mid-level), or $114,000 (experienced) per year in the US according to Indeed. (source)

Do you want to become a Python Developer? Here’s a step-by-step learning path I’d propose to get started with Python:

You can find many courses on the Finxter Computer Science Academy (flatrate model).

? Learn More: Read more about this specific Python library career path in our in-depth Finxter article.

#1 – Keras


Let’s have a look at the definition from the official Keras website:

“Keras is an API designed for human beings, not machines. Keras follows best practices for reducing cognitive load: it offers consistent & simple APIs, it minimizes the number of user actions required for common use cases, and it provides clear & actionable error messages. It also has extensive documentation and developer guides.”

A Keras Developer developer creates, edits, analyzes, debugs, and supervises the development of software written in the Keras deep learning framework. Keras developers create machine learning apps using deep learning.

? The average annual income of a Keras Developer in the United States is $95,000 per year, according to PayScale (source). Top earners make $156,000 and more in the US!

Do you want to become a Keras Developer? Here’s a step-by-step learning path I’d propose to get started with Keras:

? Learn More: Read more about this specific Python library career path in our in-depth Finxter article.

#2 – Django


What is Django? Let’s have a look at the definition from the official website (highlights by me):

“Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.”

A Django Developer developer creates, edits, analyzes, debugs, and supervises the development of software written in the Python programming language using the Django web development framework. You need to have good Python, HTML, and CSS skills.

? The average annual income of a Django Developer in the United States is between $101,000 (25th percentile) and $137,000 (75th percentile) with an average of $117,000 per year according to Ziprecruiter (source) and $90,000 per year according to PayScale (source). Top earners make $150,000 and more in the US!

Do you want to become a Django Developer? Here’s a step-by-step learning path I’d propose to get started with Django:

? Learn More: Read more about this specific Python library career path in our in-depth Finxter article.

#3 – Flask


A Flask Developer developer creates, edits, analyzes, debugs, and supervises the development of software written in the Flask programming language. You should have a basic understanding of web technologies such as HTML, CSS, JavaScript, and of course Python.


Let’s have a look at the definition from the Flask wiki page (highlights by me):

“Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries.

It has no database abstraction layer, form validation, or any other components where pre-existing third-party libraries provide common functions.

However, Flask supports extensions that can add application features as if they were implemented in Flask itself. Extensions exist for object-relational mappers, form validation, upload handling, various open authentication technologies and several common framework related tools.”


? The average annual income of a Flask Developer in the United States is between $79,000 (25th percentile) and $123,000 (75th percentile) with an average of $103,000 per year according to Ziprecruiter (source). Top earners make $151,000 and more in the US!

Do you want to become a Flask Developer? Here’s a step-by-step learning path I’d propose to get started with Flask:

? Learn More: Read more about this specific Python library career path in our in-depth Finxter article.

#4 – NumPy


Let’s have a look at the definition from the official NumPy website:

“Nearly every scientist working in Python draws on the power of NumPy. NumPy brings the computational power of languages like C and Fortran to Python, a language much easier to learn and use. With this power comes simplicity: a solution in NumPy is often clear and elegant.”

Here’s where NumPy is used in practice:

source

? The average annual income of a NumPy Developer in the United States is $105,000 per year according to PayScale (source). Top earners make $149,000 and more in the US!

Do you want to become a NumPy Developer? Here’s a step-by-step learning path I’d propose to get started with NumPy:

? Learn More: Read more about this specific Python library career path in our in-depth Finxter article.

#5 – Pandas


What is pandas? Let’s have a look at the definition from the official Pandas website:

“pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool, built on top of the Python programming language.”

You may also want to check out our Pandas resources on the Finxter blog:

? The average annual income of a Pandas Developer in the United States is $87,000 per year according to Ziprecruiter (source). Top earners make $125,000 and more in the US!

Do you want to become a Pandas Developer? Here’s a step-by-step learning path I’d propose to get started with Pandas:

? Learn More: Read more about this specific Python library career path in our in-depth Finxter article.

#6 – TensorFlow


A TensorFlow Developer creates, edits, analyzes, debugs, and supervises the development of code written with the TensorFlow library that is accessed mostly via the Python API. Because a TensorFlow developer is a deep learning engineer, they design and create machine learning models, train them, and improve them to reach high level of model accuracy and robustness.


Let’s have a look at the definition from the official TensorFlow website:

TensorFlow is “An end-to-end open source machine learning platform. The core open source library to help you develop and train ML models. TensorFlow makes it easy for beginners and experts to create machine learning models for desktop, mobile, web, and cloud. See the sections below to get started.”

? The average annual income of a TensorFlow Developer in the United States is between $104,000 (25th percentile) and $187,000 (75th percentile) with an average of $148,000 per year according to Ziprecruiter (source). Top earners make $197,000 and more in the US!

Do you want to become a TensorFlow Developer? Here’s a step-by-step learning path I’d propose to get started with TensorFlow:

? Learn More: Read more about this specific Python library career path in our in-depth Finxter article.

#7 – PyTorch


A PyTorch Developer writes code using in Python’s PyTorch library to analyze data, create machine learning models, or runs deep learning algorithms on various hardware devices such as GPUs.

What Is PyTorch? Let’s have a look at the definition from the official PyTorch website:

“An open source machine learning framework that accelerates the path from research prototyping to production deployment. More specifically, PyTorch is an optimized tensor library for deep learning using GPUs and CPUs.”

? The average annual income of a PyTorch Developer in the United States is $109,000 per year according to PayScale (source). Top earners make $131,000 and more in the US!

Do you want to become a PyTorch Developer? Here’s a step-by-step learning path I’d propose to get started with PyTorch:

? Learn More: Read more about this specific Python library career path in our in-depth Finxter article.

#Bonus – Plotly Dash



If you’re interested in learning more about how to create beautiful dashboard applications in Python, check out our new book Python Dash.


You’ve seen dashboards before; think election result visualizations you can update in real-time, or population maps you can filter by demographic.

With the Python Dash library, you’ll create analytic dashboards that present data in effective, usable, elegant ways in just a few lines of code.

Get the book on NoStarch or Amazon!


Summary


These are some of the most profitable Python libraries you could build your career on:



https://www.sickgaming.net/blog/2022/07/...n-in-2023/

Print this item

  PC - Endling - Extinction is Forever
Posted by: xSicKxBot - 07-27-2022, 10:25 AM - Forum: New Game Releases - No Replies

Endling - Extinction is Forever



Will the last mother fox on Earth be able to save its three little cubs? Experience how life would be in a world ravaged by mankind through the eyes of the last fox on Earth in this eco-conscious adventure. Discover the destructive eect of the human race, which corrupts day after day the most precious and needed resources of the natural environments. Explore Endling's 3D side-scrolling world and defend your cubs, three tiny and defenseless fur balls, feed them, see how they grow up level after level, notice their unique personalities and fears, and most importantly, make them survive. Use the cover of night to sneak with your litter towards a safer place. Spend the day resting in an improvised shelter and plan for your next movement carefully since it could be the last one for you or your cubs.

Publisher: HandyGames

Release Date: Jul 19, 2022




https://www.metacritic.com/game/pc/endli...is-forever

Print this item

  News - GTA Online Update: How To Start Operation Paper Trail Mission
Posted by: xSicKxBot - 07-27-2022, 10:25 AM - Forum: Lounge - No Replies

GTA Online Update: How To Start Operation Paper Trail Mission

One of the biggest content drops of the year just arrived in GTA Online. The Criminal Enterprises update was released on July 26 and it delivers a plethora of content to the online world of Los Santos.

Among the content that arrived with The Criminal Enterprises update is a new IAA mission for players to undertake. Called "Operation Paper Trail," this mission allows players to go undercover to figure out why the oil prices have skyrocketed in Los Santos. Players can expect espionage, criminal conspiracies, and high-speed action every step of the way in this operation. However, there's been some confusion as to how actually start Operation Paper Trail in GTA Online. Below, we lay out all of the details you need to know about the new IAA mission.

Starting Operation Paper Trail

As with most IAA missions in GTA Online, Operation Paper Trail begins with Agent ULP. This agent is the player's liaison for the IAA in Los Santos. This time around, Agent ULP has caught wind of a conspiracy behind the rising oil prices in Los Santos. The agent believes that the Duggan family and the FIB are in cahoots to drive up the price of oil. For what reason is unclear, but that's where you come into play. Agent ULP needs your help to get to the bottom of the conspiracy.

Continue Reading at GameSpot

https://www.gamespot.com/articles/gta-on...01-10abi2f

Print this item

  [Tut] Python Convert String to CSV File
Posted by: xSicKxBot - 07-26-2022, 12:21 PM - Forum: Python - No Replies

Python Convert String to CSV File

5/5 – (1 vote)

Problem Formulation


Given a Python string:

my_string = '''a,b,c
1,2,3
9,8,7'''

? Question: How to convert the string to a CSV file in Python?

The desired output is the CSV file:

'my_file.csv':

a,b,c
1,2,3
9,8,7

Simple Vanilla Python Solution


To convert a multi-line string with comma-separated values to a CSV file in Python, simply write the string in a file (e.g., with the name 'my_file.csv') without further modification.

This works if the string is already in the correct CSV format with values separated by commas.

The following code uses the open() function and the file.write() functions to write the multi-line string to a file without modification.

my_string = '''a,b,c
1,2,3
9,8,7''' with open('my_file.csv', 'w') as out: out.write(my_string)

The result is a file 'my_file.csv' with the following contents:

a,b,c
1,2,3
9,8,7

Parsing and Modifying Text to CSV


The string may not be in the correct CSV format.

For example, you may want to convert any of the following strings to a CSV file—their format is not yet ready for writing it directly in a comma-separated file (CSV):

  1. Example 1: 'abc;123;987'
  2. Example 2: 'abc 123 987'
  3. Example 3: 'a=b=c 1=2=3 9=8=7'

To parse such a string and modify it before writing it in a file 'my_file.csv', you can use the string.replace() and string.split() methods to make sure that each value is separated by a comma and each row has its own line.

Let’s go over each of those examples to see how to parse the string effectively to bring it into the CSV format:

Example 1


# Example 1:
my_string = 'abc;123;987' with open('my_file.csv', 'w') as out: lines = [','.join(line) for line in my_string.split(';')] my_string = '\n'.join(lines) out.write(my_string)

I’ve higlighted the two code lines that convert the string to the CSV format.

  • The first highlighted line uses list comprehension to create a list of three lines, each interleaved with a comma.
  • The second highlighted line uses the string.join() function to bring those together to a CSV format that can be written into the output file.

The output file 'my_file.csv' contains the same CSV formatted text:

a,b,c
1,2,3
9,8,7

Example 2


The following example is the same as the previous code snippet, only that the empty spaces ' ' in the input string should be converted to new lines to obtain the final CSV:

# Example 2:
my_string = 'abc 123 987' with open('my_file.csv', 'w') as out: lines = [','.join(line) for line in my_string.split(' ')] my_string = '\n'.join(lines) out.write(my_string)

The output file 'my_file.csv' contains the same CSV formatted text:

a,b,c
1,2,3
9,8,7

Example 3


If the comma-separated values are not yet comma-separated (e.g., they may be semicolon-separated 'a;b;c'), you can use the string.replace() method to replace the symbols accordingly.

This is shown in the following example:

# Example 3:
my_string = 'a=b=c 1=2=3 9=8=7' with open('my_file.csv', 'w') as out: my_string = my_string.replace('=', ',').replace(' ', '\n') out.write(my_string)

Thanks for reading this article! I appreciate the time you took to learn Python with me.

If you’re interested in writing more concise code, feel free to check out my one-liner book here:

Python One-Liners Book: Master the Single Line First!


Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!



https://www.sickgaming.net/blog/2022/07/...-csv-file/

Print this item

  [Tut] Convert HTML Table to Excel using JavaScript
Posted by: xSicKxBot - 07-26-2022, 12:21 PM - Forum: PHP Development - No Replies

Convert HTML Table to Excel using JavaScript

by Vincy. Last modified on July 25th, 2022.

Converting a HTML table to an excel file is a standard requirement of reporting websites. It will be simple and easier if the conversion is taking place on the client side.

There are many client-side and server-side plugins to perform the excel export. For example, PHPSpreadSheet allows writing data into excel and exporting.

This article will give different options and approaches to achieving the HTML table to excel conversion with JavaScript.

This simple example includes a few lines of JavaScript that build the template for excel export.

It uses the following steps to convert the exported excel report of tabular data.

  1. Defines HTML template structure with required meta.
  2. Include the table’s inner HTML into the template.
  3. Marks the location by specifying the protocol to download the file via browser.
  4. Redirect via JavaScript to point to the location with the encoded excel content.

Quick example


A JavaScript handler to set export template and push HTML table data.

function exportToExcel() { var location = 'data:application/vnd.ms-excel;base64,'; var excelTemplate = '<html> ' + '<head> ' + '<meta http-equiv="content-type" content="text/plain; charset=UTF-8"/> ' + '</head> ' + '<body> ' + document.getElementById("table-conatainer").innerHTML + '</body> ' + '</html>' window.location.href = location + window.btoa(excelTemplate);
}

View demo

The below HTML table code displays a product listing and it is static. Refer jsPDF AutoTables examples to render a dynamic table by loading row by row.

The button below this table triggers the export to excel process on the click event. The exportToExcel() function handles the event and proceeds the client-side export.

<div id="table-conatainer"> <table class="striped"> <thead> <tr> <th>S.No</th> <th>Product Name</th> <th>Price</th> <th>Model</th> </tr> </thead> <tbody> <tr> <td>1q</td> <td>GIZMORE Multimedia Speaker with Remote Control, Black</td> <td>2300</td> <td>2020</td> </tr> <tr> <td>2</td> <td>Black Google Nest Mini</td> <td>3400</td> <td>2021</td> </tr> </tbody> </table>
</div>

html table excel javascript

Let us see more options available for converting HTML table data into an excel file.

Option 2: Export as CSV to view in excel


This example is for parsing the HTML table content via JavaScript. It follows the below steps to export an HTML table to excel as CSV.

  1. Accessing HTML table element object.
  2. Convert the object into an array of row data.
  3. Iterate row data array to prepare comma-separated values of records.
  4. Set the protocol and content type to export the CSV data prepared from the table.

The below JavaScript function implements the above steps to export table data on the client side.

function exportCSVExcel() { var tableElement = document.getElementById("table-product-list"); var sourceData = "data:text/csv;charset=utf-8,"; var i = 0; while (row = tableElement.rows[i]) { sourceData += ([ row.cells[0].innerText, row.cells[1].innerText, row.cells[2].innerText, row.cells[3].innerText ]).join(",") + "\r\n"; i++; } window.location.href = encodeURI(sourceData);
}

Option 3 – Export HTML table to excel using jQuery-based plugin


The jQuery table2excel plugin is a popular solution to arrive HTML to excel export.

It has many features with the export core functionalities. Those are,

  1. Excludes/includes HTML tags like inputs, links, or images that are in the source table HTML.
  2. Excludes some table components with the reference of that particular element’s class or id selectors.
  3. Provides properties to set file name with which the exported data will be downloaded to the browser.

Include the jQuery and the table2excel plugin file in the <head> section as below.

There are alternative methods to install the table2excel plugin. Its Github page provides documentation of installation and usage methodologies.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/rainabba/jquery-table2excel/1.1.0/dist/jquery.table2excel.min.js"></script>

Then, initiate the plugin class by pointing to the source HTML table element.

The below JavaScript does the initiation. During instantiation, it sets the options array to specify a file name, extension and applicable flags.

function exportCSVExcel() { $('#table-product-list').table2excel({ exclude: ".no-export", filename: "download.xls", fileext: ".xls", exclude_links: true, exclude_inputs: true });
}

This example uses the same HTML table source for the export operation. The difference is that the source table content marks some of the rows with no-export class.

This class is configured in the above script with exclude property of this plugin class.

Conclusion:


So, we have seen three simple implementations of adding HTML table to excel feature. Though the export feature is vital, we must have a component that provides a seamless outcome.

I hope, the above solution can be a good base for creating such components. It is adaptable for sure to dock more features to have an efficient excel report generation tool.

View demoDownload

↑ Back to Top



https://www.sickgaming.net/blog/2022/07/...avascript/

Print this item

  (Indie Deal) FREE Interstellaria, Sweet Rose Bundle
Posted by: xSicKxBot - 07-26-2022, 12:21 PM - Forum: Deals or Specials - No Replies

FREE Interstellaria, Sweet Rose Bundle

Interstellaria FREEbie
[freebies.indiegala.com]
Command a fleet of vessels wandering the galaxy for adventure and profit!

https://www.youtube.com/watch?v=-UDoaPCTVr4
Sweet Rose Bundle | 8 Adult?Games | 91% OFF
[www.indiegala.com]
Colorful & enticing, smooth and yet prickly, the latest eroge bundle brings a bouquet of attractive adult 18+ games: Metempsychosis, Secret Kiss is Sweet and Tender, Magical Girl Noble Rose, Maken-shi Sara, Grayscale Memories, Meria and The Island of Orcs & Hero Zex.

https://www.youtube.com/watch?v=RHU8amMVr7E
[www.indiegala.com]
[www.indiegala.com]
Stay Inside, Stay Safe and Enjoy Good Games.
Check out IndieGala on Twitter, YouTube & Facebook[www.facebook.com]


https://steamcommunity.com/groups/indieg...1986144272

Print this item

 
Latest Threads
Codice Sconto Temu [ald91...
Last Post: Benbeckman81
51 minutes ago
Coupon Temu [ald911505] -...
Last Post: Benbeckman81
52 minutes ago
30% Codice Sconto Temu [a...
Last Post: Benbeckman81
53 minutes ago
Compra GRATIS su Temu: CO...
Last Post: Benbeckman81
58 minutes ago
Codice Sconto Temu 100€ [...
Last Post: Benbeckman81
59 minutes ago
TEMU Codice Sconto [ald91...
Last Post: Benbeckman81
1 hour ago
Temu Sconto studenti 30% ...
Last Post: Benbeckman81
1 hour ago
Codice promozionale Temu ...
Last Post: Benbeckman81
1 hour ago
Apollo Neuro Promo Code [...
Last Post: ARJNxKDS
1 hour ago
Apollo Neuro Coupon Code ...
Last Post: ARJNxKDS
1 hour ago

Forum software by © MyBB Theme © iAndrew 2016