So you need a nested loop: for each tuple in the list, for each element in the tuple, intify if possible. The way to do that is with the csv module, not by calling literal_eval on each value.īut you don't just have a tuple, you have a list of tuples. In fact, from your comments, it seems like what you're trying to reverse is CSV quoting. (Or, better, change the generating code to generate something more sensible in the first place, if possible…) All of your examples happen to work with either one, but that isn't the point what you really want to do is look at what generated this code, and write the appropriately corresponding code to reverse that. It's not actually clear here whether ast.literal_eval or json.loads or something different is what you want here. ![]() The word representation of numbers could be anywhere in the passage. ![]() Also takes care of the decimal part, if present. It can find numbers present in word form in a sentence and then convert them to the proper numeric format. The literal_eval will turn '"Earth, LGO"' into the string 'Earth, LGO', but the int on that will fail, so you'll get the original (extra-quoted) string. Make use of the Python package: WordToDigits. The literal_eval will turn '"3"' into the string '3', and the int on that will return the integer 3. The literal_eval will turn '20121001' into the integer 20121001, and the int on that will return it directly. To do that, you need to first try to interpret the value as a string representation of some Python value, and then interpret the result of that as an integer. ![]() In Python 2.x, this will give you a list in 3.x, an iterator if you need a tuple back, either way, the tuple function will convert it for you: > new_ex = tuple(map(safe_int, ex))īut you apparently don't want to just handle just '20121001', but also '"3"'. Then, to call that function on every element in the tuple, you need to loop over the tuple-whether an explicit for statement, a comprehension, or a call to map. If you know about try/ except and the int function, you should be able to write a safe_int function that returns int(x) if it succeeds and x if it doesn't.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |