string-formatting

pandas dataframe string formatting (access a given column)

依然范特西╮ 提交于 2019-12-13 01:53:16
问题 I try to use new-style formatting to display the entry at a given/specified column: np.random.seed(1234) df = pd.DataFrame(np.random.randint(7, size=(2, 2)), columns=['a', 'b']) c = df.iloc[0, :] # get row number 0 print("Here is {one[0]} and {two}".format(one=c, two=c['b'])) # Ok But I'd like to do it as follows: print("Here is {one['a']} and {two}".format(one=c, two=c['b'])) ## Unfortunately KeyError: "'a'" Is it possible to do that and how? 回答1: I think you can remove '' in one['a'] :

C# custom formatting a datagridview column's displayed data without changing the underlying values

為{幸葍}努か 提交于 2019-12-12 20:50:37
问题 I have a datagridview being populated by a collection of objects. the values within the first column are similar to: "SOMEDISPLAYTEXT#T:\blasndw\lwwdjawn\wjnawdja" "somedisplaytext#T:\kndwla\igrhysbv\kjnfak" I do not wish to wish to change these values, as i am also constantly updating them, however, i wish the datagridview to only show the first part of this string 'somedisplaytext', up to but not including the '#' ..without changing the underlying values. 回答1: If you use WinForms: According

How can I use my enum in QString.arg()?

纵然是瞬间 提交于 2019-12-12 18:27:28
问题 My enum is declared as Q_ENUM macro so it print the enum field's name when using with qDebug() (as I'm using QT 5.5) instead of its value. I'd like to do the same with QString().arg() so I declared that same enum with Q_DECLARE_METATYPE() macro but it didn't work either and give the below error. Code: qDebug() << QString("s = %1").arg(myClass::myEnum::ok); error: error: no matching function for call to 'QString::arg(myClass::myEnum)' How can I fix this? 回答1: Q_ENUM does not provide a direct

scanf(“%d”, char*) - char-as-int format string?

孤街醉人 提交于 2019-12-12 18:09:51
问题 What is the format string modifier for char-as-number? I want to read in a number never exceeding 255 (actually much less) into an unsigned char type variable using sscanf. Using the typical char source[] = "x32"; char separator; unsigned char dest; int len; len = sscanf(source,"%c%d",&separator,&dest); // validate and proceed... I'm getting the expected warning: argument 4 of sscanf is type char*, int* expected. As I understand the specs, there is no modifier for char (like %sd for short, or

Check if a string to interpolate provides expected placeholders

人走茶凉 提交于 2019-12-12 18:08:38
问题 Consider this fictitious Python function: def f(s): # accepts a string containing placeholders # returns an interpolated string return s % {'foo': 'OK', 'bar': 'OK'} How can I check that the string s provides all the expected placeholders, and if not, make the function politely show the missing keys? My solution follows. My question: is there a better solution? import sys def f(s): d = {} notfound = [] expected = ['foo', 'bar'] while True: try: s % d break except KeyError as e: key = e.args[0

How to delegate to default format provider?

99封情书 提交于 2019-12-12 11:58:31
问题 I'd like to create an EscapingFormatProvider that automatically HTML-escapes formatted strings before returning them, unless the format argument starts with a !: string.Format(new EscapingFormatProvider(), "<div>{0}</div>", "<script src='foo'></script>"); // => <div><script src='foo'></script></div> string.Format(new EscapingFormatProvider(), "<div>{0:!}</div>", "<script src='foo'></script>"); // => <div><script src='foo'></script></div> I'd like it to pass the format argument, less the ! if

Is there a “branching” string format descriptor?

别等时光非礼了梦想. 提交于 2019-12-12 11:13:20
问题 I just did this printf( (n==0) ? " %d" : " %3d", n ); but is there a conditional format descriptor? So, it would mean something like "if this is very short use such and such padding, but, if this is longer use such and such padding." Can do? 回答1: There's no conditional, but you can use * to specify the width in the arguments. e.g.: printf(" %*d", (n==0)?1:3, n); 回答2: You can pass the field width as an argument using * : printf("%*d", length, n); 回答3: Other answers have pointed out the field

Use scientific notation only if needed

狂风中的少年 提交于 2019-12-12 11:11:08
问题 I want to parse a double value into a string . I want my number to have a specified number of digits (that I won't know until runtime). If this number can be expressed with a non-zero value in number of digits this is what I want. If the number comes out as zero's like this I want it expressed in scientific notation. Some examples will make this more clear, this assumes I wanted 3 digits: Value: .2367 Output: "0.23" Value: .00367 Output: "3.67E-3" Value: 22.3 Output: "22.3" Value: 3364.0

Changing the default thousand and decimal separator in a binding

こ雲淡風輕ζ 提交于 2019-12-12 10:39:47
问题 Let's say I have a number 1234567.89 . The number is displayed in a WPF TextBlock. I am trying to apply StringFormat attribute to the Text property so that the number would be displayed like: 1.234.567,89 As you can see, the thousand and decimal separators are inverted from the invariant culture specification. I've tried setting numerous combinations for the StringFormat , but without success. This is the latest I came up with: Text="{Binding SomeBinding, StringFormat={}{0:#'.'##0','00}}" But

How to format output using MATLAB's num2str

烈酒焚心 提交于 2019-12-12 08:16:32
问题 I'm trying to ouput an array of numbers as a string in MATLAB. I know this is easily done using num2str, but I wanted commas followed by a space to separate the numbers, not tabs. The array elements will at most have resolution to the tenths place, but most of them will be integers. Is there a way to format output so that unnecessary trailing zeros are left off? Here's what I've managed to put together: data=[2,3,5.5,4]; datastring=num2str(data,'%.1f, '); datastring=['[',datastring(1:end-1),'