stringio - python 3.x ImportError: No module named ...

Is this a StringIO bug in ruby 2.7.0?

When running tests on one of my projects for 2.7.0, I ran into some weird failures, which I reduced/isolated to this.
StringIO no longer preserves encoding of strings it was initialized with in 2.7.0.
Is this a bug? I thought I'd ask for feedback here before trying to submit it as a bug on ruby tracker (and figure out how to do so).
 binary_string = "\x11\x99".force_encoding("ASCII-8BIT") # note this is invalid UTF-8, for realism puts "binary_string.encoding: #{binary_string.encoding}" # "ASCII-8BIT" string_io = StringIO.new(binary_string) retrieved = string_io.read puts "retrieved.encoding: #{retrieved.encoding}" # On Ruby 2.7.0, UTF-8 # On Ruby 2.6.5, ASCII-8BIT 
Oh except OOPS, I can't reproduce this in a straight irb terminal. But it is reproducing in my actual project!
So it must not be StringIO by itself, it must be one of my dependencies rudely changing the behavior of StringIO somehow... this is painful.
Does Rails monkey-patch StringIO somehow? Confirmed I can reproduce this in a project that does "require 'rails'".
I might go ahead and report this as a bug to rails then.... unless it's somehow intended or a bugfix? Very odd. Still curious if anyone has any advice. I think I may have trouble getting this report consideration on Rails tracker. I really don't understand what's going on.
submitted by jrochkind to ruby [link] [comments]

Need help with differences between StringIO and using physical files

I've just started back at learning Python so tried to modify an old webscrape I wrote in order to not use physical files, I found online StringIO which seemed perfect.

However, when substituting StringIO in place of open() I can't quite figure out where I've gone wrong.
It seems my loop just overwrites the string rather than creating the list it does with a physical file.

Screenshots of outputs

Any pointers would be greatly appreciated
submitted by GKing360 to learnpython [link] [comments]

Why is it necessary to rewind files written to a StringIO object?

from io import StringIO file = StringIO() file.write('reddit') file.seek(0) print(file.read()) 
Why is this?
submitted by python_noob_001 to learnpython [link] [comments]

Can I get help simplifying Popen and StringIO?

The below code works, but I want to simplify it. For instance, with a file I can use with open(filename) as f: However, I don't have the option of going with StringIO.StringIO(stdout) as buf: because StringIO.StringIO isn't built for that.
How can I simplify the below script? Right now, it works perfectly fine, but I'm trying to get into the habit of using with.
The code itself just retrieves the IP addresses for all interfaces (excluding loopback) and prints them out in a friendly format.
runCmd = 'ip -4 -o addr' proc = subprocess.Popen(runCmd.split(' '),stdout=subprocess.PIPE) stdout, stderr = proc.communicate() buf = StringIO.StringIO(stdout) for line in buf: line = line.split() if line[1] != 'lo': iface = line[1] + ':' print(iface.ljust(10) + line[3]) buf.close() 
submitted by UtahJarhead to learnpython [link] [comments]

Is there anything out there that can slice lists as efficiently as io.StringIO.read can slice strings?

[*map(StringIO(x).read, repeat(16, 16))] is 3.9x faster than [x[i:i+16] for i in range(0, 1024, 16)]. Something in C that can can bypass all of the Python overhead and the overhead required to create slice objects which for some reason are retardedly slow to create.
submitted by Sexual_Congressman to learnpython [link] [comments]

How do you expose a buffer interface from Pillow to xlsxwriter/or some other module - for inserting modified images into a excel sheet?

In my example - I need to insert a modified image into an excel bill-of-material - but I get
> TypeError: 'JpegImageFile' does not have the buffer interface
import xlsxwriter import PIL from PIL import Image from io import BytesIO from StringIO import StringIO i = PIL.Image.open('/tmp/python.jpg') o = BytesIO(i) print(type(o)) # Create an new Excel file and add a worksheet. workbook = xlsxwriter.Workbook('images.xlsx') worksheet = workbook.add_worksheet() wrap_format = workbook.add_format({'text_wrap': True}) wrap_format = wrap_format.set_align('vjustify') worksheet.set_column(0, 0, 30) worksheet.write('A2', 'Some text that wraps', wrap_format) worksheet.write('A3', 'Some text that wraps', wrap_format) worksheet.write('A4', 'Some text that wraps', wrap_format) worksheet.write('A5', 'Some text that wraps', wrap_format) worksheet.insert_image('B2', o, {'object_position': 1, 'anchor': 1}) worksheet.insert_image('B3', '/tmp/python.jpg', {'object_position': 1, 'anchor'$ worksheet.insert_image('B4', '/tmp/python.jpg', {'object_position': 1, 'anchor'$ worksheet.insert_image('B5', '/tmp/python.jpg', {'object_position': 1}) worksheet.insert_image('B6', '/tmp/python.jpg', {' 
submitted by veekm to learnpython [link] [comments]

