Change the length of Python csv strings

import csv I had to change the length of the csv string during the extremum test. I wish I had done it quickly with EXCEL, but I changed the length of the character string with Python because of my interest. Below is the code

code

length.py


import csv
import random

#List for output
exp_list = []
max_len = 256
#read csv
with open(r'sample.csv', 'r',encoding='utf-8') as f:
    reader = csv.reader(f)
    for r in reader:
        buff = ''
        r_len = len(r[0])
        while len(r) < max_len - r_len:
            ran = random.randrange(10)
            r += str(ran)
        for x in r:
            buff += x
        exp_list.append(buff)
#csv write
with open(r'output.csv', 'w',encoding='utf-8') as f:
    writer = csv.writer(f, lineterminator='\n')
    writer.writerow(exp_list)

Execution result

Execution command.txt


python length.py

input

sample.csv


data1
data2
data3

output

sample.csv


data198745...
data265644...
data354351...

Recommended Posts

Change the length of Python csv strings
Change the Python version of Homebrew
the zen of Python
Remove specific strings at the end of python
Towards the retirement of Python2
About the ease of Python
Change the theme of Jupyter
Change the style of matplotlib
About the features of Python
[Python] Let's change the URL of the Django administrator site
The Power of Pandas: Python
[Python] Change the Cache-Control of the object uploaded to Cloud Storage
[Python: UnicodeDecodeError] One of the error solutions when reading CSV
The story of Python and the story of NaN
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Change the background of Ubuntu (GNOME)
Existence from the viewpoint of Python
pyenv-change the python version of virtualenv
[Python] Understanding the potential_field_planning of Python Robotics
Review of the basics of Python (FizzBuzz)
Change the suffix of django-filter / DateFromToRangeFilter
About the basics list of Python basics
[Python] Change the alphabet to numbers
Learn the basics of Python ① Beginners
How to change the log level of Azure SDK for Python
Feel free to change the label of the legend in Seaborn in python
Check the behavior of destructor in Python
[Python3] Understand the basics of Beautiful Soup
Script to change the description of fasta
Pass the path of the imported python module
The story of making Python an exe
Learning notes from the beginning of Python 1
Check the existence of the file with python
About the virtual environment of python version 3.7
[Python] Understand the content of error messages
[Python3] Rewrite the code object of the function
I didn't know the basics of Python
The result of installing python in Anaconda
[Python] Accelerates loading of time series CSV
[Python] Try pydash of the Python version of lodash
[python] Checking the memory consumption of variables
Check the path of the Python imported module
The story of manipulating python global variables
[python] [meta] Is the type of python a type?
The basics of running NoxPlayer in Python
Pandas of the beginner, by the beginner, for the beginner [Python]
Bulk replacement of strings in Python arrays
The Python project template I think of.
Change the order of PostgreSQL on Heroku
In search of the fastest FizzBuzz in Python
Python Basic Course (at the end of 15)
Set the process name of the Python program
[Python] Get the character code of the file
[Python] Various combinations of strings and values
The story of blackjack A processing (python)
Intuitively learn the reshape of Python np
Python Note: The secret role of commas
Change the destination batfish server of pybatfish
Learning notes from the beginning of Python 2
Japanese translation: PEP 20 --The Zen of Python