Ok im doing some coding work making a username saver, I need to save multiple users to different text files, But I get an error when trying to set the file directory,
fileloc = "N:\Documents\1) Subjects, Word and Powerpoint\GCSE Computing\NEA\GCSE 2017\users\"
filename = fileloc+newname+".txt"
print filename
adduserfile = open(filename, "rw+")
I get the error "EOL while scanning string literal", It then highlights the last quotation mark at the end of line 1, Im not sure how to fix this, Please help
Sorry for asking such a simple question, I understand that it was my use of the address because of the special characters () causing it to break, thanks for the time and help
Using forward slashes will work under Windows.
fileloc = "N:/Documents/1) Subjects, Word and Powerpoint/GCSE Computing/NEA/GCSE 2017/users/"
Alternatively you can use a raw string literal by prefixing an r.
fileloc = r"N:\Documents\1) Subjects, Word and Powerpoint\GCSE Computing\NEA\GCSE 2017\users\"
You need to be carefull about special chars "\":
fileloc = "N:\Documents\1) Subjects, Word and Powerpoint\GCSE Computing\NEA\GCSE 2017\users\\"
filename = fileloc+"newname"+".txt"
print filename
N:\Documents) Subjects, Word and Powerpoint\GCSE Computing\NEA\GCSE 2017\users\newname.txt
I used "newname" as a string here, you can set it and change as a var.
By ending your string with a \ you're escaping the next character (which is ") hence the string is not terminated.
Beware that you're also escaping the character next to every \
What you probably want
fileloc = "N:\\Documents\\1) Subjects, Word and Powerpoint\\GCSE Computing\\NEA\\GCSE 2017\\users\\"
learn more about character escape here: https://en.wikipedia.org/wiki/Escape_character
Related
Okay, so basically I want the user to be able to input something, like "quote python syntax and semantics", remove the word 'quote' and anything else (for example, the command could be, 'could you quote for me Python syntax and semantics') then format it in a way that I can pass it to the Wikipedia article URL (in this case 'https://en.wikipedia.org/wiki/Python_syntax_and_semantics'), request it and scrape the element(s) I want.
Any answer would be greatly appreciated.
Here's a simple example of doing this:
import re
msg = input() # Here give as input "quote python syntax and semantics"
repMsg = re.sub("quote", "", msg).strip() # Erase "quote" and space at the start
repMsg = re.sub(" ", "_", repMsg) # Replace spaces with _
print(repMsg) # prints "python_syntax_and_semantics"
The python regex module is very handy for doing this sort of things. Note that you'll probably need to fine tune your code e.g. decide when to replace first occurrence vs replace all, at which point to strip white spaces etc.
I asked a Python question minutes ago about how Python's newline work only to have it closed because of another question that's not even similar or have Python associated with it.
I have text with a '\n' character and '\t' in it, in a file. I read it using
open().read()
I then Stored the result in an identifier. My expectations is that such a text e.g
I\nlove\tCoding
being read from a file and assigned to an identifier should be same as one directly assigned to the string literal
"I\nlove\tCoding"
being directly assigned to a file.
My assumption was wrong anyway
word = I\nlove\tCoding
ends up being different from
word = open(*.txt).read()
Where the content of *.txt is exactly same as string "I\nlove\tCoding"
Edit:
I did make typo anyway, I meant \t && \n , searching with re module's search() for \t, it return None, but \t is there. Why is this please?
You need to differentiate between newlines/tabs and their corresponding escape sequences:
for filename in ('test1.txt', 'test2.txt'):
print(f"\n{filename} contains:")
fileData = open(filename, 'r').read()
print(fileData)
for pattern in (r'\\n', r'\n'):
# first is the escape sequences, second the (real) newline!
m = re.search(pattern, fileData)
if m:
print(f"found {pattern}")
Out:
test1.txt contains:
I\nlove\tCoding
found \\n
test2.txt contains:
I
love Coding
found \n
The string you get after reading from file is I\\nlove\\nCoding.If you want your string from literal equals string from file you should use r prefix. Something like this - word = r"I\nlove\nCoding"
I have a file in windows remote server which I'm trying to read in a pandas dataframe. The file path has white spaces and dots in it. Following is what I'm trying to do but its not working for me.
file_location = '\\servername\foldername\folder name\5. folder_name\foldername - 0331v7\filename.txt'
df = pd.read_csv(filelocation, sep = '|')
I'm getting the no such file exists error. I tried to prefix the file_location string with r and thats not working too. I would appreciate if someone could help me with this.
Backslahes are used to tell the parser that the next character should be interpreted as an escapce sequence and not a regular character/operator.
For example if you want to print " you have to use the escape sequence \":
print("This is a quotation mark: \".")
Output:
This is a quotation mark: ".
If you are using a single backslash the next character is marked as escape sequence and therefore makes the path invalid. To get around this you can either use the escape sequence for backslash wich is \ or in most cases you can use a forward slash as most librarys automatically convert it to a backslash on windows.
I'm trying to remove the apostrophe from a string in python.
Here is what I am trying to do:
source = 'weatherForecast/dataRAW/2004/grib/tmax/'
destination= 'weatherForecast/csv/2004/tmax'
for file in sftp.listdir(source):
filepath = source + str(file)
subprocess.call(['degrib', filepath, '-C', '-msg', '1', '-Csv', '-Unit', 'm', '-namePath', destination, '-nameStyle', '%e_%R.csv'])
filepath currently comes out as the path with wrapped around by apostrophes.
i.e.
`subprocess.call(['', 'weatherForecast/.../filename')]`
and I want to get the path without the apostrophes
i.e.
subprocess.call(['', weatherForecast/.../filename)]
I have tried source.strip(" ' ", ""), but it doesn't really do anything.
I have tried putting in print(filepath) or return(filepath) since these will remove the apostrophes but they gave me
syntax errors.
filepath = print(source + str(file))
^
SyntaxError: invalid syntax
I'm currently out of ideas. Any suggestions?
The strip method of a string object only removes matching values from the ends of a string, it stops searching for matches when it first encounters a non-required character.
To remove characters, replace them with the empty string.
s = s.replace("'", "")
The accepted answer to this question is actually wrong and can cause lots of trouble. strip method removes as leading/trailing characters. So you use it when you have character to remove from start and end.
If you use replace instead, you will change all characters in the string. Here is a quick example.
my_string = "'Hello rokman's iphone'"
my_string.replace("'", "")
The above code will return Hello rokamns iphone. As you can see you lost the quote before s. This is not someting you would need in your case. However, you only parse location without that character I believe. That's why it was ok for you to use at that time.
For the solution, you are doing just one thing wrong. When you call strip method you leave space before and after. The right way to use it should be like this.
my_string = "'Hello world'"
my_string.strip("'")
However this assumes that you got ', if you get " from the response you can change quotes like this.
my_string = '"Hello world"'
my_string.strip('"')
from glob import glob
pattern = "D:\\report\\shakeall\\*.txt"
filelist = glob(pattern)
def countwords(fp):
with open(fp) as fh:
return len(fh.read().split())
print "There are" ,sum(map(countwords, filelist)), "words in the files. " "From directory",pattern
import os
uniquewords = set([])
for root, dirs, files in os.walk("D:\\report\\shakeall"):
for name in files:
[uniquewords.add(x) for x in open(os.path.join(root,name)).read().split()]
print "There are" ,len(uniquewords), "unique words in the files." "From directory", pattern
So far my code is this. This counts the number of unique words and total words from D:\report\shakeall\*.txt
The problem is, for example, this code recognizes code code. and code! different words. So, this can't be an answer to an exact number of unique words.
I'd like to remove special characters from 42 text files using Windows text editor
Or make an exception rule that solve this problem.
If using the latter, how shoud I make up my code?
Make it to directly modify text files? Or make an exception that doesn't count special characters?
import re
string = open('a.txt').read()
new_str = re.sub('[^a-zA-Z0-9\n\.]', ' ', string)
open('b.txt', 'w').write(new_str)
It will change every non alphanumeric char to white space.
I'm pretty new and I doubt this is very elegant at all, but one option would be to take your string(s) after reading them in and running them through string.translate() to strip out the punctuation. Here is the Python documentation for it for version 2.7 (which i think you're using).
As far as the actual code, it might be something like this (but maybe someone better than me can confirm/improve on it):
fileString.translate(None, string.punctuation)
where "fileString" is the string that your open(fp) read in. "None" is provided in place of a translation table (which would normally be used to actually change some characters into others), and the second parameter, string.punctuation (a Python string constant containing all the punctuation symbols) is a set of characters that will be deleted from your string.
In the event that the above doesn't work, you could modify it as follows:
inChars = string.punctuation
outChars = ['']*32
tranlateTable = maketrans(inChars, outChars)
fileString.translate(tranlateTable)
There are a couple of other answers to similar questions i found via a quick search. I'll link them here, too, in case you can get more from them.
Removing Punctuation From Python List Items
Remove all special characters, punctuation and spaces from string
Strip Specific Punctuation in Python 2.x
Finally, if what I've said is completely wrong please comment and i'll remove it so that others don't try what I've said and become frustrated.
import re
Then replace
[uniquewords.add(x) for x in open(os.path.join(root,name)).read().split()]
By
[uniquewords.add(re.sub('[^a-zA-Z0-9]*$', '', x) for x in open(os.path.join(root,name)).read().split()]
This will strip all trailing non-alphanumeric characters from each word before adding it to the set.
When working in Linux, some system files in /proc lib contains chars with ascii value 0.
full_file_path = 'test.txt'
result = []
with open(full_file_path, encoding='utf-8') as f:
line = f.readline()
for c in line:
if ord(c) == 0:
result.append(' ')
else:
result.append(c)
print (''.join(result))