Help understanding why this error is raised? StringIO / .dot issue?

Hi, I'm trying to learn ML from the Wesleyan Coursera page. The topic is decision trees and I'm trying to visualize the tree I got in a Jupyter notebook. I follow the teacher's steps 1 by 1 see notebook on github but whereas the teacher gets the chart plotted in the notebook, I get the following error:
TypeError Traceback (most recent call last)  in () 1 out = StringIO() -- 2 tree.export_graphviz(classifier, out_file=out) 3 graph=pydotplus.graph_from_dot_data(out.getvalue()) 4 Image(graph.create_png()) /Users/__/miniconda2/envs/sandbox/lib/python2.7/site-packages/sklearn/tree/export.pyc in export_graphviz(decision_tree, out_file, max_depth, feature_names, class_names, label, filled, leaves_parallel, impurity, node_ids, proportion, rotate, rounded, special_characters) 373 colors = {'bounds': None} 374 375 out_file.write('digraph Tree {\n') 376 377 # Specify node aesthetics TypeError: unicode argument expected, got 'str' 
I suspect it has something to do with StringIO and/or path stuff, but that's really just a shot in the dark. Appreciate your help!
submitted by Optimesh to learnpython [link] [comments]

Chart only redraws if breakpoints are ON

Hi, I have this very strange problem. This is my code:
import numpy as np import matplotlib.pyplot as plt import time import pandas as pd from datetime import datetime from io import StringIO
plt.ion() # Enable interactive mode fig = plt.figure() # Create figure
axes = fig.add_subplot(111) # Add subplot (dont worry only one plot appears)
axes.set_autoscale_on(True) # enable autoscale axes.autoscale_view(True,True,True)
l, = plt.plot([], [], 'r-') # Plot blank data plt.xlabel('x') # Set up axes plt.title('test')
def redraw():
df_data = pd.read_csv(r'C:\PythonProjects\Trader\venv\MACD_HIST.csv', names=['time', 'value'])
xdata = df_data['value'] ydata = df_data['time'] print(xdata) l.set_data(ydata,xdata) # update data axes.relim() # Recalculate limits axes.autoscale_view(True,True,True) #Autoscale plt.draw() # Redraw
while True: print("Redrawing") #if float(int(datetime.now().strftime('%S')) / 10).is_integer: #(run every 10 secs) redraw()
My file contains this:
34,-0.011344703807079299
35,-0.021262789441955147
36,-0.03096811875140209
37,-0.0482570408076033
38,-0.05205214122791929
39,-0.05728281908239152
40,-0.05790311604719256
41,-0.05483104904414484
42,-0.04692804506301557
43,-0.04013349322439646
44,-0.03174570044733168
45,-0.035490174964525265
46,-0.022311026500653426
47,-0.011928033178375664
48,-0.017216432396662866
49,-0.01478012126564413
50,-0.010787023100289402
51,-0.0028120167873849616
52,0.0034180587270479915
53,0.006305534206970348
54,0.005655841641916258
55,0.016850173863149184
56,0.02654658471977414
57,0.03282079065402783
58,0.04420443503661561
59,0.049158603219676474
60,0.04332938581572159
61,0.04498805804834132
62,0.03774879781523039
63,0.027778649284614848
64,0.02229976111891717
65,0.01604323481542106
66,0.021077891149583337
67,0.010541493896320366
68,0.0016227739225782376
69,0.002478114432807638
70,-0.0008016659984451912
71,-0.0052231481801123655
72,-0.0030965978671940605
73,0.0011151829338808751
74,-0.0006093274493566722
75,-0.005962799677427069
76,-0.007553016854565688
77,0.002888727565856544
78,0.011383441992201123
79,0.032226429644360344
80,0.03985952553915888
81,0.0368073998626859
82,0.031601326741420355
83,0.029075146059425558
84,0.028294828466721676
85,0.03943420278449225
86,0.04396907673368555
87,0.036368318138319436
88,0.03743114969635123
89,0.03548682687638207
90,0.030402367632562866
91,0.02546150423697247
92,0.03804238947301413
93,0.04554311088865248
94,0.025111969616972506
95,0.018181778865215378
96,0.02322645763233813
97,0.030992127962795362
98,0.031051063556743166
99,0.02492935268969902
The file is updated every minute and if I put a breakpoint on Redraw() then manually step through it, the chart updates. But if I run it with no breakpoints the chart doesn't appear at all.
Grateful for any pointers
submitted by paulbappoo to learnpython [link] [comments]

