Python Forum

Full Version: reading from a file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
(Jul-15-2020, 12:51 PM)GOTO10 Wrote: [ -> ]You aren't doing anything wrong, you just have more work to do. The Â\xa0 is a control character for a space. You could probably get rid of all of them by using the replace() method on each string, or you may need to look into using the unicodedata module or something similar. Also, if you want the numeric values to have a type other than string, you'll need to convert them to int or float as needed.
i dont understand how to use replace() in this case can you demonstrate
You'll need to figure out how to get into your nested lists on your own, since this is the homework forum after all; but at the simplest level, what I'm suggesting would look like this:
some_string = 'Â\xa0Text_To_Keep'

# Using string.replace() to replace the 'Â\xa0' with an empty string ('').
new_string = some_string.replace('Â\xa0', '')

print(new_string)
Output:
Text_To_Keep
Also, better form not to print from within the function. Rather, print the returned value in the calling script
print(get_csv_as_table(userFileName, userDelimiter))
Or, if you want to use the value and do something more with it
my_output = get_csv_as_table(userFileName, userDelimiter)
print(my_output)
Another nit pick - in naming your variables, it is considered proper form in Python to avoid camelCase and instead use underscores between_words_in_your variable names. Note how GOTO10 did that in his post above.
(Jul-15-2020, 02:40 PM)GOTO10 Wrote: [ -> ]You'll need to figure out how to get into your nested lists on your own, since this is the homework forum after all; but at the simplest level, what I'm suggesting would look like this:
some_string = 'Â\xa0Text_To_Keep'

# Using string.replace() to replace the 'Â\xa0' with an empty string ('').
new_string = some_string.replace('Â\xa0', '')

print(new_string)
Output:
Text_To_Keep
i was told that the way im doing is incorrect because in my list i get all the values as a string how am i to approach this to convert the things after the delimiter \t to numbers.and can you be kind enough to tell where i have went wrong
Have you modified your code based on any of the recent suggestions? What does it look like right now? I bet you already know a Python function that you can use to convert a string to an integer, so you need to find a way to apply a function like that to the string values in your lists that need to be converted.
Pages: 1 2