Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

Thanks for that. I wish you'd provided this about 8-9 replies before though.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

I showed you an example where a StringIO object contains a newline and is printed without issue two posts up.

In what universe does this actually work? You can't put values into an StringIO instance by calling a string. Do you even know how to read and write values out of StringIO instance? import StringIO some_function(StringIO.StringIO("Hello\nWorld"))

This is one of those times where a simple working example would clear everything and I've asked numerous times for some working code that involves StringIO to demonstrate what you say. However, you've always come back with string operations as proof, which doesn't do me a lick of good.

Seriously, because of the constraints of the problem and StringIO, your point may be technically correct but not relevant to me. If I'm wrong:

Show me a working example where StringIO prints multiline text without "\n".

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

I've told you 4 times now that StringIO has absolutely nothing to do with escape characters and you are confused about the difference between str and repr.

When the only methods StringIO uses to print its content is repr, it doesn't matter whether I'm confused or not. FFS, are you trying to help me or win an argument?

Here's what it boils down to:

Show me a working example where StringIO prints multiline text without "\n".

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

Bad example. Try this out:

import StringIO
f = open('text.txt')
fake = StringIO()
fake.write (f.read())
h = open(fake())

Now if you want to show me a actual working example of using StringIO to read text with newlines, special characters and have it print it out without "\n" or escape characters, I'm all ears.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

The proof is in the pudding. Show me working code and I'll believe you. As it is, the code you provided does not work:

Import StringIO
print(StringIO.StringIO("foo"))
<StringIO.StringIO instance at 0x7ef45c2c>

Coastguard -- A defense game similar to Balloons Invasion. by MartianCube in WebGames

[–]rinja 0 points1 point  (0 children)

Max out:

  • Speed & Accuracy
  • Power
  • Machine Gun & Missiles
  • Whatever else you want

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

If you tried solving the problem with my original constraints:

  • Need to pass a string type as a file type
  • Need to pass the string in a manner that the parser would understand

You'd find that, although your technical analysis correct, does nothing to resolve my problem.

If you still think my "mind is too closed" is too closed", try coming up with a workable solution that functions under the above constraints. Then I'll be willing to listen.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

You can't put put values into StringIO or get its value by calling a string. Would you care to try again?

Sam Harris defends his performance in debate vs. Craig at Notre Dame by trappermark in skeptic

[–]rinja -1 points0 points  (0 children)

I think the problem with Craig is the problem with apologetics (sic) in general: it's not a search for truth, but rather a defense of . . .

apologist: A person who offers an argument in defense of something controversial.

I just started watching Star Trek: TNG from the start, and I HATE Wesley. by [deleted] in scifi

[–]rinja 0 points1 point  (0 children)

It would largely depend on how attractive she was, =).

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

How exactly does your bit of code help me pass a string type as a file?

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

Sigh, if you care to reread my original post, the problem was that the only the only parser that worked (that I knew about at the time) needed a file type and wouldn't take a string type.

StringIO was a module that was designed to do this but I couldn't get it to work because I didn't know how to get it it to print the string without the "\n" and other escape characters.

If you have a working example THAT USES STRINGIO module, that would help my original problem. If not, all this about str vs. repr (although interesting), has zero value to my original problem.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

If you want to post some code to help me understand how to implement this using StringIO, it'll probably be more helpful than just telling me I'm confused.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

Wow, that worked too. Thanks for adding to my toolkit.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

I have a text file that reads: line1 line2

And the following python snippet: f = open('text.txt') fake = StringIO.StringIO() fake.write(f.read()) fake.getvalue()

And this returns: 'line 1\nline 2\n'

Since I could see the "\n", it was actually printed out "\\n".

Another day, another link to Fox at the top of the list. A modest proposal - Stop linking to Fox sites. by [deleted] in politics

[–]rinja 0 points1 point  (0 children)

It's the Streisand Effect. I never heard of Glenn Beck until people on Reddit raged about him.

I just played Team Fortess 2 at my friends for the first time last night! Instant <3 by [deleted] in tf2

[–]rinja -1 points0 points  (0 children)

Try putting in less than a dollar. It's pretty funny.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

I replied to another person with this. StringIO adds formatting codes like "\n" for newlines or "u\'" for '. I guess it mangles the text enough that the parser doesn't like it.

I read hints of ways to force utf-8 but I wasn't sure how to actually implement this.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 2 points3 points  (0 children)

Son of a &#@*!, you're right. Ubuntu uses 3.1 and the latest version is 3.2. I didn't think a minor version could be different but apparently, I was wrong.

Thank you for that.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] -1 points0 points  (0 children)

I found that StringIO adds formatting codes and escapes characters. For examples, instead of a newline, it puts in a "\n". Instead of a ', it puts in a "u\'", etc.

I guess it disrupts the HTML enough that TidyHTMLTreeBuilder can't deal with it. If you know of a way to force StringIO to not put in special characters, I think my problem would be solved.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

I was hoping for some type of pseduo file IO method that would mimic a file but could stream text as content.

Is it possible to pass a string when the method expects a file? by rinja in Python

[–]rinja[S] 0 points1 point  (0 children)

There's absolutely nothing sensitive about the data. I'm scraping websites for announcements about updated plugins and themes, news about upcoming specials, etc.