In pytest with pytest-mock, how do I test if one argument of a multi-argument (mocked) function was called with a certain argument?

I am trying to test if print() is called with a certain argument. While I can patch the function with mocker.patch('builtins.print'), the call in the tested code also takes the TextIO object destination, be it a file or the standard output destination. How do I test if the first argument to print() is the expected text? Alternatively, is there a way to also test if the destination argument is the aforementioned file or standard output destination?
Thanks in advance.
submitted by AlexKingstonsGigolo to AskPython [link] [comments]

Azure Functions - Python , Pandas and Whether this is the right way

Hi Everyone ! Thank you kindly for giving up your time to read this.
I am currently trying to work on an Azure Function on Logic Apps that triggers on someone uploading a csv to the blob storage. My entire code base is below at the moment. At the moment, I can verify that the pandas dataframe is being read correctly, but I am not sure why my outputblob.set isn't working well.
I am also not really sure if this is the right way to approach the problem. In terms of structure, I have a storage account, one container to take in the raw data, and one to drop the output files. The output files should be in either csv or excel format, and there is more than one output per raw file dropped. After that the function should also ingest the data into a MySQL database on Azure.
Should I be trying to use the bindings for this, or should I be using some other approach. The base idea is so that I can have someone just upload a csv into the blob storage and then not worry about having to do anything else.
import logging import pandas as pd from io import BytesIO, StringIO import azure.functions as func def main(myblob: func.InputStream, outputblob: func.Out[func.InputStream]): logging.info(f"Python blob trigger function processed blob \n" f"Name: {myblob.name}\n" f"Blob Size: {myblob.length} bytes" f"Blob URI: {myblob.uri}") blob_bytes = myblob.read() blob_to_read = BytesIO(blob_bytes) df = pd.read_csv(blob_to_read) print("Length of the csv file:" + str(len(df.index))) outputs = df.to_csv(StringIO(), index = False) outputblob.set(outputs) 
And this is my functions.json
{ "scriptFile": "__init__.py", "bindings": [ { "name": "myblob", "type": "blobTrigger", "direction": "in", "path": "raws/{name}", "connection": "data_STORAGE" }, { "name": "outputblob", "type": "blob", "path": "reports/{datetime}-{name}", "connection": "data_STORAGE", "direction": "out" } ] } 
Not sure if I am approaching the problem the right way with adding the details in function.json or should I be using the AzureBlobService library and using that instead. I've spent quite some time checking stackoverflow and they have one using excel, which is what I based on to use the StringIO() method to output.
Thanks for any input you guys have !
P.S, also posted this on LearnPython.
EDIT : The outputblog.set(outputs) outputs an empty file...
submitted by ElethorAngelus to AZURE [link] [comments]

No module named StringIO (django-storages, Python 3)

Hello,
I'm trying to deploy on Heroku and make it work with AWS S3, but turns out it can't properly import boto or django-storages. I've learned that django-storages-redux solves the StringIO import error of Python 3; it works on my development machine, but it's not imported properly when I run the shell on Heroku. Boto, django-storages, and django-storages-redux are in the requirements.txt and are displayed when using pip list.
from django.core.files.storage import default_storage # this works default_storage.exists('test') # returns False on my dev machine, errors on Heroku 
EDIT: Apparently Heroku does some weird thing, where the traceback shows ImportError of StringIO from s3boto.py, line 9, but according to this that line should've been changed already.
EDIT: Turns out it had to do something with the installed packages being cached; the solution was to change runtime.txt to refresh the whole thing, as suggested here.
submitted by silverpendulum to django [link] [comments]

'require' cannot load such file (loaderror) ... help! :)

Just got a new laptop and I copied my scripts over and installed Ruby and the gems I want to use. When I try to run a script I get this error. I have been searching Google all night and find similar problems others are having but I can't seem to get my problem fixed. I didn't have this issue on the other laptop. I am on Windows 10. The only thing that I know I did differently on the new computer was also to install rails. I'm at my wit's end, any help would be appreciated.

