[GO] Python> Read from a multi-line string instead of a file> io.StringIO ()

Operating environment


Python3 (ideone)

link

https://docs.python.jp/3/library/io.html

code

http://ideone.com/KLd4OP

import io

emuCsv = """1,2,3
4,5,6
7,8,9"""

print('begin')
with io.StringIO(emuCsv) as fin:
	print(fin.read())

print('end')

run


begin
1,2,3
4,5,6
7,8,9
end

v0.2

http://ideone.com/Qu0vZr

import io
 
emuCsv = """1,2,3
4,5,6
7,8,9"""
 
print('begin')
with io.StringIO(emuCsv) as fin:
	print('line9')
	print(fin.read())
 
print('end')

run


begin
line9
1,2,3
4,5,6
7,8,9
end

In fin.read (), 3 lines are printed () at a time.

In the above case, it doesn't seem to make sense to use the with syntax.

v0.3> use readline ()

There seems to be readline (). https://stackoverflow.com/questions/7472839/python-readline-from-a-string

http://ideone.com/hi1DN7

import io

emuCsv = """1,2,3
4,5,6
7,8,9"""

print('begin')
fin = io.StringIO(emuCsv)
print(fin.readline(),end='')
print(fin.readline(),end='')
print(fin.readline())
print('end')

run


begin
1,2,3
4,5,6
7,8,9
end

You can run readline () a fixed number of times. Unknown about EOF.

Recommended Posts

Python> Read from a multi-line string instead of a file> io.StringIO ()
Read line by line from a file with Python
Make a copy of a Google Drive file from Python
Create an instance of a predefined class from a string in Python
Read a file in Python with a relative path from the program
The idea of feeding the config file with a python file instead of yaml
[Note] Read a file from another directory
# 5 [python3] Extract characters from a character string
python string comparison / use'list'and'in' instead of'==' and'or'
Create a deb file from a python package
Generate a class from a string in Python
A memorandum of python string deletion process
[Python] How to read a csv file (read_csv method of pandas module)
How to read a CSV file with Python 2/3
Read a file containing garbled lines in Python
A memorandum of calling Python from Common Lisp
Create a datetime object from a string in Python (Python 3.3)
Run a Python file from html using Django
Various ways to read the last line of a csv file in Python
[Python] Multi-line string assignment
Read Python csv file
[Python] Read From Stdin
Parse a JSON string written to a file in Python
Template of python script to read the contents of the file
Cut a part of the string using a Python slice
Python points from the perspective of a C programmer
Read QR code from image file with Python (Mac)
Basics of Python learning ~ What is a string literal? ~
Python script to create a JSON file from a CSV file
I tried using Python (3) instead of a scientific calculator
[Python] Start a batch file from Python and pass variables.
[Scientific / technical calculation by Python] Plot, visualization, matplotlib of 2D data read from file
Extract template of EML file saved from Thunderbird with python3.7
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Find out the apparent width of a string in python
Summary of python file operations
[Python] Use a string sequence
Different from the import type of python. from A import B meaning
I want to start a lot of processes from python
How to quickly count the frequency of appearance of characters from a character string in Python?
[Note] Import of a file in the parent directory in Python
Write and read a file
Python: Create a dictionary from a list of keys and values
[Python] Read command line arguments from file name or stdin
Python --Most elegant way to read lines of file into list
Format when passing a long string as an argument of python
Try to extract a character string from an image with Python3
How to get a string from a command line argument in python
Read the standard output of a subprocess line by line in Python
Outputs a line containing the specified character string from a text file
[Python] Get the update date of a news article from HTML
BeautifulSoup use case: read anchored links of interest from a url
Python vba to create a date string for creating a file name
[Python] Read a csv file with a large data size using a generator
I made a subtitle file (SRT) from JSON data of AmiVoice
Get the formula in an excel file as a string in Python
DataFrame of pandas From creating a DataFrame from two lists to writing a file
Trial of writing the configuration file in Python instead of .ini etc.
A record of patching a python package
Execute Python script from batch file
Touch a Python object from Elixir