RubyGems Environment: - RUBYGEMS VERSION: 3.1.4 - RUBY VERSION: 2.6.6 (2020-03-31 patchlevel 146) [x64-mingw32] - INSTALLATION DIRECTORY: C:/Ruby26-x64/lib/ruby/gems/2.6.0 - USER INSTALLATION DIRECTORY: C:/Users/gonzo/.gem/ruby/2.6.0 - RUBY EXECUTABLE: C:/Ruby26-x64/bin/ruby.exe - GIT EXECUTABLE: - EXECUTABLE DIRECTORY: C:/Ruby26-x64/bin - SPEC CACHE DIRECTORY: C:/Users/gonzo/.gem/specs - SYSTEM CONFIGURATION DIRECTORY: C:/ProgramData - RUBYGEMS PLATFORMS: - ruby - x64-mingw32 - GEM PATHS: - C:/Ruby26-x64/lib/ruby/gems/2.6.0 - C:/Users/gonzo/.gem/ruby/2.6.0 - GEM CONFIGURATION: - :update_sources => true - :verbose => true - :backtrace => false - :bulk_threshold => 1000 - REMOTE SOURCES: - https://rubygems.org/ - SHELL PATH: - C:\windows\system32 - C:\windows - C:\windows\System32\Wbem - C:\windows\System32\WindowsPowerShell\v1.0\ - C:\windows\System32\OpenSSH\ - C:\Ruby26-x64\bin - C:\Users\gonzo\AppData\Local\Microsoft\WindowsApps - C:\Users\gonzo\AppData\Local\GitHubDesktop\bin - C:\Users\gonzo\AppData\Local\atom\bin 
Gem list:
*** LOCAL GEMS *** actioncable (6.0.3.2) actionmailbox (6.0.3.2) actionmailer (6.0.3.2) actionpack (6.0.3.2) actiontext (6.0.3.2) actionview (6.0.3.2) activejob (6.0.3.2) activemodel (6.0.3.2) activerecord (6.0.3.2) activestorage (6.0.3.2) activesupport (6.0.3.2) bigdecimal (default: 1.4.1) builder (3.2.4) bundler (2.1.4) childprocess (3.0.0) cmath (default: 1.0.0) concurrent-ruby (1.1.7) crass (1.0.6) csv (default: 3.0.9) date (3.0.1, default: 2.0.0) dbm (default: 1.0.0) did_you_mean (1.3.0) diff-lcs (1.4.4) e2mmap (default: 0.1.0) erubi (1.9.0) etc (default: 1.0.1) fcntl (default: 1.0.0) fiddle (default: 1.0.0) fileutils (default: 1.1.0) forwardable (default: 1.2.0) gdbm (default: 2.0.0) globalid (0.4.2) i18n (1.8.5) io-console (default: 0.4.7) ipaddr (default: 1.2.2) irb (default: 1.0.0) json (default: 2.1.0) logger (default: 1.3.0) loofah (2.7.0) mail (2.7.1) marcel (0.3.3) matrix (default: 0.1.0) method_source (1.0.0) mimemagic (0.3.5) mini_mime (1.0.2) mini_portile2 (2.4.0) minitest (5.11.3) mutex_m (default: 0.1.0) net-telnet (0.2.0) nio4r (2.5.2) nokogiri (1.10.10 x64-mingw32) openssl (default: 2.1.2) ostruct (default: 0.1.0) power_assert (1.1.3) prime (default: 0.1.0) psych (default: 3.1.0) rack (2.2.3, 2.0.9) rack-test (1.1.0) rails (6.0.3.2) rails-dom-testing (2.0.3) rails-html-sanitizer (1.3.0) railties (6.0.3.2) rake (12.3.3) rdoc (default: 6.1.2) regexp_parser (1.7.1) rexml (default: 3.1.9) rspec (3.9.0) rspec-core (3.9.2) rspec-expectations (3.9.2) rspec-mocks (3.9.1) rspec-support (3.9.3) rss (default: 0.2.7) rubyzip (2.3.0) scanf (default: 1.0.0) sdbm (default: 1.0.0) selenium-webdriver (3.142.7) shell (default: 0.7) sprockets (4.0.2) sprockets-rails (3.2.1) stringio (default: 0.0.2) strscan (default: 1.0.0) sync (default: 0.5.0) test-unit (3.2.9) thor (1.0.1) thread_safe (0.3.6) thwait (default: 0.1.0) tracer (default: 0.1.0) tzinfo (1.2.7) watir (6.17.0) webdrivers (4.4.1) webrick (default: 1.4.2) websocket-driver (0.7.3) websocket-extensions (0.1.5) xmlrpc (0.3.0) zeitwerk (2.4.0) zlib (default: 1.0.0) 

Here's the error:
C:\Users\gonzo\OneDrive\Desktop\Ruby>ruby test.rb Traceback (most recent call last): 2: from test.rb:5:in `
' 1: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:92:in `require' C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- watir (LoadError) 18: from test.rb:5:in `
' 17: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:156:in `require' 16: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:168:in `rescue in require' 15: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:168:in `require' 14: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/watir-6.17.0/lib/watir.rb:1:in `' 13: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 12: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 11: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.142.7/lib/selenium-webdriver.rb:20:in `' 10: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 9: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 8: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver.rb:20:in `' 7: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 6: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 5: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/childprocess-3.0.0/lib/childprocess.rb:209:in `' 4: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 3: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 2: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/childprocess-3.0.0/lib/childprocess/windows.rb:4:in `' 1: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:92:in `require' C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- ffi (LoadError) 17: from test.rb:5:in `
' 16: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:156:in `require' 15: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:168:in `rescue in require' 14: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:168:in `require' 13: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/watir-6.17.0/lib/watir.rb:1:in `' 12: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 11: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 10: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.142.7/lib/selenium-webdriver.rb:20:in `' 9: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 8: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 7: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/selenium-webdriver-3.142.7/lib/selenium/webdriver.rb:20:in `' 6: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 5: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 4: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/childprocess-3.0.0/lib/childprocess.rb:209:in `' 3: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 2: from C:/Ruby26-x64/lib/ruby/site_ruby/2.6.0/rubygems/core_ext/kernel_require.rb:72:in `require' 1: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/childprocess-3.0.0/lib/childprocess/windows.rb:3:in `' C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/childprocess-3.0.0/lib/childprocess/windows.rb:6:in `rescue in ': FFI is a required pre-requisite for Windows or posix_spawn support in the ChildProcess gem. Ensure the `ffi` gem is installed. If you believe this is an error, please file a bug at http://github.com/enkesslechildprocess/issues (ChildProcess::MissingFFIError)
submitted by vitus7 to ruby [link] [comments]

Is there memory benefit using io.BytesIO/StringIO vs bytes/str

Hi /Python ,
I'm working on a project and want to determine the best internal interface. I know io.BytesIO/StringIO has a file like interface that's compatible with most method that takes a file pointer.
Does io.BytesIO/StringIO behave like files when created with in-memory bytes/str? In a scene that a large file is divided into numbers of logical blocks. It will only read the current block into memory when necessary and write back unnecessary block to disk under memory pressure. I know this caching behavior is done by the kernel, does python do this with io.BytesIO/StringIO?
I'm asking this because the real implementation is hiding under this funny _io module that I have no access to.
from _io import (DEFAULT_BUFFER_SIZE, BlockingIOError, UnsupportedOperation, open, FileIO, BytesIO, StringIO, BufferedReader, BufferedWriter, BufferedRWPair, BufferedRandom, IncrementalNewlineDecoder, TextIOWrapper) 
Thanks,
submitted by Bbox55 to Python [link] [comments]

How to run only highlighted text in Sublime 3? (Python)

Any help would be appreciated.
submitted by TheKingWhite to SublimeText [link] [comments]

Scikit learn - decision tree x values are greater than number of columns

I've been staring at this for so long I don't even think I can figure it out at this point. My decision tree is returning x values greater than the number of columns (x19).
Why is this happening? Code below, any guidance is super appreciated.
import pandas as pd

load data

bank = pd.read_csv("bank.csv")
names = list(bank.columns.values)
X = bank[['checking_status', 'duration', 'credit_history', 'purpose', 'credit_amount', 'savings_status', 'employment', 'installment_commitment', 'personal_status', 'other_parties', 'residence_since', 'property_magnitude', 'age', 'other_payment_plans', 'housing', 'existing_credits', 'job', 'num_dependents', 'own_telephone', 'foreign_worker' ]]
x_index = 0 for column in list(X.columns): print('X' + str(x_index) + ":" + column) x_index += 1
X = pd.get_dummies(X, drop_first=True)
y = bank[['target']] y.head bank.dtypes
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3, random_state = 100)
from sklearn.tree import DecisionTreeClassifier

initialize the algorithm

dtree=DecisionTreeClassifier()

train a model

dtree.fit(X_train, y_train)
from sklearn.metrics import accuracy_score
y_pred_train = dtree.predict(X_train)
print(accuracy_score(y_train, y_pred_train))
from sklearn.metrics import accuracy_score
y_pred_test = dtree.predict(X_test)
print(accuracy_score(y_test, y_pred_test))

import the required libraries

from graphviz import Source from sklearn import tree

visualize the generated tree model

Source(tree.export_graphviz(dtree, out_file= None, class_names = bank.target, filled=True, rounded=True, special_characters=True))

load the libraries

from sklearn.externals.six import StringIO import pydot

generate DOT file for tree, convert to PNG

dot_data = StringIO() tree.export_graphviz(dtree, out_file=dot_data) graph=pydot.graph_from_dot_data(dot_data.getvalue()) graph[0].write_png("bank_tree.png")
submitted by ParzivalsQuest to learnpython [link] [comments]

pdfminer.six example not working on Windows?

Hello fellow snakes!
I have a strange problem I hope You can help with, at wits' end.
Using Python 3.7 on Win10 and Anaconda (Spyder IDE), I am trying to extract text as string from a pdf file using pdfminer.six. When I do so however, a process does run, but nothing gets displayed. It's been a while since I last snakecharmed, so what am I doing wrong?
Code example:
from io import StringIO from pdfminer.converter import TextConverter from pdfminer.layout import LAParams from pdfminer.pdfdocument import PDFDocument from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter from pdfminer.pdfpage import PDFPage from pdfminer.pdfparser import PDFParser file = r'C:\Users\grege\Downloads\A Sample PDF.pdf' output_string = StringIO() with open(file, 'rb') as in_file: parser = PDFParser(in_file) doc = PDFDocument(parser) rsrcmgr = PDFResourceManager() device = TextConverter(rsrcmgr, output_string, laparams=LAParams()) interpreter = PDFPageInterpreter(rsrcmgr, device) for page in PDFPage.create_pages(doc): interpreter.process_page(page) print(output_string.getvalue()) 
Thank You very much!

GregersDK
submitted by gregersdk to learnpython [link] [comments]

How to modify stdin, for interfacing command prompt program?

I need to create a gui for a command prompt program, since it reads user input from stdin (i assume), i would need to "write" to stdin which is not writable.
I use tkinter for the gui, and I've successfully used a StringIO object in place of stdout to get the programs prints in a widget.
The program runs via Thread.
I have access to the source code of said program.
I tried using StringIO in place of stdin as well but the program would repeatedly get "eof" as input, and i didn't find a way to stop the program from repeatedly trying to read the object.
Edit: the command line program is also written in python
Edit 2: Solved!
Solution: instead of putting the Cmd subclass (the program) into a loop, i redirected stdout using redirect_stdout from contextlib and emulated the behaviour of the loop by calling Cmd.default method with a line parameter.
submitted by Oshri_Pz to learnpython [link] [comments]

How to Parse Tables from .html files

I’m working a cleanup project and I have lots of reports that were delivered as .html files, each report is the result of an audit performed by Belarc Advisor and lists the installed software on a desktop pc in multiple tables. I need to inventory the licenses installed in each which is in a table in the output. I have to pull from these html files as that is all I have and all I’m getting. I was gonna use beautiful soup to parse the data but I’m not sure how to use with a html file as the source and not a url. I’ve never used it before. Is there a html parser that can work on files saved to my pc when there is no url (I have no idea why reports were saved as html files). Do I have to cover these to xml? There are a couple hundred and I need it all in a spreadsheet at the end. Thanks.
submitted by general_neckbeard to AskProgramming [link] [comments]

StringIO in Python (2|3)

Greetings,
I am trying to run the very first program provided by a tutorial on compilers (Let's build a compiler!). The full text is here. I've tried running in the Python 3.2 interpreter and get the following:
C:\python3>python.exe H:\projects\projects\lbac\cradle.py Enter your code on a single line. Enter '.' by itself to quit. 1 Traceback (most recent call last): File "H:\projects\projects\lbac\cradle.py", line 65, in  main() File "H:\projects\projects\lbac\cradle.py", line 61, in main init(inp=StringIO(line)) NameError: global name 'StringIO' is not defined 
I understand from 2.7->3 that the 'import io' line should be taking care of StringIO. Is this incorrect / am I missing something?
submitted by nabokovian to learnprogramming [link] [comments]

Mockio (Mocking builtin open method with StringIO)

Mockio (Mocking builtin open method with StringIO) submitted by fthrkl to Python [link] [comments]

Help with Python Azure Functions

Hi Everyone ! Thank you kindly for giving up your time to read this.
I am currently trying to work on an Azure Function on Logic Apps that triggers on someone uploading a csv to the blob storage. My entire code base is below at the moment. At the moment, I can verify that the pandas dataframe is being read correctly, but I am not sure why my outputblob.set isn't working well.
I am also not really sure if this is the right way to approach the problem. In terms of structure, I have a storage account, one container to take in the raw data, and one to drop the output files. The output files should be in either csv or excel format, and there is more than one output per raw file dropped. After that the function should also ingest the data into a MySQL database on Azure.
Should I be trying to use the bindings for this, or should I be using some other approach. The base idea is so that I can have someone just upload a csv into the blob storage and then not worry about having to do anything else.
import logging import pandas as pd from io import BytesIO, StringIO import azure.functions as func def main(myblob: func.InputStream, outputblob: func.Out[func.InputStream]): logging.info(f"Python blob trigger function processed blob \n" f"Name: {myblob.name}\n" f"Blob Size: {myblob.length} bytes" f"Blob URI: {myblob.uri}") blob_bytes = myblob.read() blob_to_read = BytesIO(blob_bytes) df = pd.read_csv(blob_to_read) print("Length of the csv file:" + str(len(df.index))) outputs = df.to_csv(StringIO(), index = False) outputblob.set(outputs) 
And this is my functions.json
{ "scriptFile": "__init__.py", "bindings": [ { "name": "myblob", "type": "blobTrigger", "direction": "in", "path": "raws/{name}", "connection": "data_STORAGE" }, { "name": "outputblob", "type": "blob", "path": "reports/{datetime}-{name}", "connection": "data_STORAGE", "direction": "out" } ] } 
Not sure if I am approaching the problem the right way with adding the details in function.json or should I be using the AzureBlobService library and using that instead. I've spent quite some time checking stackoverflow and they have one using excel, which is what I based on to use the StringIO() method to output.
Thanks for any input you guys have !
submitted by ElethorAngelus to learnpython [link] [comments]

[2.2.0] StringIO#seek doesn't accept symbols as whence argument?

Is this just an oversight?
submitted by mhd-hbd to ruby [link] [comments]

stringIO String io 100% Map Technique String IO (Input/Output) Python 3.7: Encode String Method - YouTube PAPER.IO 2 VS STRING.IO...100% MAP...Which Is Better?

The problem is, that it was replaced in Python 3 by from io import StringIO This bug always occures for me (Python 3.6), and was generated by this issue fix 👍 13 For example, I had some code which used StringIO.StringIO or io.StringIO depending on Python version, but I was actually passing a byte string, so when I got around to testing it in Python 3.x it failed and had to be fixed. Another advantage of using io.StringIO is the support for universal newlines. Reading file using StringIO It is also possible to read a file and stream it over a network as Bytes. The io module can be used to convert a media file like an image to be converted to bytes. The StringIO module an in-memory file-like object. This object can be used as input or output to the most function that would expect a standard file object. When the StringIO object is created it is initialized by passing a string to the constructer. Reactive. Spring's asynchronous, nonblocking architecture means you can get more from your computing resources.

[index] [1510] [5651] [1704] [3799] [149] [6504] [1951] [406] [4376] [6284]

stringIO

jogando StringIO ao som de dont be sad e Death Bed For a full written tutorial please visit https://www.mastercode.online *Note - We no longer answer questions on YouTube, If you have a question please visit ... Wormate.io Best Trolling Pro Never Mess With Tiny Snake Epic Wormateio Funny/Best Moments! #N1 - Duration: 11:31. Slither MasterSp 23,236,755 views Tipos de buffer customizaveis Como utilizar os buffers StringIO e BytesIO Diferença entre os buffers Como redirecionar a saída da função print Bibliografia: Programming Python - pg 126 a 128 ... The next video is starting stop. Loading... Watch Queue

https://forex-portugal.binary-option.online